Javascript 如何将html表格转换为具有多个工作表的excel?
如何将多个html表格转换为包含多个工作表的excel表格?请你帮个忙好吗Javascript 如何将html表格转换为具有多个工作表的excel?,javascript,jquery,json,html,Javascript,Jquery,Json,Html,如何将多个html表格转换为包含多个工作表的excel表格?请你帮个忙好吗 <button onclick="saveFile()">Save XLSX file</button> 我的例子 <button onclick="saveFile()">Save XLSX file</button> 函数tablesToExcel(){ { var tab_text=“”; var textRange;var j=0; tab=document.g
<button onclick="saveFile()">Save XLSX file</button>
我的例子
<button onclick="saveFile()">Save XLSX file</button>
函数tablesToExcel(){
{
var tab_text=“”;
var textRange;var j=0;
tab=document.getElementById('tbl2');//表的id
对于(j=0;j0 | |!!navigator.userAgent.match(/Trident.*rv \:11\./)//如果Internet Explorer
{
打开(“txt/html”,“replace”);
txtArea1.文档。写入(制表符文本);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand(“SaveAs”,true,“Say than to Sumit.xls”);
}
else//其他浏览器未在IE 11上测试
sa=window.open('data:application/vnd.ms excel',+encodeURIComponent(制表符文本));
返回(sa);
}
}
您可以执行以下操作:
<button onclick="saveFile()">Save XLSX file</button>
Html:
<table id="tbl1" class="table2excel">
<tr>
<td>Product</td>
<td>Price</td>
<td>Available</td>
<td>Count</td>
</tr>
<tr>
<td>Bred</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>Butter</td>
<td>4 </td>
<td>5 </td>
<td >6 </td>
</tr>
</table>
<hr>
<table id="tbl2" class="table2excel">
<tr>
<td>Product</td>
<td>Price</td>
<td>Available</td>
<td>Count</td>
</tr>
<tr>
<td>Bred</td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
<tr>
<td>Butter</td>
<td>14</td>
<td>15</td>
<td >16</td>
</tr>
</table>
<button onclick="tablesToExcel(['tbl1','tbl2'], ['ProductDay1','ProductDay2'], 'TestBook.xls', 'Excel')">Export to Excel</button>
<button onclick="saveFile()">Save XLSX file</button>
产品
价格
可用
计数
培育
1.
2.
3.
黄油
4.
5.
6.
产品
价格
可用
计数
培育
7.
8.
9
黄油
14
15
16
输出到Excel
Javascript:
var tablesToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
+ '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>'
+ '<Styles>'
+ '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>'
+ '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>'
+ '</Styles>'
+ '{worksheets}</Workbook>'
, tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'
, tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'
, 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(tables, wsnames, wbname, appname) {
var ctx = "";
var workbookXML = "";
var worksheetsXML = "";
var rowsXML = "";
for (var i = 0; i < tables.length; i++) {
if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
for (var j = 0; j < tables[i].rows.length; j++) {
rowsXML += '<Row>'
for (var k = 0; k < tables[i].rows[j].cells.length; k++) {
var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");
var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");
var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");
dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML;
var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");
dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null;
ctx = { attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':''
, nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String'
, data: (dataFormula)?'':dataValue
, attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':''
};
rowsXML += format(tmplCellXML, ctx);
}
rowsXML += '</Row>'
}
ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i};
worksheetsXML += format(tmplWorksheetXML, ctx);
rowsXML = "";
}
ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML};
workbookXML = format(tmplWorkbookXML, ctx);
var link = document.createElement("A");
link.href = uri + base64(workbookXML);
link.download = wbname || 'Workbook.xls';
link.target = '_blank';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
})();
<button onclick="saveFile()">Save XLSX file</button>
var tablesToExcel=(函数(){
var uri='data:application/vnd.ms excel;base64,'
,tmplWorkbookXML=“”
+“阿克塞尔·里希特{创建}”
+ ''
+ ''
+ ''
+ ''
+“{工作表}”
,tmplWorksheetXML='{rows}'
,tmplCellXML='{data}'
,base64=函数{return window.btoa(unescape(encodeURIComponent))}
,format=函数(s,c){返回s.replace(/{(\w+)}/g,函数(m,p){返回c[p];})}
返回函数(表、WSName、wbname、appname){
var ctx=“”;
var workbookXML=“”;
var工作表xml=“”;
var rowsXML=“”;
对于(变量i=0;i
函数表ToExcel(){
{
var tab_text=document.getElementById(“MsoNormalTable”).outerHTML;
var textRange;var j=0;
var tab=document.getElementById('MsoNormalTable');//表的id
var sa;
var ua=window.navigator.userAgent;
变量msie=ua.indexOf(“msie”);
var txt=document.getElementById('txtArea1').contentWindow;
如果(msie>0 | |!!navigator.userAgent.match(/Trident.*rv \:11\./)//如果Internet Explorer
{
打开(“txt/html”,“替换”);
txt.document.write(制表符文本);
txt.document.close();
txt.focus();
sa=txt.document.execCommand(“SaveAs”,true,“感谢Sumit.xls”);
}
else//其他浏览器未在IE 11上测试
sa=window.open('data:application/vnd.ms excel',+encodeURIComponent(制表符文本));
返回(sa);
}
}
它正在与IE7+协作(很好…:) 是一个更好的解决方案,它支持以最新的Excel格式导出表,即xlsx
<button onclick="saveFile()">Save XLSX file</button>
. 如果在Chrome上导出的行总数超过3407,则接受的解决方案将失败
<button onclick="saveFile()">Save XLSX file</button>
上面链接中的一个示例:
<button onclick="saveFile()">Save XLSX file</button>
html
<button onclick="saveFile()">Save XLSX file</button>
不丹维杰的代码运行得非常好。为了与IE 11兼容,我使用了Blob对象,如下所示:
<button onclick="saveFile()">Save XLSX file</button>
var tablesToExcel=(函数(){
var uri='数据:应用程序/v