本文仅仅是描述前端WEB或者是在做demo时如何简单实现Excel导出。
[1]、主要的测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>JavaScript实现WEB页面导出为EXCEL的方法</title> </head> <body> <br /> <table id="michale_table" width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="5" align="center">JavaScript实现WEB页面导出为EXCEL的方法</td> </tr> <tr> <td>主题</td> <td>site地址</td> <td>作者</td> </tr> <tr> <td>我的blog</td> <td>http://www.micmiu.com/</td> <td>Michael and Hazel</td> </tr> <tr> <td>keepalived实现双机热备</td> <td>http://www.micmiu.com/enterprise-app/cluster/keepalived-hot-standby/</td> <td>Michael</td> </tr> <tr> <td>条形码/二维码之开源利器ZXing图文介绍</td> <td>http://www.micmiu.com/enterprise-app/java-zxing/</td> <td>Michael</td> </tr> </table> <br /> <input type="button" onclick="javascript:exportTableToExcel('michale_table');" value="导出Excel" /> <br /> <script type="text/javascript"> function exportTableToExcel(table_id){ var o_AXO = new ActiveXObject("Excel.Application"); var o_WB; var o_Sheet; try{ o_WB = o_AXO.Workbooks.Add(); //激活当前sheet o_Sheet = o_WB.ActiveSheet; }catch(err){ alert("仅适用于IE浏览器,同时请确认已经安装好Excel软件"); return false; } var o_table = document.getElementById(table_id); var row_len = o_table.rows.length; for (i = 0; i < row_len; i++){ var cell_len = o_table.rows[i].cells.length; for (j = 0; j < cell_len; j++){ var cellText = o_table.rows[i].cells[j].innerText; o_Sheet.Cells(i + 1, j + 1).value = cellText; } } o_AXO.Visible = true; } </script> </body> </html> |
[2]、页面截图:[……]
近期评论