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

          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) 評(píng)論(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文件
          制作要點(diǎn)
          在JSP頁面中將數(shù)據(jù)轉(zhuǎn)換成Excel格式是經(jīng)常使用的一個(gè)功能。POI是Apache?Jakarta組織的子項(xiàng)目,使用簡單方便,功能強(qiáng)大,可以操作Excel、Word等文件。
          POI組件包中提供了幾個(gè)類來方便的操作Excel文檔:
          HSSFWorkbook類表示Excel文檔中的Book;
          HSSFSheet類表示Excel文檔中的Sheet;
          HSSFRow類表示Excel文檔中的行;
          HSSFCell類表示Excel文檔中的單元格。
          用POI生成一個(gè)新的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);
          ???
          ?%>


          評(píng)論

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

          2008-01-01 17:50 by Wei
          So appreciate, thanks
          主站蜘蛛池模板: 镇远县| 昌图县| 长垣县| 上杭县| 白山市| 长治县| 阳朔县| 大港区| 南乐县| 德安县| 平乐县| 桂阳县| 休宁县| 乾安县| 凉城县| 梨树县| 大连市| 延安市| 南丹县| 虹口区| 延长县| 大冶市| 迁西县| 饶平县| 青田县| 广安市| 绥芬河市| 德钦县| 柏乡县| 田东县| 丰原市| 进贤县| 雷州市| 施甸县| 乌拉特前旗| 大关县| 波密县| 瑞丽市| 比如县| 仁化县| 年辖:市辖区|