POI和easyExcel使用(2)PoI對Excel的基本讀寫操作

excel有四個主要對象:工作薄、工作表、行、單元格

1.POI的寫操作

創建一個maven項目,需要引入POI的一些包

<!--xls(03)-->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.9</version>
</dependency>
<!--xls(07)-->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.9</version>
</dependency>

寫操作基本包括如下步驟(針對03年Excel):

  • 創建工作薄
 //創建一個工作簿
 Workbook workbook = new HSSFWorkbook();
  • 創建工作表
//創建一個工作表
Sheet sheet = workbook.createSheet("學生統計表");
  • 創建行
 //創建一個行
 Row row1 =  sheet.createRow(0);
  • 創建單元格,並賦值
//創建一個單元格
Cell cell = row1.createCell(0);
cell.setCellValue("1,1");
  • 生成表
 //生成一張表
 FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xls");
 workbook.write(fileOutputStream);
 //關閉
 fileOutputStream.close();

07版和03版操作一樣,就是工作簿對象不一樣,同時也是文件後綴不一樣

  @Test
  //針對07版Excel
  public void test07() throws Exception {
    //創建一個工作簿
    Workbook workbook = new XSSFWorkbook();
    //創建一個工作表
    Sheet sheet = workbook.createSheet("學生統計表");
    //創建一個行
    Row row1 =  sheet.createRow(0);
    //創建一個單元格
    Cell cell = row1.createCell(0);
    cell.setCellValue("1,1");

    //生成一張表
    FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xlsx");
    workbook.write(fileOutputStream);

    //關閉
    fileOutputStream.close();
  }

2. POI大數據量寫入

 03版:只能導65536行數據

 @Test
  //針對03版Excel
  public void test03BigData() throws Exception {

    long begin = System.currentTimeMillis();
    //創建一個工作簿
    Workbook workbook = new HSSFWorkbook();
    //創建一個工作表
    Sheet sheet = workbook.createSheet("學生統計表");

    //大數據導入
    for(int rowNum=0;rowNum<65536;rowNum++) {
      Row row = sheet.createRow(rowNum);
      for (int column = 0; column < 10; column++) {
        Cell cell = row.createCell(column);
        cell.setCellValue(column);
      }
    }

    //生成一張表
    FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xls");
    workbook.write(fileOutputStream);

    //關閉
    fileOutputStream.close();

    long end = System.currentTimeMillis();
    System.out.println("時間"+(end-begin)/1000+"s");
  }

 07版:無行數限制速度慢

@Test
  //針對03版Excel
  public void test07BigData() throws Exception {

    long begin = System.currentTimeMillis();
    //創建一個工作簿
    Workbook workbook = new XSSFWorkbook();
    //創建一個工作表
    Sheet sheet = workbook.createSheet("學生統計表");

    //大數據導入
    for(int rowNum=0;rowNum<65536;rowNum++) {
      Row row = sheet.createRow(rowNum);
      for (int column = 0; column < 10; column++) {
        Cell cell = row.createCell(column);
        cell.setCellValue(column);
      }
    }

    //生成一張表
    FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xlsx");
    workbook.write(fileOutputStream);

    //關閉
    fileOutputStream.close();

    long end = System.currentTimeMillis();
    System.out.println("時間"+(end-begin)/1000+"s");
  }

 加速版:

@Test
  //針對07版Excel
  public void test07BigDataS() throws Exception {

    long begin = System.currentTimeMillis();
    //創建一個工作簿
    Workbook workbook = new SXSSFWorkbook();
    //創建一個工作表
    Sheet sheet = workbook.createSheet("學生統計表");

    //大數據導入
    for(int rowNum=0;rowNum<100000;rowNum++) {
      Row row = sheet.createRow(rowNum);
      for (int column = 0; column < 10; column++) {
        Cell cell = row.createCell(column);
        cell.setCellValue(column);
      }
    }

    //生成一張表
    FileOutputStream fileOutputStream = new FileOutputStream(path+"學生統計表.xlsx");
    workbook.write(fileOutputStream);

    //關閉
    fileOutputStream.close();

    //清除臨時文件
    ((SXSSFWorkbook)workbook).dispose();

    long end = System.currentTimeMillis();
    System.out.println("時間"+(end-begin)/1000+"s");
  }

3. poi讀操作

步驟:

  • 獲取文件流
FileInputStream fileInputStream = new FileInputStream(path + "學生統計表.xlsx");
  •  根據文件流創建工作薄
 //創建工作薄
 Workbook workbook = new HSSFWorkbook(fileInputStream);
  • 得到工作表
//獲得工作表
Sheet sheet = workbook.getSheetAt(0);
  • 得到行
 //獲得行
 Row row = sheet.getRow(0);
  • 得到單元格並輸出,讀取值要保證類型相同
 //獲得單元格
 Cell cell = row.getCell(0);
 System.out.println(cell.getStringCellValue());
  • 關閉文件流
fileInputStream.close();

4.讀取不同類型的數據

先獲取標題,再獲取內容

@Test
  public void readCellType() throws Exception {
    FileInputStream fileInputStream = new FileInputStream(path + "test.xls");
    //創建工作薄
    Workbook workbook = new HSSFWorkbook(fileInputStream);
    //獲得工作表
    Sheet sheet = workbook.getSheetAt(0);
    //獲取標題內容(第一行)
    Row rowHead = sheet.getRow(0);
    if (rowHead != null) {
      //獲取總數目
      int cellCount = rowHead.getPhysicalNumberOfCells();
      for (int cellNum = 0; cellNum < cellCount; cellNum++) {
        Cell cell = rowHead.getCell(cellNum);
        if (cell != null) {
          String cellValue = cell.getStringCellValue();
          System.out.print(cellValue + " | ");
        }
      }
      System.out.println();
      //讀取表中內容
      int rowCount = sheet.getPhysicalNumberOfRows();
      for (int rowNum = 1; rowNum < rowCount; rowNum++) {
        Row rowData = sheet.getRow(rowNum);
        if (rowData != null) {
          for (int cellNum=0;cellNum<cellCount;cellNum++){
            Cell cell = rowData.getCell(cellNum);
            //匹配列的數據類型
            if(cell!=null){
              int cellType = cell.getCellType();
              String cellValue = "";
              switch (cellType){
                case CELL_TYPE_STRING:System.out.println("string類型");
                                      cellValue = cell.getStringCellValue();
                                       break;
                case CELL_TYPE_BOOLEAN:System.out.println("boolean類型");
                                       cellValue = String.valueOf(cell.getBooleanCellValue());
                                       break;
                case CELL_TYPE_NUMERIC:System.out.println("數字類型");
                                       if(HSSFDateUtil.isCellDateFormatted(cell)){
                                         System.out.println("日期");
                                         Date date = cell.getDateCellValue();
                                         cellValue =new DateTime(date).toString("yyyy-MM-dd");
                                       }
                                       else{
                                         cellValue = String.valueOf(cell.getNumericCellValue());
                                       }
                                       break;
                case CELL_TYPE_FORMULA:System.out.println("formula類型");
                                       break;
                case CELL_TYPE_BLANK:System.out.println("空");
                                        break;
                case CELL_TYPE_ERROR:System.out.println("類型錯誤");
                                     break;
              }
              System.out.print(cellValue+" ");
            }
          }
          System.out.println();
        }
      }
    }
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章