使用POI中的HSSF創建Excel文件
from: http://www.cn-java.com/target/news.php?news_id=2510
出處 CN-JAVA翻譯: 孤魂一笑?????
作者:孤魂一笑(bingo_ge@hotmail.com) 日期:2003-05-05
介紹: Jakarta_POI 使用Java讀寫Excel(97-2002)文件,可以滿足大部分的需要。 因為剛好有一個項目使用到了這個工具,花了點時間順便翻譯了一下POI本身 帶的一個Guide.有一些節減和修改,希望給使用這個項目的人一些入門幫助。 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);
|
參考:
http://jakarta.apache.org/poi/hssf/quick-guide.htmlhttp://spaces.msn.com/qiqiboy/blog/