Javascript / Jquery Excell文件名
我参考下面的例子
http://jsfiddle.net/cmewv/537/
如果我点击“导出为Excell”,它会以excell的forms下载文件,但是文件名称总是以我的语言“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 tag id="exprotData"
style="display:none;"
锚标签closures
并使用此方法更改您的导出function
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 = ''; })()
您可以根据您的需要全局传递这三个参数:表,名称,文件名。
下面的代码工作正常,请尝试。
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 = document.getElementById(table); 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 = ''; })()