This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exporting to CSV

 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, how can i export the data into CSV format (not Excel) from jsp. how can i do that?

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set the content type, set the content disposition and write your text.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did not work for me. Did you use it before? Any further help is appreciated.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please get me sample code?
 
Isa Andres
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic