I am trying to generate an excel file in xlsx format and here is my code

var element = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,' + encodeURIComponent(doc);
let downloadElement = document.createElement('a');
downloadElement.href = element;
downloadElement.target = '_self';
downloadElement.download = 'Data.xlsx';

Variable doc contains the html table as string. But when I open the file I see an error in ms excel.

Excel cannot open the file 'Data.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

I tried downloading it in xls format using this article, it worked but still get a warning message every time I open the downloaded file.


1 Answer 1


After doing lots of research on this, I couldn't find a solution, so I used this external JS library for generating XLSX files. This library is very easy to use and has lots of features for formatting cells, rows, and columns.

You can also find it's implementation here. If anybody knows any alternative solution please mention it.

  • Library file present over here is giving undefined when script is getting loaded Commented May 22 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .