0

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

John Smith
  • 615
  • 4
  • 15
  • 1
    Possible duplicate: http://stackoverflow.com/questions/6677281/user-specified-download-location – TomSlick May 11 '15 at 13:28
  • I'm not sure if it's the same since I only want to export data, I can't find a place to insert the header in my code. You have a suggestion? – John Smith May 11 '15 at 13:36
  • How about this one: http://stackoverflow.com/questions/364946/how-to-make-pdf-file-downloadable-in-html-link Check the second answer from the top. – TomSlick May 11 '15 at 13:50
  • It seems to be a solution for naming the downloaded file but not for forcing a 'save as', from what I've found now I can say that it is not possible to force the 'save as', since it's a browser-option (your first link kinda solves it), thanks for your effort :) – John Smith May 12 '15 at 05:52

1 Answers1

0

As pointed out in this question already: User specified download location

it is not possible to force a 'save as' dialog, the user has to specify in his browser-options, if he wants to chose the download location or if the default one should be used

Community
  • 1
  • 1
John Smith
  • 615
  • 4
  • 15