Skip to main content

JSP - CSV to EXCEL - Junior :-)

1 reply [Last post]
boy3696
Offline
Joined: 2009-08-10

Hello All:
Just a newbie here, so trying a few things but need your help.

I've put in my web.xml the following:

csv
text/csv

csv
text/comma-separated-values

Here is my jsp I created that won't work:
<%-- Set the content type header with the JSP directive --%>
<%@ page contentType="text/csv" %>

<%-- Set the content disposition header --%>
<% response.setHeader("Content-Disposition", "application; filename=\"mult-table2.csv\""); %>
Daniel,Dan,John,Dave,Don,Mac,Albert,Viren
47,24,36,38,40,36,49,28
N,,Y,N,,,Y,N

When I run this, I get the File Download Pop Up Window and right away error occurs saying:
Internet Explorer cannot download DansExample3.jsp from localhost.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found.
Please try again later.

I see that it's trying to open the filename with the .jsp on the end, but I think it should say csv?

This works:
<%-- Set the content type header with the JSP directive --%>
<%@ page contentType="application/vnd.ms-excel;
charset=UTF-8" %>

<%-- Set the content disposition header --%>
<% response.setHeader("Content-Disposition", "inline; filename=\"mult-table2.xls\""); %>

<% for(int i = 1; i <= 12; i++){ %>

<% for(int j = 1; j <= 12; j++){ %>

<%= i * j %>

<% } %>

<% } %>

And this works:
<%@ page contentType="application/dummy.xls" %>
<% response.setHeader("Content-Disposition", "inline; filename=\"mult-table.xls\""); %>

2000 3000 4000 5000 6000 7000
12.4 13.4 14.5 15.6 16.7 17.8

I just can't get a csv file to load in xcel.
Any help would most be appreciated.

Thanks in advance. :)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
boy3696
Offline
Joined: 2009-08-10

I knew this would happen. :)
I just figured it out.

Here's the code that now works (I just wanted to post incase someone else as Junior as I wanted to know):
<%
response.reset();
response.setHeader("Content-type","application/xls");
response.setHeader("Content-disposition","inline; filename=nombre.csv");
%>Dan,John,Dave
10,20,30
Y,N,Y