隨筆 - 154  文章 - 60  trackbacks - 0
          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          聲明:

          該blog是為了收集資料,認識朋友,學習、提高技術,所以本blog的內容除非聲明,否則一律為轉載!!

          感謝那些公開自己技術成果的高人們!!!

          支持開源,尊重他人的勞動!!

          常用鏈接

          留言簿(3)

          隨筆分類(148)

          隨筆檔案(143)

          收藏夾(2)

          其他

          學習(技術)

          觀察思考(非技術)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          [正文] 1 頁


            最近在網上看到一個用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文檔了。一點亂碼也沒有。
           
          ------------------------------------------------------------
          [正文] 2頁 
             
            也許有人會問: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的輸出內容。所以輸出就正常了。

            最后,希望這篇文章能對你有所啟發,如有錯誤之處,敬請批評指正!

          posted on 2007-10-26 09:19 lk 閱讀(397) 評論(0)  編輯  收藏 所屬分類: j2ee
          主站蜘蛛池模板: 缙云县| 桃园县| 靖宇县| 辽阳县| 内丘县| 高邮市| 南城县| 微山县| 双桥区| 永州市| 龙南县| 晋江市| 杭州市| 玉屏| 虞城县| 无锡市| 丹巴县| 三明市| 延川县| 交口县| 崇州市| 通州区| 中牟县| 双柏县| 枣强县| 衡东县| 永川市| 平山县| 丰台区| 永济市| 佳木斯市| 天柱县| 六枝特区| 琼结县| 洪雅县| 临武县| 鹿邑县| 乌兰浩特市| 抚州市| 邯郸县| 邵东县|