File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes CSV file to open in Excel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "CSV file to open in Excel" Watch "CSV file to open in Excel" New topic

CSV file to open in Excel

Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
I am having trouble getting a CSV file to open up in Excel. I have a .jsp page that links to another JSP page that has the following:
<%@ page language="java" session="true" isErrorPage="false" %>
<title>Delivery Status</title>
<% response.setContentType("application/"); %>
<jsp:include page="./config/mydomain/reports/Plane_US1A.csv" />
It launches excel, but it always comes up blank. We are running on Weblogic 6.1 server on Unix and the .csv files are placed in a directory from another application. I've tried the content type as suggested by other people as "text/csv" or "text/comma-saparated-values", but they just prompt me with a dialog box to save the file.
Any suggestions would be greatly appreciated
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
Can you change the .csv file to be formatted like an HTML table? If you can, you will find that it works great without any changes, unless your users are using really *old* versions of Excel. Excel 97 and above understand data formatted as an HTML table. If you do this, Excel will even recognize the cell backgrounds, fonts, colors, embedded hyperlinks, etc.
If not, you need to get rid of the HTML tags. I haven't actually tested the following, but it should be close:
Two things to note. 1) The funny way scriptlet tags are opened (the <% is at the end of a line instead of the beginning) is important. If you don't do that, your downloaded CSV file will contain blank lines in the front, which will give blank rows in your excel spreadsheet. 2) The if( true) return at the end of the last scriptlet is also important, so that you don't get blank lines and trailing spaces at the end of the CSV file.
[ December 02, 2002: Message edited by: Michael Zalewski ]
I agree. Here's the link:
subject: CSV file to open in Excel
jQuery in Action, 3rd edition