Solved

How to export a jsp file to CSV?

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now