使用POI中的HSSF創(chuàng)建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)文件,可以滿足大部分的需要。 因?yàn)閯偤糜幸粋€項(xiàng)目使用到了這個工具,花了點(diǎn)時(shí)間順便翻譯了一下POI本身 帶的一個Guide.有一些節(jié)減和修改,希望給使用這個項(xiàng)目的人一些入門幫助。 POI 下面有幾個自項(xiàng)目:HSSF用來實(shí)現(xiàn)Excel 的讀寫.以下是HSSF的主頁 http://jakarta.apache.org/poi/hssf/index.html 下面的介紹是基于以下地址的翻譯: http://jakarta.apache.org/poi/hssf/quick-guide.html 目前的版本為1.51應(yīng)該是很長時(shí)間之內(nèi)的一個穩(wěn)定版,但HSSF提供的Sample不是基于 1.51所寫,所以使用的時(shí)候需要適當(dāng)?shù)淖⒁? 其實(shí)POI下面的幾個子項(xiàng)目側(cè)重不同讀寫 Word 的HDF正在開發(fā)當(dāng)中. XML下的FOP(http://xml.apache.org/fop/index.html) 可以輸出pdf文件,也是比較好的一個工具 目錄: 創(chuàng)建一個workbook 創(chuàng)建一個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)建一個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)建一個單元格 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è)定有很多形式包括單元格的對齊方式,內(nèi)容的字體設(shè)置, 單元格的背景色等,因?yàn)樾问奖容^多,只舉一些例子.以下的例子在 POI1.5中可能會有所改變具體查看API. .......... // Aqua background HSSFCellStyle style = wb.createCellStyle(); //創(chuàng)建一個樣式 style.setFillBackgroundColor(HSSFCellStyle.AQUA); //設(shè)定此樣式的的背景顏色填充 style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
//樣式的填充類型。 //有多種式樣如: //HSSFCellStyle.BIG_SPOTS //HSSFCellStyle.FINE_DOTS //HSSFCellStyle.SPARSE_DOTS等 style.setAlignment(HSSFCellStyle.ALIGN_CENTER ); //居中對齊 style.setFillBackgroundColor(HSSFColor.GREEN.index); //設(shè)定單元個背景顏色 style.setFillForegroundColor(HSSFColor.RED.index); //設(shè)置單元格顯示顏色 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/