html table 导出为 xlsx 但 excel 2013 无法打开它
html table exported as xlsx but excel 2013 can't open it
这是我的函数,可将我的 html table 转换为 excel 文件。问题是,我需要将 excel 文件保存为 .xlsx,但我做不到。有人帮忙吗?
function exceller() {
var uri = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><head><!--[if gte mso 9]><?xml version="1.0" encoding="UTF-8" standalone="yes"?><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]; }) }
$('#toExcel').html($('#pztable').html());
var toExcel = $('#toExcel').html();
var ctx = {
worksheet: name || '',
table: toExcel
};
var link = document.createElement("a");
link.download = "export.xlsx";
link.href = uri + base64(format(template, ctx))
link.click();
link.remove();
}
这是 jsFiddle:https://jsfiddle.net/jxz60kqj/
您是否尝试过使用第三方 jQuery 插件?我一直在使用 jquery.table2excel.js
并且效果很好。
您需要包含 JQuery 插件和可以找到的 Table2Excel 插件 here。
代码:
$("button").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Worksheet Name",
filename: "SomeFile" //do not include extension
});
});
此处演示:
http://www.jqueryscript.net/demo/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel/
Fiddle: https://jsfiddle.net/tbeckenhauer/mueqr96t/9/
如果设置了 href 和下载属性就可以下载了
HTML
<a id="link" href="" download='blah.txt'>Download File!</a>
JavaScript
var excelData = 'data:text/plain;charset=utf-8,' + encodeURIComponent('your data goes here');
$('#link').click(function(event) {
$(event.target).attr('href', excelData);
});
我解决了。我只是创建了一个 html table 和 'xls formatted head Element' 可以被 [=12 真正读取=]Excel 但 xlsx 只是这种数据的一种不同类型的格式。它实际上是许多xml的包装。所以没有什么可以将 html table 导出为 xlsx。我使用“ALASQL”来做到这一点。效果棒极了。我向您推荐使用'ALASQL'来解决这个问题。
这是我的函数,可将我的 html table 转换为 excel 文件。问题是,我需要将 excel 文件保存为 .xlsx,但我做不到。有人帮忙吗?
function exceller() {
var uri = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;content-disposition:attachment;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><head><!--[if gte mso 9]><?xml version="1.0" encoding="UTF-8" standalone="yes"?><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]; }) }
$('#toExcel').html($('#pztable').html());
var toExcel = $('#toExcel').html();
var ctx = {
worksheet: name || '',
table: toExcel
};
var link = document.createElement("a");
link.download = "export.xlsx";
link.href = uri + base64(format(template, ctx))
link.click();
link.remove();
}
这是 jsFiddle:https://jsfiddle.net/jxz60kqj/
您是否尝试过使用第三方 jQuery 插件?我一直在使用 jquery.table2excel.js
并且效果很好。
您需要包含 JQuery 插件和可以找到的 Table2Excel 插件 here。
代码:
$("button").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Worksheet Name",
filename: "SomeFile" //do not include extension
});
});
此处演示:
http://www.jqueryscript.net/demo/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel/
Fiddle: https://jsfiddle.net/tbeckenhauer/mueqr96t/9/
如果设置了 href 和下载属性就可以下载了
HTML
<a id="link" href="" download='blah.txt'>Download File!</a>
JavaScript
var excelData = 'data:text/plain;charset=utf-8,' + encodeURIComponent('your data goes here');
$('#link').click(function(event) {
$(event.target).attr('href', excelData);
});
我解决了。我只是创建了一个 html table 和 'xls formatted head Element' 可以被 [=12 真正读取=]Excel 但 xlsx 只是这种数据的一种不同类型的格式。它实际上是许多xml的包装。所以没有什么可以将 html table 导出为 xlsx。我使用“ALASQL”来做到这一点。效果棒极了。我向您推荐使用'ALASQL'来解决这个问题。