多个HTML表格转换为Excel

时间:2014-09-08 14:03:59

标签: php html excel

我想将HTML表格转换为工作表,同一页面上的多个表格应转换为工作簿。

var htmltable= document.getElementById('table');
var html = htmltable.outerHTML;
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));'

我已经尝试过这段代码将html表转换为excel文件,它对单表工作正常。如果我有多个表并希望从每个表创建多个工作表怎么办?

1 个答案:

答案 0 :(得分:0)

如果每个表都有唯一的id,您只需将id替换为第一行。例如:

<table name="mytable" id="mytable">...</table>
<table name="urtable" id="urtable">...</table>

<强>的JavaScript

var t1 = document.getElementById('mytable');
var xl1 = t1.outerHTML;
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(xl1));
var t2 = document.getElementById('urtable');
var xl2 = t2.outerHTML;
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(xl2));