Is there a way to generate an .xslx file from javascript and allow the user to download it by javascript? This page is geared towards being run offline in html 5 with no internet connectivity.
-
Maybe not an answer, but you could clear the contents of the html and write out csv to screen for copying/pasting into Excel? – Mark Redman Apr 30 '10 at 06:08
5 Answers
You could generate a data URI, and let the user save the link. However, IE8 has very limited support for data URIs. There is a 32 KB limit, and it's not allowed to be used with a href
.
Also, you still need to find a actual XLSX JS library... But it is possible.

- 278,309
- 50
- 514
- 539
It has been done successfully by Ed Spencer. This project is using an EXT DataGrid as the source of the data, but I'm sure you could adapt it pretty easily.

- 19,907
- 21
- 66
- 79
-
This example is pretty good. It's free, works well, doesn't require Microsoft's 5Mb OpenXML libraries, but all it does is to write out an XML file but with the .xls extension. The result is that Excel will happily open it, but with the warning "*The file format and extension... don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?*" For our in-house websites, this error wouldn't be acceptable... ;-( – Mike Gledhill Feb 17 '16 at 08:30
**Yes You Can do it using javascript**
** call function expexcel('table_id','output_file_name');**
<script>
function expexcel(tableID, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
</script>

- 22
- 3
You can generate any Office document with OpenXML SDK for Javascript http://openxmlsdkjs.codeplex.com/
As for allowing the user to save a file from JS I recommend FileSaver.js https://github.com/eligrey/FileSaver.js/

- 16,464
- 4
- 33
- 50
As Javascript has no file I/O, it's not going to be easy for your users to download. However, this kind of work is perfectly suited for a simple PHP script, which could generate your XSLX and save to your server dynamically.

- 2,498
- 2
- 21
- 13