• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

Junk character when write data to Exel from JSP

Hi experts!

I have a very very strange problem when trying to write data to excel from JSP.
My data comes from Oracle database and displayed correctly in IE web browser but it change into junk character in excel, when it has more than 1 row, so strange?

Please see my problem below. Many thanks!

Note: My desire result is excel data in Vietnamese character.
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
 
<netui:html>
    <head>
       <title>Missing Premium Report</title>
    </head>
    <body>
       <table width="780" height="85" border="1" cellspacing="0" cellpadding="0">
          <tr bgcolor="#FFBA00" align="center">
            <td><div align="center"><strong>No</strong></div></td>
            <td><div align="center"><strong>Partner Name </strong></div></td>
            <td><div align="center"><strong>Partner Code</strong></div></td>
          </tr>
          <%
try{
	response.setContentType("application/vnd.ms-excel; charset=UTF-8");
	response.setHeader("Content-disposition","attachment;filename=MissingPremiumReport.xls");
 
	if(request.getMethod().equals("POST") == true){
ArrayList results = null;
    
        results = ReportService.getMissingPremiumReport(DateParser.toDateDDMMYYYY("24-04-2009"), "test01");
for(int ci = 0 ; ci < results.size(); ci++  ){
		    MissingPremiumReportObj tmp  = (MissingPremiumReportObj)results.get(ci);	
%>
<tr id="tr<%=ci%>" onMouseOver="hlRow('tr<%=ci%>')">
                <td><p align='center'><%=ci+1%></p></td>
				<td><p align='left'><%=tmp.getPartner_name()%></p></td>
				<td><p align='center'><%=tmp.getPartner_id()%></p></td>
				<td><p align='center'><%=tmp.getPolicy_id()%></p></td>
</tr>
<%}%>
</table>
 <%
	}
	else{
		out.println("</table>");
	}
}
catch(Exception ex){
	out.println(ex.toString());
}
%>
    </body>
</netui:html>

Open in new window

1row.jpg
more1row.jpg
0
ncsvietnam
Asked:
ncsvietnam
  • 2
1 Solution
 
jan42Commented:
The second screenshot looks like excel or whatever program you used to display the data thinks that the data is in ISO-8859-1 encoding.

You are setting the encoding with the statment
   response.setContentType("application/vnd.ms-excel; charset=UTF-8");
but that statement comes late in the JSP. So it could happen that your servlet container starts writing out the response before the content type gets set. The exact point in time when the servlet container starts to write output to the browser depends on its buffer settings -- this might explain why you see different behavior depending on the size of the output.

To verify this hypothesis, look at the respsone from your servlet container on the wire (using TCPMon or similar tools).

To solve the problem, try moving the statement that sets the content type further up in the JSP (so that the content type gets set before any output is written).
0
 
ncsvietnamAuthor Commented:
Hi jan42,

Thanks for your help but it does not work! Or I don't put statement that sets the content type in a correct place!?
Please give more help, I've tried putting in right inside <body> tag, <netui:html> tag, even right above <netui:html>.

Thanks again!

<%
	//response.setContentType("application/vnd.ms-excel; charset=UTF-8");
	//response.setHeader("Content-disposition","attachment;filename=MissingPremiumReport.xls");%>
<netui:html>
<%
	//response.setContentType("application/vnd.ms-excel; charset=UTF-8");
	//response.setHeader("Content-disposition","attachment;filename=MissingPremiumReport.xls");%>
    <head>
       <title>Missing Premium Report</title>
    </head>
    <body>
<%
	//response.setContentType("application/vnd.ms-excel; charset=UTF-8");
	//response.setHeader("Content-disposition","attachment;filename=MissingPremiumReport.xls");%>
...

Open in new window

0
 
ncsvietnamAuthor Commented:
ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now