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.
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
Exporting to Excel file format with JQuery is impossible.
You can try with Java. There are a lot of libraries to do that.
I have been using the jQuery plugin table2excel. It works very well and no serverside coding is needed.
Using it is easy. Simply include jQuery
Now include the table2excel script (Remember to change the src destination to match yours)
Now simply call the script on the table you want exportet.
It's also easy to attach to a button like so:
All examples are taken directly from the authors github page and from jqueryscript.net
I can suggest you to try http://code.google.com/p/gwt-table-to-excel/, at least the server part.
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
Edit (February 29, 2016): You can use the table2csv implementation above in conjunction with FileSaver.js (which is a wrapper for the HTML5 W3C
saveAs()
spec).The usage will end up looking something like:
I also spend lot of time to convert html to excel after lot of R & D i found following easiest way.
create hidden field and in that pass your html data to your servlet or controller for e.g
on your button of href click call following function and pass your html data using in document.formexcel.exceldata.value and your servlet or controller in document.formstyle.action
Now in your controller or servlet write following code