Search Support

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Learn More

export excel with js with wrong file extension ".part"

  • 6 replies
  • 6 have this problem
  • 105 views
  • Last reply by mileskunta

more options

I used code : document.location = 'data:application/vnd.ms-excel;base64,'+Base64.encode('the xml to generate the excel file');

to export excel from grid,

name of the file downloaded is xxxx.xls.part

if I change the extension to .xls, the file can be opened correctly.

what should I do to get the right file extension when download?

I used code : document.location = 'data:application/vnd.ms-excel;base64,'+Base64.encode('the xml to generate the excel file'); to export excel from grid, name of the file downloaded is xxxx.xls.part if I change the extension to .xls, the file can be opened correctly. what should I do to get the right file extension when download?

Modified by mileskunta

Chosen solution

".part" will be resolved once use FF20+.

You can set your file name as below document.getElementById("Your div id").download = 'NameYouWant.xls';

Read this answer in context 👍 1

All Replies (6)

more options

In which version have you checked.Try mozilla 20 and above. I faced the same issue it got solved when i used Mozilla 20 version

more options

Suren2327,

we are using FF17, there are two problems, one is it has the extension ".part", another problem is that we cannot set the file's name, it will be an random string by default.

more options

Chosen Solution

".part" will be resolved once use FF20+.

You can set your file name as below document.getElementById("Your div id").download = 'NameYouWant.xls';

more options

Hi Suren2327,

have tried ,the extension is correct now, but about the file name, I am not downloading from a dom element, I actually created a String of hrml and download this html as an xml.

more options

Great Suren2327,

I have tried your way, it works well, Thank you for giving me such a good solution.

more options

here reference the solution in stack over flow: the url is:http://stackoverflow.com/questions/17126453/html-table-to-excel-javascript see the first answer.

You can use download attribute supported by modern browsera for a anchor element. First modify your HTML by adding an invisible anchor:

<a id="dlink" style="display:none;"></a>

<input type="button" onclick="tableToExcel('tablename', 'name', 'myfile.xls')" value="Export to Excel">

Notice also that the call to function tableToExcel now has 3rd parameter - where you specify file name.

Now use this modified code of your original function:


var tableToExcel = (function () {

       var uri = 'data:application/vnd.ms-excel;base64,'
       , template = '{table}
' , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } return function (table, name, filename) { if (!table.nodeType) table = document.getElementById(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
           document.getElementById("dlink").href = uri + base64(format(template, ctx));
           document.getElementById("dlink").download = filename;
           document.getElementById("dlink").click();
       }
   })()


Notice last 3 code lines: Instead of assigning URL to window - they assign it to the new anchor, then use new download attribute to force download as the given file name and then simple call click() method of the anchor.

Give it a try.