POI 下面有幾個(gè)自項(xiàng)目:HSSF用來實(shí)現(xiàn)Excel 的讀寫.以下是HSSF的主頁(yè)
http://jakarta.apache.org/poi/hssf/index.html
下面的介紹是基于以下地址的翻譯:
http://jakarta.apache.org/poi/hssf/quick-guide.html
目前的版本為1.51應(yīng)該是很長(zhǎng)時(shí)間之內(nèi)的一個(gè)穩(wěn)定版,但HSSF提供的Sample不是基于
1.51所寫,所以使用的時(shí)候需要適當(dāng)?shù)淖⒁?
其實(shí)POI下面的幾個(gè)子項(xiàng)目側(cè)重不同讀寫 Word 的HDF正在開發(fā)當(dāng)中.
XML下的FOP(http://xml.apache.org/fop/index.html)
可以輸出pdf文件,也是比較好的一個(gè)工具
目錄:
創(chuàng)建一個(gè)workbook
創(chuàng)建一個(gè)sheet
創(chuàng)建cells
創(chuàng)建日期cells
設(shè)定單元格格式
說明:
以下可能需要使用到如下的類
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;
創(chuàng)建workbook
HSSFWorkbook wb = new HSSFWorkbook();
//使用默認(rèn)的構(gòu)造方法創(chuàng)建workbook
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
//指定文件名
wb.write(fileOut);
//輸出到文件
fileOut.close();
創(chuàng)建一個(gè)sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//workbook創(chuàng)建sheet
HSSFSheet sheet2 = wb.createSheet("second sheet");
//workbook創(chuàng)建另外的sheet
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
創(chuàng)建cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
//注意以下的代碼很多方法的參數(shù)是short 而不是int 所以需要做一次類型轉(zhuǎn)換
HSSFRow row = sheet.createRow((short)0);
//sheet 創(chuàng)建一行
HSSFCell cell = row.createCell((short)0);
//行創(chuàng)建一個(gè)單元格
cell.setCellValue(1);
//設(shè)定單元格的值
//值的類型參數(shù)有多中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();
創(chuàng)建日期cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
//設(shè)定值為日期
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());
//設(shè)定單元格日期顯示格式
cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
設(shè)定單元格格式
單元格格式的設(shè)定有很多形式包括單元格的對(duì)齊方式,內(nèi)容的字體設(shè)置,
單元格的背景色等,因?yàn)樾问奖容^多,只舉一些例子.以下的例子在
POI1.5中可能會(huì)有所改變具體查看API.
..........
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
//創(chuàng)建一個(gè)樣式
style.setFillBackgroundColor(HSSFCellStyle.AQUA);
//設(shè)定此樣式的的背景顏色填充
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
//樣式的填充類型。
//有多種式樣如:
//HSSFCellStyle.BIG_SPOTS
//HSSFCellStyle.FINE_DOTS
//HSSFCellStyle.SPARSE_DOTS等
style.setAlignment(HSSFCellStyle.ALIGN_CENTER );
//居中對(duì)齊
style.setFillBackgroundColor(HSSFColor.GREEN.index);
//設(shè)定單元個(gè)背景顏色
style.setFillForegroundColor(HSSFColor.RED.index);
//設(shè)置單元格顯示顏色
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
使用poi的hssf生成一個(gè)excel文件以后
有一個(gè)主類Workbook(相當(dāng)于一個(gè)excel文件)的方法
Workbook.write(OutputStream)可以寫到response.getOutputStream()里面
如果事先設(shè)置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("導(dǎo)出Excel.xls".getBytes(),"iso-8859-1")) ;
book.write(response.getOutputStream());
}
其中expertExcel無非是從數(shù)據(jù)庫(kù)或者其他地方獲取數(shù)據(jù)創(chuàng)建excel即可.