views:

1929

answers:

5

Is there any way to generate Excel/CSV through Javascript? (It should be browser compaatible too)

+1  A: 

You can generate whatever you like, but the question is - how are you going to present it to the user? If your JS runs in the browser, you won't be able to make a save-as dialog box or even open it in the same window. Not unless you use some ActiveX controls, but that's an IE-only solution which will also give the user a few confirmation prompts before it starts to work.

Vilx-
No longer entirely true: There is Downloadify now. http://www.downloadify.info it requires Flash.
Pekka
@Pekka - well, technically I guess this would fall in the "ActiveX" category, although it's more platform independant so it's not entirely true. Still - you can't do it with Javascript alone.
Vilx-
+3  A: 

Create an AJAX postback method which writes a CSV file to your webserver and returns the url.. Set a hidden IFrame in the browser to the location of the CSV file on the server.

Your user will then be presented with the CSV download link.

Kinda silly though - passing data back to the server just so that it can serve it right back to the user. Also, the generated XLS might be rather large. (perhaps that's why the author wants to generate it client-side?) Maybe then it's better to generate the XLS server-side too?
Vilx-
A: 

It seems to me to be much easier to do on the server side. But I guess you have your reasons. Also, as gmc notes, drop Excel, and generate CSV-files instead. This has the added bonus of being useful for those of your users that do not have Excel installed.

rebra
A: 

I recommend you to generate an open format XML Excel file, is much more flexible than CSV.
Read http://stackoverflow.com/questions/150339/generating-an-excel-file-in-aspnet for more info

Eduardo Molteni
A: 

I see no difficulty in generating CSV data, but as pointed out above, what is the point? Will you fill up a textarea?

If so, it is very easy, so you should be more precise: where are you stuck, what kind of data you handle, where will it come from?

PhiLho