Solved

How to export a jsp file to CSV?

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

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

Suggested Solutions

Title # Comments Views Activity
iterator example remove 8 75
maven set up 2 171
jdbc error in jsp application 20 63
iterator/ListIterator approach 17 39
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

838 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