Build XLSX File With JavaScript

15,202

Solution 1

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>

Solution 2

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.

Share:
15,202
Michael McCauley
Author by

Michael McCauley

Updated on June 04, 2022

Comments

  • Michael McCauley
    Michael McCauley almost 2 years

    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.

  • Michael McCauley
    Michael McCauley over 10 years
    Nothing is actually occurring on the client side at all. This is all server side, and the server runs its own JavaScript implementation with a series of available APIs, one of which includes writing a file built with a base64 encoded string back to the server's file structure. The end user will ultimately be provided with a link to download the file, also provided by the available APIs. The issue I'm having is trying to make sure that I can actually structure out the data correctly, and take full advantage of any available open libraries, along with their dependent libraries.
  • Aaron Digulla
    Aaron Digulla over 10 years
    1. What kind of server do you use? 2. Why do you use BASE64? On the server, just use binary files.
  • Michael McCauley
    Michael McCauley over 10 years
    It's the NetSuite accounting system, and all the customization code has to be done with JavaScript. The ability to create a file in the system that isn't a text file requires base64 encoded file contents.
  • Aaron Digulla
    Aaron Digulla over 10 years
    Okay, I start to understand what you want. I've edited my answer.
  • Ifedi Okonkwo
    Ifedi Okonkwo almost 5 years
    Sad to say it remains a headache for me today, when trying to create XLSX using javascript object array, or JSON. At best, I could only arrive at XLS.