views:

140

answers:

6

Hello all,

I've a HTML table on my JSP page, that I want to be exported to Excel on a button click.
What would be the best way of going about this ?
(For ex., how would I do this using may be a jQuery function.)

Any code examples for demo. purposes should be great.

Thanks,
Pritish.

+1  A: 

Exporting to Excel file format with JQuery is impossible.

You can try with Java. There are a lot of libraries to do that.

Pablo Santa Cruz
A: 

You would have to create something on the server-side (like a servlet) to read the html and create the excel file and serve it back to the user.

You could use this library to help you do the transformation.

KLee1
A: 

I would recommend Apache POI, we've been using it for years, never had any problems.

Alot of examples online to get a good start, and the documentation on the site is also good: http://poi.apache.org/spreadsheet/quick-guide.html

Tommy
+1  A: 

Rather export to CSV format. It's supported by Excel as well. Exporting to a fullworthy XLS(X) format using for example Apache POI HSSF or JExcepAPI is slow and memory hogging.

Exporting to CSV is relatively simple. You can find a complete code example in this answer.


As to exporting to files using JavaScript, this is not possible without interaction of Flash or the server side. In Flash there's as far only the Downloadify library which can export to simple txt files. Further, ExtJs seems to have a CSV export library, but I can't find any feasible demo page.

BalusC
A: 

Excel can load CSV (comma-separated value) files, which are basically just files with everything that would go into separate Excel cells separated by comma.

I don't know enough about how jQuery can handle pushing information into a file that you would download, but it seems a jQuery library has been written that at least transforms html tables to CSV format, and it is here: http://www.kunalbabre.com/projects/table2CSV.php

dmott
This doesn't create a CSV file for download. This only displays the generated CSV content in a textarea in a popup which the enduser has to copypaste himself. This doesn't really suit the requirements I suppose ;)
BalusC
+2  A: 

you can parse the table using library like http://jsoup.org/

after you get the data , you can store it in Excel-compatible format ( CSV ) , or using java excel library for that like POI, or using JDBC to write data into excel sheet, see this example: http://stackoverflow.com/questions/2609301/password-protected-excel-file/2609535#2609535

Wajdy Essam