Solved

How to export a jsp file to CSV?

Posted on 2004-03-31
4
1,422 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 50 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SimpleDateFormat.parse examples 19 122
getting param value 7 62
caughtSpeeding java challenge 16 141
nested if has else if 13 101
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
Deploying our service is a grudge match between customer benefits and customer pain. In one corner, rolling out fixes (yay!) and delivering new features (double yay!). In the other corner, training on new features (boo – sounds like work), and chan…

732 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