JavaScript – 将HTML表格数据导出到Excel中

我试图将HTML表格转换为Excel,我已经尝试了将简单的表格转换为Excel的JavaScript函数,它工作正常。 如果我有多个表格,我将如何能够将所有表格数据添加到Excel文件中。 这是我的尝试。 我创build了2个表,并给出表索引testTabletestTable1

如何通过点击button将这两个表格id传递给JavaScript函数? 现在点击button只有第一个表格导出到Excel,因为我只传递'testTable' 。 我将如何能够导出多个表,例如: testTabletestTable1到Excel?

这里是JavaScript:

 <script> 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)) } })() </script> 

这是HTML部分,

 <table id="testTable"> <thead> <tr> <th>Name</th> <th>ACP</th> <th>OEMCP</th> <th>Unix<br> NT 3.1</th> <th>Unix<br> NT 3.51</th> <th>Unix<br> 95</th> </tr> </thead> </table> <table id="testTable1"> <thead> <tr> <th>Name</th> <th>ACP</th> <th>OEMCP</th> <th>Windows<br> NT 3.1</th> <th>Windows<br> NT 3.51</th> <th>Windows<br> 95</th> </tr> </thead> </table> 

请让我知道,这可以做到这一点?
谢谢

我build议另一种格式的方法。 约翰Resig微型模板是一个非常好的和简单的工具来做你所需要的。 ( ejohn microtemplating )

 (function(){ var cache = {}; this.tmpl = function tmpl(str, data){ // Figure out if we're getting a template, or if we need to // load the template - and be sure to cache the result. var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) : // Generate a reusable function that will serve as a template // generator (and which will be cached). new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + // Introduce the data as local variables using with(){} "with(obj){p.push('" + // Convert the template into pure JavaScript str.replace(/[\r\t\n]/g, " ") .split("{{").join("\t") .replace(/((^|}})[^\t]*)'/g, "$1\r") .replace(/\t=(.*?)}}/g, "',$1,'") .split("\t").join("');") .split("}}").join("p.push('") .split("\r").join("\\'") + "');}return p.join('');"); // Provide some basic currying to the user return data ? fn( data ) : fn; }; })(); 

这是非常简单的使用。 这不仅允许在HTML之间显示variables,还可以执行JavaScript代码

您的模板string需要修改才能使用此microtemplate。

 {{for(var i=0; i<tables.length;i++){ }} <table> {{=tables[i]}} </table> {{ } }} 

最后只需要select你的例子中出现的所有表

 document.getElementsByTagName("table"); 

你可以看到它是如何工作http://jsfiddle.net/Scipion/P8rpn/1/

创build一个函数并将tableID传递给它

  function passing_id_to_excel(tableID){ var myTableid = document.getElementById(tableID) //remaining code } 
  1. 为每个表添加一个checkbox。 使用JavaScript来处理那些被检查的。
  2. 如果你只是想转换每个表,你可以使用$('table').each(function() { do something })

检查我的答案在这里: –

使用JavaScript / JQuery将HTML表格数据导出到Excel在Chrome浏览器中无法正常工作