JSP动态输出Excel及中文乱码的解决
最近在网上看到一个用java来操纵excel的open source,在weblogic上试用了一下,觉得很不错,特此向大家推荐一下。
首先去http://www.andykhan.com/jexcelapi/index.html下载最新的jexcelapi,把jxl.jar置于你的classpath中。
写一个javabean,利用jexcelapi来动态生成excel文档,我这里写一个最简单的,示意性的。复杂的你可能还要查询数据库什么的。
///////////////////////////test.java///////////////////////////////////////////
package com.jagie.test;
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
import java.util.*;
import java.awt.color;
public class test{
public static void writeexcel(outputstream os) throws exception {
jxl.write.writableworkbook wwb = workbook.createworkbook(os);
jxl.write.writablesheet ws = wwb.createsheet("testsheet1", 0);
jxl.write.label labelc = new jxl.write.label(0, 0, "我爱中国");
ws.addcell(labelc);
jxl.write.writablefont wfc = new jxl.write.writablefont(writablefont.arial,20, writablefont.bold, false,
underlinestyle.no_underline, jxl.format.colour.green);
jxl.write.writablecellformat wcffc = new jxl.write.writablecellformat(wfc);
wcffc.setbackground(jxl.format.colour.red);
labelc = new jxl.write.label(6, 0, "中国爱我",wcffc);
ws.addcell(labelc);
//写入exel工作表
wwb.write();
//关闭excel工作薄对象
wwb.close();
}
//最好写一个这样的main方法来测试一下你的这个class是否写好了。
public static void main(string[] args)throws exception{
file f=new file("kk.xls");
f.createnewfile();
writeexcel(new fileoutputstream(f));
}
}
写一个jsp,来利用test这个javabean输出excel文档。
///////////////////////////test_excel.jsp//////////////////////////
<%@page import="com.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的输出内容。所以输出就正常了。
最后,希望这篇文章能对你有所启发,如有错误之处,敬请批评指正!