Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

exporting to CSV

 
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set the content type, set the content disposition and write your text.
 
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 VI Editor Redhat
 
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.
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 VI Editor Redhat
  • 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?
 
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 VI Editor Redhat
  • 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.


 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic