• Post Reply Bookmark Topic Watch Topic
  • New Topic

Download data to file

 
Dilip kumar
Ranch Hand
Posts: 360
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All -

In My J2EE application I need to provide "download recordset into local drive" option along with displaying data on JSP page. I'm using following code to write records into file. Right now I have hardcode the values but will read from DB. Is this the proper way ? Are there any better options ?

response.setContentType("application/csv");
response.setHeader("Content-Disposition","attachment; filename=gdagams-mis.csv");
PrintWriter out = response.getWriter();
String sHeading1 = "Date";
String sHeading2 = "Amount";
String sHeading3 = "Department";
String sValue1 = "20070421";
String sValue2 = "13.22";
String sValue3 = "Tracking";
out.println(sHeading1 + "," + sHeading2 + "," + sHeading3);
out.println(sValue1 + "," + sValue2 + "," + sValue3);
out.flush();
out.close();

Also the contents of the file is coming as

Date,Amount,Department
20070421,13.22,Tracking

How do it change it to below format ?

"Date","Amount","Department"
"20070421","13.22","Tracking"

Really appreciate any suggestions.

Thanks much
[ May 11, 2007: Message edited by: Dilip kumar ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks fine to me.

As to adding the double quotes, you could create a method that adds them at the beginning of a line, the end of a lne, and before and after each comma, and then prints the whole line to the output. This only works if the data contains no commas, of course. And if it contains no commas, then you don't need the double quotes...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're doing this in a servlet, not a JSP, right?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!