Search code examples
javascriptactivexexport-to-excelexport-to-csv

Creating an xls or csv file from a Javascript variable


I have an app that uses Javascript to perform some calculations and then plot the data, but I'd like to add the option for the user to be able to actually download the data into a csv or xls file.

Is there a way in Javascript (or some other method) to have the user press a button, then it will prompt them for the name of the file to save it as, and it will then create a comma-delimited or excel spreadsheet?

Thanks!

EDIT:

Thanks for all the suggestions everyone. Wish I could mark you all as answers, but upboats will have to do for now


Solution

  • It's not hard to open a window and write the csv into it. But I don't know of any way for javascript to change the Content-Type: header. And without that it won't prompt to save or open.

    You'll need assistance from the server to do this. You can send the data to the server in a form variable and have the server send it right back with the correct header Content-type: text/csv you may also want the Content-Disposition: header to give your file a name.