<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-42  評論-42  文章-0  trackbacks-0

    包下載地址:http://www.apache.org/dist/jakarta/poi/release/bin/ xxx .zip

    參考:http://www.oracle.com/technology/global/cn/pub/articles/saternos_tables.html 使用 Apache Jakarta POI 從 Excel 電子表格生成外部表
       http://blog.tostudy.com.cn/blog/show_996.html
       http://blog.tostudy.com.cn/blog/show_995.html

    一 創建Excel 文檔

      示例1將演示如何利用Jakarta POI API 創建Excel 文檔。

      示例1程序如下:

     1 package all;
     2 
     3 import java.io.FileOutputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFCell;
     6 import org.apache.poi.hssf.usermodel.HSSFRow;
     7 import org.apache.poi.hssf.usermodel.HSSFSheet;
     8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     9 
    10 public class CreateXL {
    11 
    12     /** Excel 文件要存放的位置,假定在D盤下 */
    13 
    14     public static String outputFile = "D:\\english.xls";
    15 
    16     public static void main(String argv[]) {
    17 
    18         try {
    19 
    20             // 創建新的Excel 工作簿
    21 
    22             HSSFWorkbook workbook = new HSSFWorkbook();
    23 
    24             // 在Excel工作簿中建一工作表,其名為缺省值
    25             // 如要新建一名為"效益指標"的工作表,其語句為:
    26             // HSSFSheet sheet = workbook.createSheet("效益指標");
    27             HSSFSheet sheet = workbook.createSheet("EnglishTable");
    28 
    29             // 在索引0的位置創建行(最頂端的行)
    30 
    31             HSSFRow row = sheet.createRow((short0);
    32 
    33             // 在索引0的位置創建單元格(左上端)
    34             HSSFCell cell = row.createCell((short0);
    35             // 定義單元格為字符串類型
    36             cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    37             // 在單元格中輸入一些內容
    38             cell.setCellValue("增加值");
    39             // 新建一輸出文件流
    40             FileOutputStream fOut = new FileOutputStream(outputFile);
    41             // 把相應的Excel 工作簿存盤
    42             workbook.write(fOut);
    43             fOut.flush();
    44             // 操作結束,關閉文件
    45             fOut.close();
    46             System.out.println("文件生成");
    47 
    48         } catch (Exception e) {
    49             System.out.println("已運行 xlCreate() : " + e);
    50         }
    51     }
    52 }
    53 

      

    二 讀取Excel文檔中的數據

      示例2將演示如何讀取Excel文檔中的數據。假定在D盤JTest目錄下有一個文件名為test1.xls的Excel文件。

      示例2程序如下:

     

     1 package all;
     2 
     3 import java.io.FileInputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFCell;
     6 import org.apache.poi.hssf.usermodel.HSSFRow;
     7 import org.apache.poi.hssf.usermodel.HSSFSheet;
     8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     9 
    10 public class ReadXL {
    11     /**
    12      * Excel文件的存放位置:注意是兩個反斜線。 或者可以用一個正斜線 D:/test.xls
    13      * */
    14     public static String fileToBeRead = "D:\\test.xls";
    15 
    16     public static void main(String argv[]) {
    17         try {
    18             // 創建對Excel工作簿文件的引用
    19             HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
    20             // 創建對工作表的引用。
    21             // 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
    22             HSSFSheet sheet = workbook.getSheet("Sheet");
    23             // 也可用getSheetAt(int index)按索引引用,
    24             // 在Excel文檔中,第一張工作表的缺省索引是0,
    25             // 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
    26             // 讀取左上端單元
    27             HSSFRow row = sheet.getRow(0);
    28             HSSFCell cell = row.getCell((short0);
    29             // 輸出單元內容,cell.getStringCellValue()就是取所在單元的值
    30             System.out.println("左上端單元是: " + cell.getStringCellValue());
    31         } catch (Exception e) {
    32             System.out.println("已運行xlRead() : " + e);
    33             e.printStackTrace();
    34         }
    35     }
    36 }
    37 



    三 設置單元格格式

      在這里,我們將只介紹一些和格式設置有關的語句,我們假定workbook就是對一個工作簿的引用。在Java中,第一步要做的就是創建和設置字體和單元格的格式,然后再應用這些格式:

      1、創建字體,設置其為紅色、粗體、字號大小為18

    HSSFFont font = workbook.createFont();
    font.setColor(HSSFFont.COLOR_RED);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    font.setFontHeightInPoints((short) 18);

      2、創建格式

    HSSFCellStyle cellStyle= workbook.createCellStyle();
    cellStyle.setFont(font);

      3、應用格式

    HSSFCell cell = row.createCell((short) 0);
    cell.setCellStyle(cellStyle);
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue("標題 "); 


    四 處理WORD文檔(還沒弄明白關于word文檔,有待學習)

    import java.io.*;
    import org.textmining.text.extraction.WordExtractor;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFCell;

    public class TestPoi {
    public TestPoi() {
    }
    public static void main(String args[]) throws Exception
    {
    FileInputStream in = new FileInputStream ("D:\\a.doc");
    WordExtractor extractor = new WordExtractor();
    String str = extractor.extractText(in);
    //System.out.println("the result length is"+str.length());
    System.out.println(str);
    }
    }
    向EXCEL文件中導入數據以及讀取Excel文檔中的數據。

    posted on 2008-08-04 22:15 BlueSunshine 閱讀(449) 評論(1)  編輯  收藏 所屬分類: 學習心得

    評論:
    # re: 生成 Excel文件 2008-08-04 22:24 | BlueSunshine
    做了個練習:

     1 package all;
     2 
     3 import java.io.FileOutputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFFont;
     6 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
     7 import org.apache.poi.hssf.usermodel.HSSFRow;
     8 import org.apache.poi.hssf.usermodel.HSSFSheet;
     9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    10 
    11 public class CreateExcel {
    12     public static void main(String[] args) {
    13         try {
    14             HSSFWorkbook workbook = new HSSFWorkbook();
    15             HSSFSheet sheet = workbook.createSheet("mySheet1");
    16 
    17             HSSFRow row0 = sheet.createRow(0);
    18             HSSFRichTextString arg0 = new HSSFRichTextString("number");
    19             HSSFFont font = workbook.createFont();
    20             font.setColor(HSSFFont.COLOR_RED);
    21             // bold : 粗體
    22             font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    23             // 字號
    24             font.setFontHeightInPoints((short18);
    25             arg0.applyFont(font);
    26 
    27             row0.createCell((short0).setCellValue(arg0);
    28             row0.createCell((short1).setCellValue("name");
    29             row0.createCell((short2).setCellValue("content");
    30 
    31             HSSFRow row1 = sheet.createRow(1);
    32             row1.createCell((short0).setCellValue("1");
    33             row1.createCell((short1).setCellValue("one");
    34             row1.createCell((short2).setCellValue("This is one.");
    35 
    36             HSSFRow row2 = sheet.createRow(2);
    37             row2.createCell((short0).setCellValue("2");
    38             row2.createCell((short1).setCellValue("two");
    39             row2.createCell((short2).setCellValue("This 是 two.");
    40 
    41             HSSFRow row3 = sheet.createRow(3);
    42             row3.createCell((short0).setCellValue("3");
    43             row3.createCell((short1).setCellValue("哈哈");
    44             row3.createCell((short2).setCellValue("哈哈是pig。");
    45 
    46             FileOutputStream fileOutputStream = new FileOutputStream("E:\\myExcel1.xls");
    47             workbook.write(fileOutputStream);
    48             fileOutputStream.flush();
    49             fileOutputStream.close();
    50             System.out.println("ok");
    51         } catch (Exception e) {
    52             System.out.println("no create" + e);
    53         }
    54     }
    55 }
    56 

    HSSFCell 的 sellCellValue(Stirng value) 方法不提倡使用,所以根據 API 改用sellCellValue(HSSFRiceTextString value)方法。第二個方法具體用法見上例18-27行。

    建出的Excel文件:


      如果要創建的Excel文檔已存在,那么將修改當前的Excel文檔。   回復  更多評論
      
    主站蜘蛛池模板: 亚洲成年人免费网站| 亚洲欧洲精品在线| 亚洲AV永久无码精品网站在线观看| 亚洲中文字幕无码久久综合网| 亚洲中文字幕成人在线| 国产精品亚洲一区二区三区| 永久免费毛片手机版在线看| jiz zz在亚洲| 成人爱做日本视频免费| 美女免费视频一区二区三区| 四虎影视免费永久在线观看| 日韩毛片免费一二三| 亚洲无码精品浪潮| 国产一级a毛一级a看免费视频 | 极品色天使在线婷婷天堂亚洲| 丰满少妇作爱视频免费观看| 亚洲国产精品成人一区| 久草免费福利在线| 亚洲狠狠久久综合一区77777| 亚洲欧洲精品成人久久曰| 看全色黄大色大片免费久久| 亚洲色大成网站WWW国产| 男人的天堂亚洲一区二区三区 | 国产免费卡一卡三卡乱码| 老司机午夜性生免费福利 | 久久久无码精品亚洲日韩软件 | 亚洲神级电影国语版| 无人在线观看免费高清视频| 无码天堂va亚洲va在线va| 国产亚洲精品精品国产亚洲综合| 久久综合久久综合亚洲| 亚洲 小说区 图片区 都市| 在线人成免费视频69国产| 亚洲日本乱码一区二区在线二产线| 国产免费人成视频尤勿视频| 久久久亚洲裙底偷窥综合| 免费被黄网站在观看| baoyu116.永久免费视频| 亚洲最大免费视频网| 四虎影视在线永久免费看黄| 9277手机在线视频观看免费|