使用jQuery从html表生成Excel表

一旦用户点击一个button,我想生成一个Excel工作表。 基本上我想要做什么在这里讨论

如何通过HTML表格值Excel表格单元格 (Kalle H.Väravas答案)

JavaScript将HTML表格导出到Excel

但不知何故,当我点击button什么也没有发生。 我正在使用Mozilla浏览器。 我的代码需要启用ActiveXObject。 我需要做些额外的事情来完成它。 ?

我创build了这个小提琴进行testing。 如果这个工程,我会尝试我的真实的代码。 如果这对你有用,那么也请让我知道。 谢谢

jFiddle

代码:

JS:

<script type="text/javascript"> function CreateExcelSheet() { var i, j, str, myTable = document.getElementById('mytable'), rowCount = myTable.rows.length, excel = new ActiveXObject('Excel.Application');// Activates Excel excel.Workbooks.Add(); // Opens a new Workbook excel.Application.Visible = true; // Shows Excel on the screen for (i = 0; i < rowCount; i++) { for (j = 0; j < myTable.rows[i].cells.length; j++) { str = myTable.rows[i].cells[j].innerText; excel.ActiveSheet.Cells(i + 1, j + 1).Value = str; // Writes to the sheet } } return; } </script> 

Html:

 <table id ="myTable" > <tr> <td>1</td> <td>Jan</td> <td>01/04/2012</td> <td>Approved</td> </tr> <tr> <td>2</td> <td>Feb</td> <td>01/04/2012</td> <td>Approved</td> </tr> </table> <form> <input type="button" onclick="CreateExcelSheet();" value="Create Excel Sheet" > </form> 

下面是答案:通过insin 导出dynamicHTML表格,以在Firefox中的JavaScript中performance优异

 var tableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,' , template = '<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"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>' , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } return function(table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} window.location.href = uri + base64(format(template, ctx)) } })() 

要回答关于IE的问题(由于声望重新定义,我无法在评论框中回答),Sharun发布的上述代码在IE 10+('权限被拒绝'错误)上不起作用。 这里有一个可以在IE 10+以及IE,Chrome和FF的旧版本中工作的片段:

 var TableToExcel = (function () { var uri = 'data:application/vnd.ms-excel;base64,' , template = '<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"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table cellspacing="0" rules="rows" border="1" style="color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:9pt;text-align:center;">{table}</table></body></html>' , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } return function (table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } if (navigator.msSaveBlob) { var blob = new Blob([format(template, ctx)], { type: 'application/vnd.ms-excel', endings: 'native' }); navigator.msSaveBlob(blob, 'export.xls') } else { window.location.href = uri + base64(format(template, ctx)) } } })() 
  $("#btnExport").click(function(e) { e.preventDefault(); //getting data from table var data_type = 'data:application/vnd.ms-excel'; var table_div = document.getElementById('table_wrapper');//table_wrapper is table id var table_html = table_div.outerHTML.replace(/ /g, '%20'); var a = document.createElement('a'); a.href = data_type + ', ' + table_html; a.download = 'table_name_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls'; a.click(); }); 

你可以使用这个好的库: https : //github.com/jmaister/excellentexport