I want to export a table from HTML into an excel file by using this function
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,';
var 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>';
var base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
}
var 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 }
var url = uri + base64(format(template, ctx));
window.location.href = url;
}
})()
The default filename seems to be Download.xls, I can already set it to a different one by using this function which wrapps the download (I adjusted the first function, it returns the url now instead of opening it in the browser).
function download(table, name) {
var link = document.createElement('a');
link.download = "LoL.xls";
link.href = tableToExcel(table, name);
link.click();
}
However it always downloads immidiatly when calling the function (e.g. by clicking on a button). I'd like to click on the button - chose the filename AND more important the location - and then download the excel-file.
Is there a way to change the download location? Is there even a way to get a download prompt (name + location)?
regards