JSP动态输出Excel及中文乱码的解决

时间:2021-05-26

  最近在网上看到一个用java来操纵excel的open source,在weblogic上试用了一下,觉得很不错,特此向大家推荐一下。

  首先去http://.jagie.test.Test" %>
<%
 response.reset();
 response.setContentType("application/vnd.ms-excel");
 Test.writeExcel(response.getOutputStream());
%>

  这样就大功告成了,你用ie访问test_excel.jsp就能在ie里面打开动态生成的excel文档了。一点乱码也没有。

  也许有人会问:response.reset();可不可以不要这一句,我的建议是一定要写,除非你能保证response的buffer里面没有别的东西。

  还有人也许会问:我在jsp开头加上<%@page contentType="application/vnd.ms-excel;charset=GBK" %>这一句,去掉response.setContentType("application/vnd.ms-excel");行不行?回答这个问题很简单,就是查看jsp服务器编译jsp后生成的java代码,如果改成这样,我的welogic7编译test_excel.jsp后生成的java文件的示意性代码是这样的:

public void _jspService(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws java.io.IOException,
javax.servlet.ServletException {

 // declare and set well-known variables:
 javax.servlet.ServletConfig config = getServletConfig();
 javax.servlet.ServletContext application = config.getServletContext();
 javax.servlet.jsp.tagext.Tag _activeTag = null;
 // variables for Tag extension protocol

 Object page = this;
 javax.servlet.jsp.JspWriter out;
 javax.servlet.jsp.PageContext pageContext =
 javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this,
 request, response, null, true, 8192, true);

 response.setHeader("Content-Type", "application/vnd.ms-excel; charset=GBK");
 out = pageContext.getOut();
 JspWriter _originalOut = out;

 javax.servlet.http.HttpSession session = request.getSession(true);

 try { // error page try block
  response.setContentType("application/vnd.ms-excel;charset=GBK");
  out.print("\r\n\r\n\r\n\r\n");
  out.print("\r\n");
  //[ /test_excel.jsp; Line: 6]
  response.reset(); //[ /test_excel.jsp; Line: 7]
  //response.setContentType("application/vnd.ms-excel");
  //[ /test_excel.jsp; Line: 8]
  Test.writeExcel(response.getOutputStream()); //[ /test_excel.jsp; Line: 9]
  } catch (Throwable __ee) {
   while (out != null && out != _originalOut) out = pageContext.popBody();
  ((weblogic.servlet.jsp.PageContextImpl)pageContext).handlePageException((Throwable)__ee);
 }

 //before final close brace...
}

  很明显,屏蔽response.setContentType("application/vnd.ms-excel");后,在Test.writeExcel(response.getOutputStream());之前,response.reset(); 之后没有设置response contenttype的正确类型,当然输出为乱码了。而正确输出excel的jsp的编译后源码是这样的:

public void _jspService(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) throws java.io.IOException,
javax.servlet.ServletException
{
 // declare and set well-known variables:
 javax.servlet.ServletConfig config = getServletConfig();
 javax.servlet.ServletContext application = config.getServletContext();
 javax.servlet.jsp.tagext.Tag _activeTag = null;
 // variables for Tag extension protocol

 Object page = this;
 javax.servlet.jsp.JspWriter out;
 javax.servlet.jsp.PageContext pageContext =
  javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);

 out = pageContext.getOut();
 JspWriter _originalOut = out;

 javax.servlet.http.HttpSession session = request.getSession(true);

 try { // error page try block
  out.print("\r\n");
  //[ /test_excel.jsp; Line: 2]
  response.reset(); //[ /test_excel.jsp; Line: 3]
  response.setContentType("application/vnd.ms-excel"); //[ /test_excel.jsp; Line: 4]
  Test.writeExcel(response.getOutputStream()); //[ /test_excel.jsp; Line: 5]
 } catch (Throwable __ee) {
  while (out != null && out != _originalOut) out = pageContext.popBody();
  ((weblogic.servlet.jsp.PageContextImpl)pageContext).handlePageException((Throwable)__ee);
 }

  //before final close brace...
}

  大家可以看到在response.reset();之后,Test.writeExcel(response.getOutputStream());之前正确的设置了response的输出内容。所以输出就正常了。

  最后,希望这篇文章能对你有所启发,如有错误之处,敬请批评指正!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章