0
我有一個Angular2應用程序,現在有人問我實現一個基本 表格Excel導出。Angular2 - 導出表到HTML Excel和給文件名
我正在使用的函數有效,但我不知道如何設置生成的excel的文件名。
這是功能:
tableToExcel(e,table, name) {
console.log(e);
let 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]-->
<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>
</head>
<body>
<table>{table}</table>
</body>
</html>',
base64 = function (s) {
return window.btoa(decodeURI(decodeURIComponent(s)))
},
format = function (s, c) {
return s.replace(/{(\w+)}/g,
function (m, p) { return c[p];
})
}
if (!table.nodeType)
table = document.getElementById(table)
var ctx = {
worksheet: name || 'Worksheet', table: table.innerHTML
}
//window.location.href = uri + base64(format(template, ctx))
console.log(uri + base64(format(template, ctx)));
window.open(uri + base64(format(template, ctx)), '_blank', 'top=0,left=0,height=100%,width=auto');
return false;
}
,這是按鈕:
<a type="button" href="#" (click)="tableToExcel($event,'testTable', 'W3C Example Table')" download="Schede.xlsx" class="btn btn-warning"> Excel </a>
在這一刻我可以下載文件Excel文件,但文件名是完全隨機的 。
謝謝支持
https://stackoverflow.com/questions/283956/is-there-any-way-to-specify-a-suggested-filename-給你想要的文件名when-using-data-uri您可能不得不提供uri作爲href和文件名作爲'download'參數值 – Skeptor
在我的情況下,您的鏈接上的建議不適用...謝謝 – DarioN1