I have been trying to address a download CSV issue and one of the options I am trying is to write the results of a user request in comma-separated format to a static file and then redirect the user to a url pointing to the dynamically created file.
PrintWriter csvWriter = new PrintWriter(new BufferedWriter(new FileWriter(csvFileFullName))); csvWriter.print(ostr); csvWriter.close();
I enclose data elements in quotes since they could contain commas. The download shows up fine in IE. However when Firefox is used, it is trying to open *.csv.xls and the data comes up in a single column in Excel displaying the quotes in them.
I have set the mime-type in web.xml as follows --- <mime-mapping> <extension>csv</extension> <mime-type>application/vnd.ms-excel</mime-type> </mime-mapping>
Can anyone suggest how I can get around this problem.
In your web.xml file, you have assigned to the .csv extension the MIME type that everybody else in the world assigns to the .xls extension. So, you are telling the browsers that what you are downloading is an Excel worksheet. But when the browser takes that advice seriously, you have a problem. So: don't tell the browser that the data is an Excel worksheet when it isn't one.
Hmm. That would explain why Firefox appends a .xls to the file name and Excel is not translating the comma-separated values anymore. I looked for a mime-type for .csv that will allow us to associate Ms-Excel when the browser downloads it. I couldn't find one. If I remove the mime-type mapping, even IE fails now. The file is being opened inline in the browsers as-is (commas, quotes are being displayed in the browser window ).
I guess I could change the format to tab-delimited and name the file .xls and use the mime-type.
Would that work ? Is that a good practice ?
Thanks a lot for your comments/suggestions, they've been very helpful. I am still rying to see if my original code will work for slow n/w users. So if you have any thoughts on what i am doing wrong in my code, I'd really appreciate it. [ July 23, 2007: Message edited by: Las Avasa ]