aspose file tools*
The moose likes JSP and the fly likes exporting to CSV Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "exporting to CSV" Watch "exporting to CSV" New topic
Author

exporting to CSV

bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
Hi, how can i export the data into CSV format (not Excel) from jsp. how can i do that?

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Set the content type, set the content disposition and write your text.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Are you exporting it to the client, or are exporting it into a file on the server?

Regardless you can generate the csv by adding a new line for every row, and each column should be seperated with comma(,)

So, a csv file will look like this



You can look at any csv file by opening it in notepad

If you are exporting to client, you can set the content type and write the above text to the response(like Ben said) If you have to save it on the server, create an FileOutputStream and write the text out
bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
what should i set the content type to, for CSV exporting? for Excel, i do set it it: application/vnd.ms-excel, what should it be for CSV?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

text/csv or text/plain.
bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
That did not work for me. Did you use it before? Any further help is appreciated.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

It's practically imposible to formulate a response when all you tell us is "That did not work".

What did it do? What are you expecting it to do?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
Well, what i meant was: when i was using mimeypte as: application/vnd.ms-excel, it is prompting me to save in XLS format, but when i change the mimetype to: text/csv or text/plain, I assume it will prompt me to save it csv format (filename.csv), but it was displaying the results on my web page. How can I save the file in csv format, not in excel.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Set the mime type to something that the browser doesn't have an association for, like: "application/octet-stream"
Then set the content disposition tag with "attached" and your filename.
[ May 17, 2005: Message edited by: Ben Souther ]
bala kiran
Ranch Hand

Joined: Jun 11, 2004
Posts: 34
can you please get me sample code?
Isa Andres
Greenhorn

Joined: May 30, 2005
Posts: 1
Hi, I have a JSP that exports data to a CSV file. However, when I get the downloaded CSV file, there are trailing white spaces above the text. How can I get rid of those white spaces? I would appreaciate your help!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by bala kiran:
can you please get me sample code?


I just tried with this code using several different values for the content-type header on Fire Fox (FF).

It looks like FF first reads the Content-Type and prompts me to open with the application that is associated with that type.
If if there is no registered app for that type, it then reads the extension set in the Content-Disposition tag and figures out that it's CSV file.
It prompts me to save or open with Open Office which is what I've got registered for CSVs.
I haven't tried with MSIE or any box that has MS-Office.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exporting to CSV