?
Solved

How to export a jsp file to CSV?

Posted on 2004-03-31
4
Medium Priority
?
1,522 Views
Last Modified: 2010-04-01
I use
    response.setContentType("application/vnd.ms-excel");
    response.setHeader("Content-disposition", "attachment; filename=report.csv");
It will open a Excel file with all HTML code of the JSP file: <table><tr><td>.......

I do not intend to use
    response.setHeader("Content-Disposition","attachment; filename=report.xls" );

How to solve this problem?

Thanks in advance.
thungrac01
0
Comment
Question by:thungrac02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:searlas
ID: 10722502
A CSV file should be Comma Separated Values.

Why are you outputting <table....> to your CSV file?

It should be:
some,header,names
bag of flour,52.19,delivered
two kg sugar,12.29,awaiting delivery

etc...
0
 

Author Comment

by:thungrac02
ID: 10729155
My code is below: CSV.jsp

<%
      response.setContentType("application/vnd.ms-excel");
      response.setHeader("Content-disposition", "attachment; filename=report.csv");
   
      for (int j=0; j<9; j++){
            StringBuffer line = new StringBuffer();
          for(int i=0;i<=17;i++){
                line.append("\"").append(i + j).append("\",");      
            }
%>
            <%=line%>
<%      
      }
%>

when I request this file, it popup 2 box require me to open CSV.jsp and then report.csv. Can we made the file popup 1 time for report.csv?

when I open the report.csv, it has a unexpexted column A containing "0", "1", ... I think it mean the lines number. How to delete this column?

Thanks in advance.
0
 

Author Comment

by:thungrac02
ID: 10729309
My code expected covert to CSV is below, how to convert it, I don't know how to deal with the <logic:iterate> tag. Could any one help me?

        <logic:iterate id="record" name="reportC11Form" property ="query.results">
            <tr>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(0))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(1))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(2))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(3))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(4))%>" /></td>            </tr>
        </logic:iterate>

Thanks in advance!
0
 
LVL 7

Accepted Solution

by:
searlas earned 200 total points
ID: 10730478
I'm getting good results using the correct csv mime-type: text/comma-separated-values

A complete example jsp would be:
<%
response.setContentType("text/comma-separated-values");
response.setHeader("Content-disposition", "file; filename=misc.csv");
int[][] values = {{1, 0, 0}, {2, 3, 2}, {4, 3, 2}};
for (int row = 0; row < values.length; row++) {
  for (int col = 0; col < values[row].length; col++) {
    if (col > 0) {
      out.print(",");
    }
    out.print(values[row][col]);
  }
  out.println();
}
%>

You will have issues with your iteration:
 <logic:iterate id="record" name="reportC11Form" property ="query.results">
            <tr>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(0))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(1))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(2))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(3))%>" /></td>
                <td><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(4))%>" /></td>            </tr>
        </logic:iterate>

First you need to strip out the html tags, but you also need to remove all the new-lines, which means you end up with this horribly long line:
<logic:iterate id="record" name="reportC11Form" property ="query.results"><bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(0))%>" />,<bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(1))%>" />,<bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(2))%>" />,<bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(3))%>" />,<bean:write name="record" property="<%=(String)(reportC11Form.fieldList.get(4))%>" />
</logic:iterate>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question