在网上找到了js导出为excel的方法,可就是找不到如何修改导出的excel标题的方式,找到了如下的网站: 参考国外的网站:http://stackoverflow.com/questions/17126453/html-table-to-excel-javascript.他是利用a标签,可以设置download属性,设置下载的文件标题,这里是a标签的详细解释http://www.runoob.com/tags/att-a-download.html:
二 英文的原文和大概翻译如下:
I'm trying to use this script to save a html table to an Excel file, and it works fine, however it doesn't come up in the proper name, but rather with a random string. And I can't see why .
I call it with:
<input type="button" οnclick="tableToExcel('tablename', 'name')" value="Export to Excel">
code
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) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
答案如下:
意思是:可以使用a标签的download属性来设置下载的文件标题:
You can use download attribute supported by modern browsera for a anchor element. First modify your HTML by adding an invisible anchor:
/ /这里是在你点击导出的按钮上方增加一个隐藏的a标签,只是为了更改标题内容
<a id="dlink" style="display:none;"></a>
<input type="button" οnclick="tableToExcel('tablename', 'name', 'myfile.xls')" value="Export to Excel">
Notice also that the call to function tableToExcel now has 3rd parameter - where you specify file name.
Now use this modified code of your original function:
//这里是将html内容转换成excel的方法
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) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename ;//这里是关键所在,当点击之后,设置a标签的属性,这样就可以更改标签的标题了
document.getElementById("dlink").click();
}
})()