1. 程式人生 > >純前臺js json匯出到excel,不與後臺互動

純前臺js json匯出到excel,不與後臺互動

上程式碼

[javascript] view plain copy
  1. <html>  
  2. <head>  
  3.     <meta http-equiv="content-type" content="text/html; charset=utf-8">  
  4.     <script type="text/javascript" src="jquery.min.js"></script>  
  5.     <script type="text/javascript">  
  6.         $(document).ready(function(){  
  7.             $('#wwo').click(function(){  
  8.                 var data = {"title":[{"value":"集團""type":"ROW_HEADER_HEADER""datatype":"string"}, {"value":"日期""type":"ROW_HEADER_HEADER""datatype":"string"}],"data":[[{"value":"好好""type":"ROW_HEADER"}, {"value":"2015-08-24""type":"ROW_HEADER"}]]};  
  9.                 if(data == '')  
  10.                     return;  
  11.                 JSONToExcelConvertor(data.data, "Report", data.title);  
  12.             });  
  13.         });  
  14.         function JSONToExcelConvertor(JSONData, FileName, ShowLabel) {  
  15.             //先轉化json
  16.             var arrData = typeof JSONData != 'object'
     ? JSON.parse(JSONData) : JSONData;  
  17.             var excel = '<table>';      
  18.             //設定表頭
  19.             var row = "<tr>";  
  20.             for (var i = 0, l = ShowLabel.length; i < l; i++) {  
  21.                 row += "<td>" + ShowLabel[i].value + '</td>';  
  22.             }  
  23.             //換行
  24.             excel += row + "</tr>";  
  25.             //設定資料
  26.             for (var i = 0; i < arrData.length; i++) {  
  27.                 var row = "<tr>";  
  28.                 for (var index in arrData[i]) {  
  29.                     var value = arrData[i][index].value === "." ? "" : arrData[i][index].value;  
  30.                     row += '<td>' + value + '</td>';  
  31.                 }  
  32.                 excel += row + "</tr>";  
  33.             }  
  34.             excel += "</table>";  
  35.             var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";  
  36.             excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';  
  37.             excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';  
  38.             excelFile += '; charset=UTF-8">';  
  39.             excelFile += "<head>";  
  40.             excelFile += "<!--[if gte mso 9]>";  
  41.             excelFile += "<xml>";  
  42.             excelFile += "<x:ExcelWorkbook>";  
  43.             excelFile += "<x:ExcelWorksheets>";  
  44.             excelFile += "<x:ExcelWorksheet>";  
  45.             excelFile += "<x:Name>";  
  46.             excelFile += "{worksheet}";  
  47.             excelFile += "</x:Name>";  
  48.             excelFile += "<x:WorksheetOptions>";  
  49.             excelFile += "<x:DisplayGridlines/>";  
  50.             excelFile += "</x:WorksheetOptions>";  
  51.             excelFile += "</x:ExcelWorksheet>";  
  52.             excelFile += "</x:ExcelWorksheets>";  
  53.             excelFile += "</x:ExcelWorkbook>";  
  54.             excelFile += "</xml>";  
  55.             excelFile += "<![endif]-->";  
  56.             excelFile += "</head>";  
  57.             excelFile += "<body>";  
  58.             excelFile += excel;  
  59.             excelFile += "</body>";  
  60.             excelFile += "</html>";  
  61.             var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);  
  62.             var link = document.createElement("a");      
  63.             link.href = uri;  
  64.             link.style = "visibility:hidden";  
  65.             link.download = FileName + ".xls";  
  66.             document.body.appendChild(link);  
  67.             link.click();  
  68.             document.body.removeChild(link);  
  69.         }  
  70.     </script>  
  71. </head>  
  72. <body>  
  73.     <input type="button" id="wwo" value="匯出" />  
  74. </body>  
  75. </html>  

附件是tableExport程式碼

http://download.csdn.net/detail/a410147597/9052583