Solved

How to export a jsp file to CSV?

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery - parsing the results returned from webmethod 1 112
authenticate preemptively soap ui option 3 143
java cigar challenge 58 212
withoutString  challenge 40 199
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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