內(nèi)蒙古java團(tuán)隊

          j2se,j2ee開發(fā)組
          posts - 139, comments - 212, trackbacks - 0, articles - 65
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          JSP使用POI讀寫Excel文件

          Posted on 2007-12-25 10:43 帥子 閱讀(2763) 評論(1)  編輯  收藏 所屬分類: J2EE技術(shù)專區(qū)
          步驟詳解
          1、在Tomcat安裝目錄下的webapps目錄下新建POI目錄,在該目錄中建立WEB-INF、并在WEB-INF中建立lib目錄。
          2、從http://jakarta.apache.org/poi/?下載POI組件包,將其解壓縮到硬盤,找到其中的poi-2.5.1-final-20040804.jar拷貝到應(yīng)用程序的WEB-INF/lib目錄下。
          3、打開記事本軟件,編寫生成Excel文件的頁面writeexcel.jsp,其代碼如下:
          <%@?page?language="java"??contentType?="text/html;charset=gb2312"?%>?
          ?<%@?page?import=?"java.util.*,org.apache.poi.hssf.usermodel.HSSFWorkbook,org.apache.poi.hssf.usermodel.HSSFSheet,org.apache.poi.hssf.usermodel.HSSFRow,org.apache.poi.hssf.usermodel.HSSFCell,java.io.*?"?%>?
          ?<%?
          ????HSSFWorkbook?wb??=???new??HSSFWorkbook();
          ????HSSFSheet?sheet??=??wb.createSheet(?"?sheet1?"?);
          ?????HSSFRow?row??=??sheet.createRow((?short?)0);
          ????HSSFCell?cell1??=??row.createCell((?short?)0);
          ????HSSFCell?cell2??=??row.createCell((?short?)1);
          ????HSSFCell?cell3??=??row.createCell((?short?)2);
          ????cell1.setEncoding((?short?)1);
          ????cell1.setCellType(?1?);
          ????cell2.setEncoding((?short?)1);
          ????cell2.setCellType(?1?);
          ????cell3.setEncoding((?short?)1);
          ????cell3.setCellType(?1?);
          ????cell1.setCellValue(?"?測試?"?);
          ????cell2.setCellValue(?"?測試2?"?);
          ????cell3.setCellValue(?"?測試3?"?);
          ?????for??(?int?i?=?0?;?i?<?4?;?i?++?)???{
          ?????????row??=??sheet.createRow((?short?)?i??+?1?);
          ????????cell1??=??row.createCell((?short?)??0?);
          ????????cell2??=??row.createCell((?short?)??1?);
          ????????cell3??=??row.createCell((?short?)??2?);
          ????????cell1.setEncoding((?short?)1);
          ????????cell1.setCellType(?1?);
          ????????cell2.setEncoding((?short?)1);
          ????????cell2.setCellType(?1?);
          ????????cell3.setEncoding((?short?)1);
          ????????cell3.setCellType(?1?);
          ????????cell1.setCellValue(?"?ggg?"?);
          ????????cell2.setCellValue(?"?00000?"?);
          ????????cell3.setCellValue(?"?sun?"?);
          ????}?
          ????String?filename=application.getRealPath("/")+"test.xls";
          ????FileOutputStream?fo=new?FileOutputStream(filename);
          ????wb.write(fo);
          ????out.println("excel?文件生成,存放在"+filename);
          ???
          ?%>
          4、按下鍵盤上的【Ctrl】/【S】鍵,保存該文件,保存在“webapps\POI”目錄下。
          5、雙擊桌面上的IE瀏覽器圖標(biāo)?,然后在瀏覽器的地址欄中輸入http://127.0.0.1:8080/POI/writeexcel.jsp,按鍵盤上的“回車鍵?
          使用POI生成Excel文件
          6、到該目錄下打開生成的Excel文件
          制作要點
          在JSP頁面中將數(shù)據(jù)轉(zhuǎn)換成Excel格式是經(jīng)常使用的一個功能。POI是Apache?Jakarta組織的子項目,使用簡單方便,功能強(qiáng)大,可以操作Excel、Word等文件。
          POI組件包中提供了幾個類來方便的操作Excel文檔:
          HSSFWorkbook類表示Excel文檔中的Book;
          HSSFSheet類表示Excel文檔中的Sheet;
          HSSFRow類表示Excel文檔中的行;
          HSSFCell類表示Excel文檔中的單元格。
          用POI生成一個新的Excel文件基本步驟如下:
          1、創(chuàng)建新的Excel工作簿
          HSSFWorkbook?workbook?=?new?HSSFWorkbook();
          2、創(chuàng)建工作表
          HSSFSheet?sheet?=?workbook.createSheet();
          3、在索引0的位置創(chuàng)建行(最頂端的行)
          HSSFRow?row?=?sheet.createRow((short)0);
          4、在索引0的位置創(chuàng)建單元格(左上端)
          HSSFCell?cell?=?row.createCell((short)?0);
            ?5、定義單元格類型
          cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          6、在單元格中輸入一些內(nèi)容
          cell.setCellValue("增加值");
          7、新建一輸出文件流并把相應(yīng)的Excel?工作簿存盤
          FileOutputStream?fOut?=?new?FileOutputStream(outputFile);
          workbook.write(fOut);
          fOut.flush();
          程序關(guān)鍵代碼解釋
          生成Excel文件的頁面writeexcel.jsp片斷代碼:
          <%?
          ????//創(chuàng)建新的Excel工作簿
          ????HSSFWorkbook?wb??=???new??HSSFWorkbook();
          ????//創(chuàng)建名稱為sheet1的工作表
          ????HSSFSheet?sheet??=??wb.createSheet(?"?sheet1?"?);
          ????//在索引0的位置創(chuàng)建行
          ????HSSFRow?row??=??sheet.createRow((?short?)0);
          ????//在索引0的位置創(chuàng)建單元格
          ????HSSFCell?cell1??=??row.createCell((?short?)0);
          ????HSSFCell?cell2??=??row.createCell((?short?)1);
          ????HSSFCell?cell3??=??row.createCell((?short?)2);
          ????//設(shè)置每列的屬性名
          ????cell1.setEncoding((?short?)1);
          ????cell1.setCellType(?1?);
          ????cell2.setEncoding((?short?)1);
          ????cell2.setCellType(?1?);
          ????cell3.setEncoding((?short?)1);
          ????cell3.setCellType(?1?);
          ????cell1.setCellValue(?"?測試?"?);
          ????cell2.setCellValue(?"?測試2?"?);
          ????cell3.setCellValue(?"?測試3?"?);
          ????//循環(huán)生成每行中單元格中的值
          ?????for??(?int?i?=?0?;?i?<?4?;?i?++?)???{
          ?????????row??=??sheet.createRow((?short?)?i??+?1?);
          ????????cell1??=??row.createCell((?short?)??0?);
          ????????cell2??=??row.createCell((?short?)??1?);
          ????????cell3??=??row.createCell((?short?)??2?);
          ????????cell1.setEncoding((?short?)1);
          ????????cell1.setCellType(?1?);
          ????????cell2.setEncoding((?short?)1);
          ????????cell2.setCellType(?1?);
          ????????cell3.setEncoding((?short?)1);
          ????????cell3.setCellType(?1?);
          ????????cell1.setCellValue(?"?ggg?"?);
          ????????cell2.setCellValue(?"?00000?"?);
          ????????cell3.setCellValue(?"?sun?"?);
          ????}?
          ????//將生成的Excle表格保存
          ????String?filename=application.getRealPath("/")+"test.xls";
          ????FileOutputStream?fo=new?FileOutputStream(filename);
          ????wb.write(fo);
          ????out.println("excel?文件生成,存放在"+filename);
          ???
          ?%>


          評論

          # re: JSP使用POI讀寫Excel文件[未登錄]  回復(fù)  更多評論   

          2008-01-01 17:50 by Wei
          So appreciate, thanks
          主站蜘蛛池模板: 措美县| 临武县| 福建省| 巧家县| 双桥区| 平阳县| 龙州县| 临城县| 莆田市| 老河口市| 吐鲁番市| 石家庄市| 龙南县| 渑池县| 什邡市| 黄陵县| 大关县| 莎车县| 白银市| 安平县| 宁强县| 宜城市| 鄂伦春自治旗| 永康市| 芜湖县| 吴忠市| 什邡市| 桦南县| 康马县| 宁陵县| 墨竹工卡县| 腾冲县| 崇明县| 涿州市| 龙门县| 和政县| 东宁县| 克东县| 顺昌县| 安乡县| 丰都县|