Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Saving data to Excel files

 
Anant Kadiyala
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a web page (JSP) that renders some data in HTML tables. We would like to have an �export to Excel button� on the page. What is the best way to export HTML data from JSP into an Excel file?
I know that Excel can read HTML and CSV formats. But the requirement is to save the user the trouble of doing the import in Excel. When the user opens the .xls file, they should have everything readily formatted for them. The formatting itself is not very complex. It is a simple table. So, basic column headers and a title for the report would do.
Any ideas??!!
Thanks,
Anant
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out my earlier post on exporting to SYLK format a week or so ago.
http://www.coderanch.com/forums/
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We look at CSV files in Excel all the time. In fact, Excel assumes any file with a .csv extension to contain comma-separated values. It's easy to create one. The basic idea is that commas serve as delimiters and each row begins a new record.
In English, that means that if I want to save some address info into a CSV file, then in a text editor like Notepad, the data will look like this:
John Smith, 42 Main St, Houston, TX, 77005
Frank Jones, 19 Timberlake, Evanston, IL, 60201
Each field is separated by a comma, and each record begins on a new row/line. Excel will display each field in its own cell automatically. Now, what happens if a field contains a comma? How would you handle John Smith, Jr.? Simple, with double quotes:
"John Smith, Jr.", 42 Main St, Houston, TX 77005
Try to make the file in Notepad, save it with a .csv extension, and then double-click it and Excel will probably be chosen to open it. If not, then just open it in Excel and you'll see that the values don't all get lumped into the first cells.
As you can imagine, it's easy to write out to a text file in CSV format using basic Java IO stuff.
God bless,
Stephen
 
Anant Kadiyala
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the posts.
If I were to save the data as a .csv file, is there a way to specify simple Excel formatting (like displaying simple column header names, etc in Excel)?
It is a simple table, but the Excel sheet should not be mere dump of the data. The requirement is that it should be presented in a decent format to the user.
Thanks,
Anant
 
Brian Showalter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anant Kadiyala:
I have a web page (JSP) that renders some data in HTML tables. We would like to have an �export to Excel button� on the page. What is the best way to export HTML data from JSP into an Excel file?

You could write a servlet that uses the Jakarta POI library (http://jakarta.apache.org/poi/) to build the Excel spreadsheet. I am currently using POI to generate several Excel files, and it works very well.
Brian
 
Ben Dover
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Showalter:

You could write a servlet that uses the Jakarta POI library (http://jakarta.apache.org/poi/) to build the Excel spreadsheet. I am currently using POI to generate several Excel files, and it works very well.
Brian

What excactly does POI stand for?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic