8

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?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
user1877936
  • 351
  • 3
  • 7
  • 22

6 Answers6

10

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/

katmanco
  • 1,146
  • 12
  • 24
kvs
  • 466
  • 1
  • 10
  • 24
6

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

Erhan Gidici
  • 61
  • 1
  • 3
3

I had the same issue, and since the new format (maybe not supported by every browser) <a download=""></a> the following worked fine for me. This use directly HTML/Javascript without the PHP server part, because using a submit form is too heavy for big data tables.

  • changing <button> to <a>
  • no more window.open()
  • using the basic <a> behavior (so, no more e.preventDefault()) but changing href to data:blabla and adding download="filename" :
<div id="example" class="k-content">
    <a href="#" id="btnExport" >Export to csv!</a>
    <div id="grid"></div>
</div>
<script>
    $("#btnExport").click(function (e) {
        var result = "data:application/vnd.ms-excel,";
        this.href = result;
        this.download = "my-custom-filename.xls";
        return true;
    });
</script>
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Asenar
  • 6,732
  • 3
  • 36
  • 49
  • It works great, but for some reason changing `button` to `a` caused Windows to block the file (https://weblogs.asp.net/dixin/understanding-the-internet-file-blocking-and-unblocking). Any idea how to work around this issue? – HuBeZa Jan 22 '17 at 07:41
1

You can't do this with client-side JavaScript, you need to set the response header...

.NET

Response.AddHeader("Content-Disposition", "inline;filename=filename.xls")

Or PHP

$filename = 'somehting.xls';

header('Content-Disposition: attachment; filename="'.$filename.'"');
Red
  • 6,230
  • 12
  • 65
  • 112
1
Response.AddHeader "Content-Disposition", "attachment; filename=C:\YOURFILENAME.xls;"
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
0
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
a.href = data_type + ', ' + encodeURIComponent(tab_text);
//setting the file name
a.download = 'SupervisorReport.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
return (a);
NightOwl888
  • 55,572
  • 24
  • 139
  • 212