Home » excel » excel – Build XLSX File With JavaScript

excel – Build XLSX File With JavaScript

Posted by: admin April 23, 2020 Leave a comment


I’m trying to build out an XLSX file using JavaScript using some database query results. I have to use JavaScript as the server back end only provides a JavaScript interface to work with (and it doesn’t like jQuery). Now, I seem to have uncovered something to actually create the base64 encoded data to write to the file (the server interface provides an API for creating files using base64 encoded strings and defining a file type). XLSX.js looks like it will work well for that purpose, since it looks like it can read in some form of JS object and convert it to a base64 string. However, I am rather unclear about how to generate the worksheet XML data in the first place, or how I would want to construct a representative JS object. The only useful information I can seem to find doesn’t really give me a good idea how to build it out, if I can even locate information on structuring. Most I find is about reading the files, not creating them. Also, I thought I found some simple to implement methods, but then they devolve into a mess of libraries and I get lost trying to understand which files are actually necessary and how to correctly include them in the work.

How to&Answers:

You can export data from js to XLS, XLSX and CSV formats with Alasql library.

For example we want to write a data which we saved in test variable in an XLSX file through click a button. To do this just use following steps:

1- Include following files in your page:

 <script src="http://alasql.org/console/alasql.min.js"></script> 
 <script src="http://alasql.org/console/xlsx.core.min.js"></script> 

2- So Then make a function in controller to save the test array as a xlsx file:

function saveAsXlsx(){
   alasql('SELECT * INTO XLSX("output.xlsx",{headers:true}) FROM ?',[test]);

So We save the data which we had in variable test into a file which we named it output.xlsx here.

3- The last part is the easiest part. Run the function on click on a button:

 <button onclick="saveAsXlsx()" >Save as XLSX</button>


I’ve abandoned trying to create an XLSX document in the environment I’m working in. I’ve been provided with a way to instead create an XLS file using the XML formatting for Excel 2003 that seems to work except for a warning when trying to open files generated in this fashion stating that the file is in a different format than what is specified by the extension. Other than that warning, I have been able to create workable files that open in Excel and behave pretty well.

I guess maybe I’ll have to leave generating XLSX files for any future .NET projects I may have. This kind of sucks as a resolution, but thank you everyone for your responses.


Excel supports two forms of XML documents as input. One is a ZIP archive which contains various XML documents and other data. This is the standard XLSX file format.

The second form is just the sheet data itself as an XML document. So all you need is to create the XML as string. Wikipedia has an example for the Excel XML format. You can find links to the reference documentation under “External links

If you create a file in this format and save it as .xslx, then Excel should be able to open it. If not, then try the .xsl extension. If you have trouble formatting something properly, do it in Excel, save and look at the resulting file.

I’m not sure XLSX.js will be much help since it’s a browser / client framework that depends on jQuery and jszip and other things. The “base64 and back” option refers to adding an Excel sheet to a HTML document as base64 string. The framework can then open this virtual file and examine it. I think it’s not meant to generate Excel files for download.