Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the name of file while exporting data to Excel?

How do I change the name of file while exporting data to Excel?

<div id="example" class="k-content">
    <button type="button"id="btnExport">Export to csv!</button>
    <div id="grid"></div>
</div>
<script>
$("#btnExport").click(function (e) {
    var result = "data:application/vnd.ms-excel,";

    window.open(result);

    e.preventDefault();
});
</script>

When I click the export button I am getting as download.xls. Is it possible to set the file name as data.xls? Can any one explain me where I need to configure that?

like image 968
user1877936 Avatar asked Mar 12 '13 05:03

user1877936


2 Answers

here is an example which demonstrates Export HTML Table to Excel With Custom File Name: http://www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/

like image 63
kvs Avatar answered Nov 15 '22 10:11

kvs


Not only for excel, in addition, for many kind of format can useable.

 var element = document.createElement('a');
            element.setAttribute('href', 'data:application/vnd.ms-excel,' + encodeURIComponent(htmlTable));
            element.setAttribute('download', fileName);
            element.style.display = 'none';
            document.body.appendChild(element);
            element.click();
            document.body.removeChild(element);

https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server

like image 6
Erhan Gidici Avatar answered Nov 15 '22 10:11

Erhan Gidici