This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

exporting to CSV

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
text/csv or text/plain.
 
bala kiran
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That did not work for me. Did you use it before? Any further help is appreciated.
 
Sheriff
Posts: 67602
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please get me sample code?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic