POI 下面有幾個自項目:HSSF用來實現Excel 的讀寫.以下是HSSF的主頁
http://jakarta.apache.org/poi/hssf/index.html
下面的介紹是基于以下地址的翻譯:
http://jakarta.apache.org/poi/hssf/quick-guide.html
目前的版本為1.51應該是很長時間之內的一個穩定版,但HSSF提供的Sample不是基于
1.51所寫,所以使用的時候需要適當的注意.
其實POI下面的幾個子項目側重不同讀寫 Word 的HDF正在開發當中.
XML下的FOP(http://xml.apache.org/fop/index.html)
可以輸出pdf文件,也是比較好的一個工具
目錄:
創建一個workbook
創建一個sheet
創建cells
創建日期cells
設定單元格格式
說明:
以下可能需要使用到如下的類
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
創建workbook
HSSFWorkbook wb = new HSSFWorkbook();
//使用默認的構造方法創建workbook
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
//指定文件名
wb.write(fileOut);
//輸出到文件
fileOut.close();
創建一個sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//workbook創建sheet
HSSFSheet sheet2 = wb.createSheet("second sheet");
//workbook創建另外的sheet
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
創建cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
//注意以下的代碼很多方法的參數是short 而不是int 所以需要做一次類型轉換
HSSFRow row = sheet.createRow((short)0);
//sheet 創建一行
HSSFCell cell = row.createCell((short)0);
//行創建一個單元格
cell.setCellValue(1);
//設定單元格的值
//值的類型參數有多中double ,String ,boolean,
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
創建日期cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
//設定值為日期
cell.setCellValue(new Date());
HSSFCellStyle cellStyle = wb.createCellStyle();
//指定日期顯示格式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
//設定單元格日期顯示格式
cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
設定單元格格式
單元格格式的設定有很多形式包括單元格的對齊方式,內容的字體設置,
單元格的背景色等,因為形式比較多,只舉一些例子.以下的例子在
POI1.5中可能會有所改變具體查看API.
..........
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
//創建一個樣式
style.setFillBackgroundColor(HSSFCellStyle.AQUA);
//設定此樣式的的背景顏色填充
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
//樣式的填充類型。
//有多種式樣如:
//HSSFCellStyle.BIG_SPOTS
//HSSFCellStyle.FINE_DOTS
//HSSFCellStyle.SPARSE_DOTS等
style.setAlignment(HSSFCellStyle.ALIGN_CENTER );
//居中對齊
style.setFillBackgroundColor(HSSFColor.GREEN.index);
//設定單元個背景顏色
style.setFillForegroundColor(HSSFColor.RED.index);
//設置單元格顯示顏色
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
使用poi的hssf生成一個excel文件以后
有一個主類Workbook(相當于一個excel文件)的方法
Workbook.write(OutputStream)可以寫到response.getOutputStream()里面
如果事先設置response的contentType為excel和下載的附件名稱就可下載excel
HSSFWorkbook book = _proxy.expertExcel(_formBean,_login);
if(book!=null) {
response.setContentType ( "application/ms-excel" ) ;
response.setHeader ( "Content-Disposition" ,"attachment;filename="+new String("導出Excel.xls".getBytes(),"iso-8859-1")) ;
book.write(response.getOutputStream());
}
其中expertExcel無非是從數據庫或者其他地方獲取數據創建excel即可.
posted on 2006-02-17 16:08
aiyoyoyo 閱讀(383)
評論(0) 編輯 收藏 所屬分類:
其他