最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

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

日期:11-16    来源:中国设计秀    作者:cnwebshow.com

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

  首先去http://www.andykhan.com/jexcelapi/index.html下载最新的JexcelApi,把jxl.jar置于你的classpath中。BLi中国设计秀

  写一个javaBean,利用JexcelApi来动态生成excel文档,我这里写一个最简单的,示意性的。复杂的你可能还要查询数据库什么的。BLi中国设计秀

///////////////////////////Test.java///////////////////////////////////////////BLi中国设计秀
package com.jagie.test;BLi中国设计秀
import java.io.*;BLi中国设计秀
import jxl.*;BLi中国设计秀
import jxl.write.*;BLi中国设计秀
import jxl.format.*;BLi中国设计秀
import java.util.*;BLi中国设计秀
import java.awt.Color;BLi中国设计秀

public class Test{BLi中国设计秀
 public static void writeexcel(OutputStream os) throws Exception {BLi中国设计秀
  jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);BLi中国设计秀
  jxl.write.WritableSheet ws = wwb.createSheet("TestSheet1", 0);BLi中国设计秀
  jxl.write.Label labelC = new jxl.write.Label(0, 0, "我爱中国");BLi中国设计秀
  ws.addCell(labelC);BLi中国设计秀
  jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.ARIAL,20, WritableFont.BOLD, false,BLi中国设计秀
  UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);BLi中国设计秀
  jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);BLi中国设计秀
  wcfFC.setBackground(jxl.format.Colour.RED);BLi中国设计秀
  labelC = new jxl.write.Label(6, 0, "中国爱我",wcfFC);BLi中国设计秀
  ws.addCell(labelC);BLi中国设计秀
  //写入Exel工作表BLi中国设计秀
  wwb.write();BLi中国设计秀
  //关闭excel工作薄对象BLi中国设计秀
  wwb.close();BLi中国设计秀
 }BLi中国设计秀

 //最好写一个这样的main方法来测试一下你的这个class是否写好了。BLi中国设计秀
 public static void main(String[] args)throws Exception{BLi中国设计秀
  File f=new File("kk.xls");BLi中国设计秀
  f.createNewFile();BLi中国设计秀
  writeexcel(new FileOutputStream(f));BLi中国设计秀
 }BLi中国设计秀
}BLi中国设计秀

  写一个jsp,来利用Test这个javabean输出excel文档。BLi中国设计秀

///////////////////////////test_excel.jsp//////////////////////////BLi中国设计秀

<%@page import="com.jagie.test.Test" %>BLi中国设计秀
<%BLi中国设计秀
 response.reset();BLi中国设计秀
 response.setContentType("application/vnd.ms-excel");BLi中国设计秀
 Test.writeexcel(response.getOutputStream());BLi中国设计秀
%>BLi中国设计秀

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

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

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

public void _jspService(javax.servlet.http.HttpServletRequest request, BLi中国设计秀
javax.servlet.http.HttpServletResponse response) throws java.io.IOException, BLi中国设计秀
javax.servlet.ServletException {BLi中国设计秀

 // declare and set well-known variables:BLi中国设计秀
 javax.servlet.ServletConfig config = getServletConfig();BLi中国设计秀
 javax.servlet.ServletContext application = config.getServletContext();BLi中国设计秀
 javax.servlet.jsp.tagext.Tag _activeTag = null;BLi中国设计秀
 // variables for Tag extension PRotocolBLi中国设计秀

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

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

 javax.servlet.http.Httpsession session = request.getSession(true);BLi中国设计秀

 try { // error page try blockBLi中国设计秀
  response.setContentType("application/vnd.ms-excel;charset=GBK");BLi中国设计秀
  out.print("rnrnrnrn");BLi中国设计秀
  out.print("rn");BLi中国设计秀
  //[ /test_excel.jsp; Line: 6]BLi中国设计秀
  response.reset(); //[ /test_excel.jsp; Line: 7]BLi中国设计秀
  //response.setContentType("application/vnd.ms-excel"); BLi中国设计秀
  //[ /test_excel.jsp; Line: 8]BLi中国设计秀
  Test.writeexcel(response.getOutputStream()); //[ /test_excel.jsp; Line: 9]BLi中国设计秀
  } catch (Throwable __ee) {BLi中国设计秀
   while (out != null && out != _originalOut) out = pageContext.popBody();BLi中国设计秀
  ((weblogic.servlet.jsp.PageContextImpl)pageContext).handlePageException((Throwable)__ee);BLi中国设计秀
 }BLi中国设计秀

 //before final close brace...BLi中国设计秀
}BLi中国设计秀

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

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

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

 out = pageContext.getOut();BLi中国设计秀
 JspWriter _originalOut = out;BLi中国设计秀

 javax.servlet.http.HttpSession session = request.getSession(true);BLi中国设计秀

 try { // error page try blockBLi中国设计秀
  out.print("rn");BLi中国设计秀
  //[ /test_excel.jsp; Line: 2]BLi中国设计秀
  response.reset(); //[ /test_excel.jsp; Line: 3]BLi中国设计秀
  response.setContentType("application/vnd.ms-excel"); //[ /test_excel.jsp; Line: 4]BLi中国设计秀
  Test.writeexcel(response.getOutputStream()); //[ /test_excel.jsp; Line: 5]BLi中国设计秀
 } catch (Throwable __ee) {BLi中国设计秀
  while (out != null && out != _originalOut) out = pageContext.popBody();BLi中国设计秀
  ((weblogic.servlet.jsp.PageContextImpl)pageContext).handlePageException((Throwable)__ee);BLi中国设计秀
 }BLi中国设计秀

  //before final close brace...BLi中国设计秀
}BLi中国设计秀

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

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

本文引用地址:/bc/article_46741.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明