• 周二. 9月 27th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Js导出Excel

admin

11月 28, 2021

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3 <html xmlns="http://www.w3.org/1999/xhtml"> 
  4 <head> 
  5     <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  6     <title>导出EXCEL文档</title> 
  7 </head> 
  8 <body> 
  9     <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"> 
 10         <tr> 
 11             <td colspan="5" align="center"> 
 12                 导出EXCEL文档
 13             </td> 
 14         </tr> 
 15         <tr> 
 16             <td> 
 17                 列标题1 
 18             </td> 
 19             <td> 
 20                 列标题2 
 21             </td> 
 22             <td> 
 23                 列标题3 
 24             </td> 
 25             <td> 
 26                 列标题4 
 27             </td> 
 28             <td> 
 29                 列标题5 
 30             </td> 
 31         </tr> 
 32         <tr> 
 33             <td> 
 34                 题1 
 35             </td> 
 36             <td> 
 37 题2 
 38             </td> 
 39             <td> 
 40                 题3 
 41             </td> 
 42             <td> 
 43                 题4
 44             </td> 
 45             <td> 
 46                 题5 
 47             </td> 
 48         </tr> 
 49         <tr> 
 50             <td> 
 51                 AAA 
 52             </td> 
 53             <td> 
 54 
 55             </td> 
 56             <td> 
 57                 CCC 
 58             </td> 
 59             <td> 
 60                 DDD 
 61             </td> 
 62             <td> 
 63                 EEE 
 64             </td> 
 65         </tr> 
 66         <tr> 
 67             <td> 
 68                 FFF 
 69             </td> 
 70             <td> 
 71                 GGG 
 72             </td> 
 73             <td> 
 74                 HHH 
 75             </td> 
 76             <td> 
 77                 III 
 78             </td> 
 79             <td> 
 80                 JJJ 
 81             </td> 
 82         </tr> 
 83     </table> 
 84     <input type="button" onclick="javascript:method1('tableExcel');" value="第一种方法导入到EXCEL"> 
 85     <input type="button" onclick="javascript:method2('tableExcel');" value="第二种方法导入到EXCEL"> 
 86     
 87     <script language="javascript"> 
 88         function method1(tableid) {//整个表格拷贝到EXCEL中 
 89             var curTbl = document.getElementById(tableid); 
 90             var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel 
 91             var oWB = oXL.Workbooks.Add(); //获取workbook对象 
 92             var oSheet = oWB.ActiveSheet; //激活当前sheet 
 93             var sel = document.body.createTextRange(); 
 94             sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 
 95             sel.select(); //全选TextRange中内容 
 96             sel.execCommand("Copy"); //复制TextRange中内容 
 97             oSheet.Paste(); //粘贴到活动的EXCEL中 
 98             oXL.Visible = true; //设置excel可见属性 
 99         } 
100         function method2(tableid) //读取表格中每个单元到EXCEL中 
101         { 
102             var curTbl = document.getElementById(tableid); 
103             var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel 
104             var oWB = oXL.Workbooks.Add(); //获取workbook对象 
105             var oSheet = oWB.ActiveSheet; //激活当前sheet 
106             var Lenr = curTbl.rows.length; //取得表格行数 
107             for (i = 0; i < Lenr; i++) { 
108                 var Lenc = curTbl.rows(i).cells.length; //取得每行的列数 
109                 for (j = 0; j < Lenc; j++) { 
110                     oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; //赋值 
111                 } 
112             } 
113             oXL.Visible = true; //设置excel可见属性 
114         } 
115        
116     </script> 
117 </body> 
118 </html>

View Code

就这么简单,你懂得

发表回复

您的电子邮箱地址不会被公开。