使用POI中的HSSF創(chuàng)建Excel文件

          使用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)文件,可以滿足大部分的需要。
          因為剛好有一個項目使用到了這個工具,花了點時間順便翻譯了一下POI本身
          帶的一個Guide.有一些節(jié)減和修改,希望給使用這個項目的人一些入門幫助。
          POI 下面有幾個自項目:HSSF用來實現(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)該是很長時間之內(nèi)的一個穩(wěn)定版,但HSSF提供的Sample不是基于
          1.51所寫,所以使用的時候需要適當(dāng)?shù)淖⒁?
          其實POI下面的幾個子項目側(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è)置,
          單元格的背景色等,因為形式比較多,只舉一些例子.以下的例子在
          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.html

          http://spaces.msn.com/qiqiboy/blog/

          posted on 2006-05-17 16:46 扭轉(zhuǎn)乾坤 閱讀(405) 評論(0)  編輯  收藏 所屬分類: JAVA使用技巧

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(2)

          隨筆分類(31)

          隨筆檔案(30)

          文章分類(32)

          文章檔案(33)

          相冊

          PHP小站-首頁

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 老河口市| 汉中市| 孝义市| 巴林右旗| 东安县| 沽源县| 汕头市| 永胜县| 北安市| 洪洞县| 霸州市| 开鲁县| 泗洪县| 鄂托克旗| 宁津县| 延津县| 杭州市| 额济纳旗| 建宁县| 新巴尔虎右旗| 泾川县| 天峨县| 淮安市| 德阳市| 锦州市| 都江堰市| 林口县| 合阳县| 福建省| 寻甸| 奉化市| 大英县| 新疆| 湘阴县| 开阳县| 宁晋县| 赣榆县| 石城县| 宝应县| 濉溪县| 辽宁省|