aspose file tools*
The moose likes Servlets and the fly likes Save excel file by hitting a url Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Save excel file by hitting a url" Watch "Save excel file by hitting a url" New topic
Author

Save excel file by hitting a url

berender mavi
Ranch Hand

Joined: Feb 18, 2011
Posts: 31

Hello all

I have a jsp page which is showing data in that page from DB.there is also a facility to show data in excel file.So there is a link to import the data in excel file.

I have noted down that url by pointing the cursor to that link that is also in the attached jpg file.Now i am hitting that url using url class but it will showing some jsp code,i think of that page in the ouput of java file.

But i want to save that data in excel file as it is occurring from GUI by clicking that icon.How can i save that file on the system by hitting that url.

SEE THE URL IN THE STATUS BAR OF ATTACHED JPG



[Thumbnail for Screenshot-1.png]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

You'll need to use a proper servlet for generating the Excel file, not a JSP file. The latter are not as good for binary data (which Excel files are).

I'll move this thread to our Servlets forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I assume you want to just click on the link and have the data showing on your browser page to be saved in an Excel file. Unfortunately, it's not that simple. You must do the following:

1. When the user clicks the link, you need to send a request to the servlet.
2. The servlet gets the data from the database.
3. The servlet converts the data into a format that Excel can understand. This can be for example, true Excel format, comma-delimited or tabbed delimited, data. (For true Excel format see the Apache POI API.)
4. The servlet sends the data back to the browser not as HTML but as the Excel-formatted data. This is done by setting the Content-Type (and other fields) in the HTTP header of the HttpServletResponse
5. The browser will then receive the data and, depending on its settings, will display the data in Excel or offer to save the file, or something else.

I hope there are enough buzzwords in the above to start your google investigation.
berender mavi
Ranch Hand

Joined: Feb 18, 2011
Posts: 31
buddy actually my front end is doing the whole work to export data to excel file.but i want a write a java program that runs in background and automatically click on that link and save the excel.

there is no problem in my jsp page but i want to do it also from my java code so that automatically at a particular time excel file is downloaded on the system so no need to click on the link manually.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Are you asking how to generate the Excel file, or how to make the browser save the file without user interaction?

If you're asking for the later, for security reasons most browsers won't allow this. At best, you could have a "refresh" meta tag point to the generated Excel file. The user would likely be prompted to save the Excel file after the refresh timeout (unless they've sent their browser to automatically save that file type).


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Krysthiam Diaz
Greenhorn

Joined: Aug 16, 2011
Posts: 1
I assume you want to just click on the link and have the data showing on your browser page to be saved in an Excel file. Unfortunately, it's not that simple. You must do the following:

1. When the user clicks the link, you need to send a request to the servlet.
2. The servlet gets the data from the database.
3. The servlet converts the data into a format that Excel can understand. This can be for example, true Excel format, comma-delimited or tabbed delimited, data. (For true Excel format see the Apache POI API.)
4. The servlet sends the data back to the browser not as HTML but as the Excel-formatted data. This is done by setting the Content-Type (and other fields) in the HTTP header of the HttpServletResponse
5. The browser will then receive the data and, depending on its settings, will display the data in Excel or offer to save the file, or something else.

I hope there are enough buzzwords in the above to start your google investigation.


Tom Reilly, what about if I'm using spring MVC? Is there any problem related to the spring context if I use a servlet to create my Excel file?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Save excel file by hitting a url
 
Similar Threads
How to Map vectors data in java into a Microsoft Excel Sheet
Render Charts in Excel to PDF & Advanced Conditional Formatting
excel page not opening in a browser
JSP save as Excel File after displaying data
how to download or view the "Excel FILE "i n IE browser