久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

java 導(dǎo)出excel中文亂碼的問題

 IT民工收藏 2015-04-28

最近在網(wǎng)上看到一個(gè)用java來操縱Excel的open source,在weblogic上試用了一下,覺得很不錯(cuò),特此向大家推薦一下。

  首先去http://www./jexcelapi/index.html下載最新的JExcelApi,把jxl.jar置于你的classpath中,。

  寫一個(gè)javaBean,利用JExcelApi來動(dòng)態(tài)生成excel文檔,我這里寫一個(gè)最簡單的,,示意性的,。復(fù)雜的你可能還要查詢數(shù)據(jù)庫什么的。

///////////////////////////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();
  //關(guān)閉Excel工作薄對(duì)象
  wwb.close();
 }

 //最好寫一個(gè)這樣的main方法來測(cè)試一下你的這個(gè)class是否寫好了,。
 public static void main(String[] args)throws Exception{
  File f=new File("kk.xls");
  f.createNewFile();
  writeExcel(new FileOutputStream(f));
 }
}

  寫一個(gè)jsp,來利用Test這個(gè)javabean輸出excel文檔,。

///////////////////////////test_excel.jsp//////////////////////////

http://www./doc/mailto:%@page import="com.jagie.test.Test" %>
<%
 response.reset();
 response.setContentType("application/vnd.ms-excel");
 Test.writeExcel(response.getOutputStream());
%>

  這樣就大功告成了,你用ie訪問test_excel.jsp就能在ie里面打開動(dòng)態(tài)生成的excel文檔了,。一點(diǎn)亂碼也沒有,。

  也許有人會(huì)問:response.reset();可不可以不要這一句,我的建議是一定要寫,除非你能保證response的buffer里面沒有別的東西,。

  還有人也許會(huì)問:我在jsp開頭加上<http://www./doc/mailto:%@page contentType="application/vnd.ms-excel;charset=GBK" %>這一句,,去掉response.setContentType("application/vnd.ms-excel");行不行?回答這個(gè)問題很簡單,就是查看jsp服務(wù)器編譯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(); 之后沒有設(shè)置response contenttype的正確類型,,當(dāng)然輸出為亂碼了。而正確輸出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());之前正確的設(shè)置了response的輸出內(nèi)容,。所以輸出就正常了。

  最后,,希望這篇文章能對(duì)你有所啟發(fā),如有錯(cuò)誤之處,,敬請(qǐng)批評(píng)指正!

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多