js javascript 导出table表中数据到Excel中

时间:2022-12-24 09:19:27
Js代码  js javascript 导出table表中数据到Excel中
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
  2. <HTML>  
  3.  <HEAD>  
  4.   <TITLE> 导出到Excel </TITLE>  
  5.   <META NAME="Generator" CONTENT="EditPlus">  
  6.   <META NAME="Author" CONTENT="">  
  7.   <META NAME="Keywords" CONTENT="">  
  8.   <META NAME="Description" CONTENT="">  
  9.   <script>  
  10.     function toExcel(inTblId, inWindow) {   
  11.             try {   
  12.                 var allStr = "";   
  13.                 var curStr = "";   
  14.                 if (inTblId != null && inTblId != "" && inTblId != "null") {   
  15.                     curStr = getTblData(inTblId, inWindow);   
  16.                 }   
  17.                 if (curStr != null) {   
  18.                     allStr += curStr;   
  19.                 }   
  20.                 else {   
  21.                     alert("你要导出的表不存在!");   
  22.                     return;   
  23.                 }   
  24.                 var fileName = getExcelFileName();   
  25.                 doFileExport(fileName, allStr);   
  26.             }   
  27.             catch (e) {   
  28.                 alert("导出发生异常:" + e.name + "->" + e.description + "!");   
  29.             }   
  30.         }   
  31.         function getTblData(inTbl, inWindow) {   
  32.             var rows = 0;   
  33.             var tblDocument = document;   
  34.             if (!!inWindow && inWindow != "") {   
  35.                 if (!document.all(inWindow)) {   
  36.                     return null;   
  37.                 }   
  38.                 else {   
  39.                     tblDocument = eval(inWindow).document;   
  40.                 }   
  41.             }   
  42.             var curTbl = tblDocument.getElementById(inTbl);   
  43.             var outStr = "";   
  44.             if (curTbl != null) {   
  45.                 for (var j = 0; j < curTbl.rows.length; j++) {   
  46.                     for (var i = 0; i < curTbl.rows[j].cells.length; i++) {   
  47.                         if (i == 0 && rows > 0) {   
  48.                             outStr += " \t";   
  49.                             rows -= 1;   
  50.                         }   
  51.                         outStr += curTbl.rows[j].cells[i].innerText + "\t";   
  52.                         if (curTbl.rows[j].cells[i].colSpan > 1) {   
  53.                             for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {   
  54.                                 outStr += " \t";   
  55.                             }   
  56.                         }   
  57.                         if (i == 0) {   
  58.                             if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {   
  59.                                 rows = curTbl.rows[j].cells[i].rowSpan - 1;   
  60.                             }   
  61.                         }   
  62.                     }   
  63.                     outStr += "\r\n";   
  64.                 }   
  65.             }   
  66.             else {   
  67.                 outStr = null;   
  68.                 alert(inTbl + "不存在!");   
  69.             }   
  70.             return outStr;   
  71.         }   
  72.         function getExcelFileName() {   
  73.             var d = new Date();   
  74.             var curYear = d.getYear();   
  75.             var curMonth = "" + (d.getMonth() + 1);   
  76.             var curDate = "" + d.getDate();   
  77.             var curHour = "" + d.getHours();   
  78.             var curMinute = "" + d.getMinutes();   
  79.             var curSecond = "" + d.getSeconds();   
  80.             if (curMonth.length == 1) {   
  81.                 curMonth = "0" + curMonth;   
  82.             }   
  83.             if (curDate.length == 1) {   
  84.                 curDate = "0" + curDate;   
  85.             }   
  86.             if (curHour.length == 1) {   
  87.                 curHour = "0" + curHour;   
  88.             }   
  89.             if (curMinute.length == 1) {   
  90.                 curMinute = "0" + curMinute;   
  91.             }   
  92.             if (curSecond.length == 1) {   
  93.                 curSecond = "0" + curSecond;   
  94.             }   
  95.             var fileName = "卖场" + "_" + curYear + "年" + curMonth + "月" + curDate + "日" + "_"   
  96. + curHour + "点" + curMinute + "分" + curSecond + "秒" + ".csv";   
  97.             return fileName;   
  98.         }   
  99.         function doFileExport(inName, inStr) {   
  100.             var xlsWin = null;   
  101.             if (!!document.all("glbHideFrm")) {   
  102.                 xlsWin = glbHideFrm;   
  103.             }   
  104.             else {   
  105.                 var width = 6;   
  106.                 var height = 4;   
  107.                 var openPara = "left=" + (window.screen.width / 2 - width / 2)   
  108.    + ",top=" + (window.screen.height / 2 - height / 2)   
  109.    + ",scrollbars=no,width=" + width + ",height=" + height;   
  110.                 xlsWin = window.open("""_blank", openPara);   
  111.             }   
  112.             xlsWin.document.write(inStr);   
  113.             xlsWin.document.close();   
  114.             xlsWin.document.execCommand('Saveas'true, inName);   
  115.             xlsWin.close();   
  116.         }  
  117.   </script>  
  118.  </HEAD>  
  119.   
  120.  <BODY>  
  121.  <table id="toExcel" border="1" bgcolor="#999999">  
  122.      <tr>  
  123.           <td height="25" align="center" bgcolor="#CCCCCC">姓名</td>  
  124.           <td align="center" bgcolor="#CCCCCC">性别</td>  
  125.           <td align="center" bgcolor="#CCCCCC">年龄</td>  
  126.           <td align="center" bgcolor="#CCCCCC">地区</td>  
  127.           <td align="center" bgcolor="#CCCCCC">工作</td>  
  128.     </tr>  
  129.           <td align="center" bgcolor="#FFFFFF">黑色头发</td>  
  130.           <td align="center" bgcolor="#FFFFFF">男</td>  
  131.           <td align="center" bgcolor="#FFFFFF">99</td>  
  132.           <td align="center" bgcolor="#FFFFFF">北京</td>  
  133.           <td align="center" bgcolor="#FFFFFF">工程师</td>  
  134.     </tr>  
  135.     </tr>  
  136.           <td align="center" bgcolor="#FFFFFF">紫色头发</td>  
  137.           <td align="center" bgcolor="#FFFFFF">女</td>  
  138.           <td align="center" bgcolor="#FFFFFF">99</td>  
  139.           <td align="center" bgcolor="#FFFFFF">北京</td>  
  140.           <td align="center" bgcolor="#FFFFFF">工程师</td>  
  141.     </tr>  
  142.     </tr>  
  143.           <td align="center" bgcolor="#FFFFFF">白色头发</td>  
  144.           <td align="center" bgcolor="#FFFFFF">男</td>  
  145.           <td align="center" bgcolor="#FFFFFF">79</td>  
  146.           <td align="center" bgcolor="#FFFFFF">北京</td>  
  147.           <td align="center" bgcolor="#FFFFFF">歌手</td>  
  148.     </tr>  
  149. </table>  
  150.   <input type="button" onclick="javascript:toExcel('toExcel',null);" value="导出到EXCEL">   
  151.   
  152.  </BODY>  
  153. </HTML>