步驟詳解
1、在Tomcat安裝目錄下的webapps目錄下新建POI目錄,在該目錄中建立WEB-INF、并在WEB-INF中建立lib目錄。
2、從
http://jakarta.apache.org/poi/?下載POI組件包,將其解壓縮到硬盤,找到其中的poi-2.5.1-final-20040804.jar拷貝到應用程序的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瀏覽器圖標?,然后在瀏覽器的地址欄中輸入
http://127.0.0.1:8080/POI/writeexcel.jsp,按鍵盤上的“回車鍵?
使用POI生成Excel文件
6、到該目錄下打開生成的Excel文件
制作要點
在JSP頁面中將數據轉換成Excel格式是經常使用的一個功能。POI是Apache?Jakarta組織的子項目,使用簡單方便,功能強大,可以操作Excel、Word等文件。
POI組件包中提供了幾個類來方便的操作Excel文檔:
HSSFWorkbook類表示Excel文檔中的Book;
HSSFSheet類表示Excel文檔中的Sheet;
HSSFRow類表示Excel文檔中的行;
HSSFCell類表示Excel文檔中的單元格。
用POI生成一個新的Excel文件基本步驟如下:
1、創建新的Excel工作簿
HSSFWorkbook?workbook?=?new?HSSFWorkbook();
2、創建工作表
HSSFSheet?sheet?=?workbook.createSheet();
3、在索引0的位置創建行(最頂端的行)
HSSFRow?row?=?sheet.createRow((short)0);
4、在索引0的位置創建單元格(左上端)
HSSFCell?cell?=?row.createCell((short)?0);
?5、定義單元格類型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
6、在單元格中輸入一些內容
cell.setCellValue("增加值");
7、新建一輸出文件流并把相應的Excel?工作簿存盤
FileOutputStream?fOut?=?new?FileOutputStream(outputFile);
workbook.write(fOut);
fOut.flush();
程序關鍵代碼解釋
生成Excel文件的頁面writeexcel.jsp片斷代碼:
<%?
????//創建新的Excel工作簿
????HSSFWorkbook?wb??=???new??HSSFWorkbook();
????//創建名稱為sheet1的工作表
????HSSFSheet?sheet??=??wb.createSheet(?"?sheet1?"?);
????//在索引0的位置創建行
????HSSFRow?row??=??sheet.createRow((?short?)0);
????//在索引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?"?);
????}?
????//將生成的Excle表格保存
????String?filename=application.getRealPath("/")+"test.xls";
????FileOutputStream?fo=new?FileOutputStream(filename);
????wb.write(fo);
????out.println("excel?文件生成,存放在"+filename);
???
?%>