Josiah
Josiah

Reputation: 654

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

Upvotes: 3

Views: 5120

Answers (6)

alexspeller
alexspeller

Reputation: 788

I think that it would be possible to do this (up to a certain size limit) with data URIs

Upvotes: 0

taxilian
taxilian

Reputation: 14324

You could certainly write a browser plugin (ActiveX control on IE, NPAPI on others) with FireBreath that would do this; you'd have to write it in C++. Honestly, I agree with others in suggesting that you do this server-side instead, but you can do it with a plugin and it wouldn't be too difficult.

Upvotes: 1

David Laberge
David Laberge

Reputation: 16061

If you want to do it in a browser website style it might be hard. But Javascript is a good language to do this, but you will need to use .hta instead of a normal .html. Creating an .hta creates a stand alone application just like a normal .exe.

Here is what you want to look for ActiveXObject("Excel.Application")

In order to transform a html into an hta, here is the tag

<HTA:APPLICATION
  id="SomeId"
  border="thin"
  borderStyle="normal"
  caption="yes"
  maximizeButton="yes"
  minimizeButton="yes"
  showInTaskbar="yes"
  windowState="yes"
  innerBorder="yes"
  navigable="yes"
  scroll="auto"
  scrollFlat="yes"
  singleinstance="yes"
/>

For futher reading on hta and the excel active X

Upvotes: 1

Michael M.
Michael M.

Reputation: 467

No, you can't create and/or save a file directly from JavaScript. On some browsers/platforms (IE/Windows), you could create and write to a file via ActiveX object:

function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\temp\\Test.txt", true);
s.WriteLine('Hello');
s.Close();
}

Another solution is to use client-side JavaScript (inside a browser) to output CSV data into a separate window (or pop-up) and have a user to copy/paste it into Excel.

Upvotes: 1

gilly3
gilly3

Reputation: 91497

Yes, but you'll need to use server-side code as well. Use JavaScript to construct a link to a page that streams the csv data back as an attachment. The server output should contain a content-disposition header of attachment; filename="fileName.csv".

Upvotes: 1

Ariel
Ariel

Reputation: 26753

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.

Upvotes: 4

Related Questions