婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > JSP動(dòng)態(tài)輸出Excel及中文亂碼的解決

JSP動(dòng)態(tài)輸出Excel及中文亂碼的解決

熱門(mén)標(biāo)簽:地圖標(biāo)注制作道路 廣州三五防封電銷(xiāo)卡 電銷(xiāo)外呼系統(tǒng) 排行榜 長(zhǎng)春回?fù)芡夂粝到y(tǒng)廠家 珠海銷(xiāo)售外呼系統(tǒng)運(yùn)營(yíng)商 地圖標(biāo)注創(chuàng)業(yè)項(xiàng)目入駐 山東智能云外呼管理系統(tǒng) 外呼系統(tǒng)啥意思 四川電信外呼系統(tǒng)靠譜嗎

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

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

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

///////////////////////////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, "我愛(ài)中國(guó)");
  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, "中國(guó)愛(ài)我",wcfFC);
  ws.addCell(labelC);
  //寫(xiě)入Exel工作表
  wwb.write();
  //關(guān)閉Excel工作薄對(duì)象
  wwb.close();
 }

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

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

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

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

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

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

  還有人也許會(huì)問(wèn):我在jsp開(kāi)頭加上<%@page contentType="application/vnd.ms-excel;charset=GBK" %>這一句,去掉response.setContentType("application/vnd.ms-excel");行不行?回答這個(gè)問(wèn)題很簡(jiǎn)單,就是查看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(); 之后沒(méi)有設(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)指正!

您可能感興趣的文章:
  • jsp 頁(yè)面上圖片分行輸出小技巧
  • jsp輸出金字塔的簡(jiǎn)單實(shí)例
  • jsp輸出九九乘法表的簡(jiǎn)單實(shí)例
  • jsp輸出當(dāng)前時(shí)間的實(shí)現(xiàn)代碼
  • JSP輸出HTML時(shí)產(chǎn)生的大量空格和換行的去除方法
  • 動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法

標(biāo)簽:潮州 肇慶 玉樹(shù) 吳忠 紹興 北海 廣元 保定

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP動(dòng)態(tài)輸出Excel及中文亂碼的解決》,本文關(guān)鍵詞  JSP,動(dòng)態(tài),輸出,Excel,及,中文,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP動(dòng)態(tài)輸出Excel及中文亂碼的解決》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于JSP動(dòng)態(tài)輸出Excel及中文亂碼的解決的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 驻马店市| 宁津县| 曲沃县| 岳阳县| 乐平市| 枣阳市| 丰城市| 双柏县| 航空| 江阴市| 大渡口区| 龙井市| 大洼县| 木兰县| 田林县| 江永县| 泌阳县| 剑川县| 凤庆县| 潼南县| 东城区| 资源县| 宝鸡市| 临汾市| 察雅县| 津南区| 渑池县| 牡丹江市| 赤壁市| 竹北市| 天长市| 盐源县| 陇西县| 娄底市| 三台县| 东乌珠穆沁旗| 靖江市| 汪清县| 上犹县| 延川县| 蓬安县|