Learn how to a build a cloud-first strategyRegister Now


Exporting results into excel using JSP

Posted on 2004-11-03
Medium Priority
Last Modified: 2012-05-07
currently, the webpage returns all the results against my SQL query into a single page.Now I would like to export/download  these results into an excel sheet.Can you please give me some code reference of how to do it in JSP.
Question by:kalyanm
  • 4
  • 3

Expert Comment

ID: 12490771

Expert Comment

ID: 12490778

Use POI.

OS, available at http://jakarta.apache.org/poi/.


Expert Comment

ID: 12490800
Specifically, use the HSSF portion of POI.  Its a java library for reading/writing MS Excel files.


If I gave you a sample it would just be to copy code from:

You would probably want to create a wrapper class that takes your jdbc record set
and builds the excel file.  Make sure you can pass WHERE you want the file, and
make sure that whereever you create the file you have mapped within your webserver
(tomcat/apache most likely?).  You may also want to setup some sort of daemon to
clean out the cache directory, since you actually need to generate the file to disk and
then redirect the webbrowser to that disk.

If you are doing CHARTS, i would suggest creating the chart in excel before hand
and creating a second worksheet you can fill out from java to populate the chart.

I hope this is enough to get you started, if you have specific questions I will do my
best to answer them.  

- sbrown2k
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 12491538
Thanks very much for solution.unfortunately it look bit complicated from my point of view.I will explain you the scenario.perhaps you can help me better!!

I have a page which just fetches results from database and displays them in a JSP page.Now I would like to give the users and option to view/download these results into an excel sheet.Fo this I would like to provide a link called 'download to excel' in the same page clicking on which we should get an option to either save or view these results in excel.Can you please let me know if we can write a code within JSP page (as this is the requirement).


Expert Comment

ID: 12495073
Well, I dont know of any way to create an in-memory copy of an excel file and stream it to your client easily.
An alternate solution would be to create a comma delimited file (.csv) and send that.  It can excel can open it and display it as a worksheet. Given that its raw text it should be easy to create from jsp.  Creating an excel file you need to use a library as its a binary format that is too complicated to just write from jsp (POI is the "right" solution, IMHO for creating an excel file, but inorder to get it to the client you have to cache it to disk). The .csv is easier.

- sbrown2k

Author Comment

ID: 12495357
Thanks for all your efforts but I was looking at some sample code which can help me download the data displayed into an excel sheet.Can you please let me kow how to download  it into a csv(code prefereable)
 .I have tried contentType="application/vnd.ms-excel"  but it doesn't work.

Accepted Solution

lilian-arnaud earned 2000 total points
ID: 12499096
Excel converts automaticly html pages into Excel documents.

You just need to change mime type of your response :

<%@ page language="java"

or you can try to set directly in your JSP (warning, you cannot set the contentType in page directive and in your code at the same time) :


But it's better to set the content type in your servlet (or Struts action) before the display of your JSP.


Author Comment

ID: 12524168
Hi All
Thanx for your suggestions.i could get the answer after some research!!...
I would suggest this can be query can be closed now...
Thanks once again

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Screencast - Getting to Know the Pipeline

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question