gpt4 book ai didi

Javascript/Jquery Excel 文件名

转载 作者:行者123 更新时间:2023-11-28 08:02:49 25 4
gpt4 key购买 nike

我引用下面的例子

http://jsfiddle.net/cmewv/537/

如果我点击“导出到 Excel”,它会将文件下载为 Excel,但文件名始终采用我的语言“Download.xls”

如何将特定的 Download.xls 更改为“MyFile.xls”

Javascript代码:

    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"><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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></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));
}
})()

我更改了工作表和名称,但它对我不起作用。如何更改下载的文件名?

最佳答案

只需将链接放置在页面的任意位置即可

anchor 标记 id="exprotData" style="display:none;" anchor 标记关闭

并使用此方法更改您的导出函数

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, filename) {
table = tableId;
name = "Report";
filename = "Report.xls";
if (table != "") {
if (!table.nodeType) table = document.getElementById(table)
$(table).find(':not(:visible)').remove();
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
document.getElementById("exprotData").href = uri + base64(format(template, ctx));
document.getElementById("exprotData").download = filename;
document.getElementById("exprotData").click();
}
else {
alert('No Record Found.');
}
}
tableId = '';
})()

您可以根据需要全局传递这三个参数:table、name、filename。

关于Javascript/Jquery Excel 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com