Java架構-Apache POI Excel

相信在項目中,對數據進行動態導出這是一個比較常見的功能。對於數據導出咱們可使用Apache-POI這個框架來幫我來進行Excel的寫入與讀取。下面就用代碼來實現Apache POI寫入與讀取excel文件。java

一、Apache POI基本概念apache

下面將簡單的描述一下當進行Excel讀取與寫入的時候要使用到的基本類。bash

  1. HSSF 爲前綴的類名錶示操做的是Microsoft Excel 2003文件。
  2. XSSF 爲前綴的類名錶示操做的是Microsoft Excel 2007或之後的版本
  3. XSSFWorkbook 和 HSSFWorkbook表示一個Excel的Workbook.
  4. HSSFSheet 和 XSSFSheet 表示一個Excel的Worksheet.
  5. Row 表示一個Excel行
  6. Cell 表示當前Row中一個Cell.

二、下載Apache POI架構

在項目中是使用Maven來管理Jar依賴的,因此在Pom.xml添加如下依賴:框架

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.15</version>
</dependency>
複製代碼

三、寫入一個Excel文件xss

下面的代碼將會簡單的展現使用Apache POI寫入一個Excel文件。數據將會寫入到XSSFWorkbook對象中。學習

ApachePOIExcelWrite.java
複製代碼
package com.weimob.o2o.carl.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ApachePOIExcelWrite {

    private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";

    public static void main(String[] args) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Data types in Java");
        Object[][] dataTypes = {
                {"DataType", "Type", "Size(in bytes)"},
                {"int", "Primitive", 2},
                {"float", "Primitive", 4},
                {"double", "Primitive", 8},
                {"char", "Primitive", 1},
                {"String", "Non-Primitive", "No fixed size"}
        };

        int rowNum = 0;
        System.out.println("Creating excel");
        for(Object[] dataType : dataTypes){
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for(Object field : dataType){
                Cell cell = row.createCell(colNum++);
                if(field instanceof String){
                    cell.setCellValue((String) field);
                } else if (field instanceof Integer){
                    cell.setCellValue((Integer) field);
                }
            }
        }
        try {
            FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
            workbook.write(outputStream);
            workbook.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Done");

    }

}

複製代碼

你將會在你項目所在的磁盤中的tmp文件夾中獲得如下的excel文件:spa

四、讀取一個Excel文件3d

下面的代碼展現如何使用Apache POI讀取Excel文件。getCellTypeEnum方法在 3.15 中不推薦使用而且會在 4.0 版本中將會更名爲:getCellType.excel

ApachePOIExcelRead.java
複製代碼
package com.weimob.o2o.carl.poi;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

public class ApachePOIExcelRead {

    private static final String FILE_NAME = "/tmp/MyFirstExcel.xlsx";

    public static void main(String[] args) {
        try {
            FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet dataTypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = dataTypeSheet.iterator();
            while(iterator.hasNext()){
                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();
                while(cellIterator.hasNext()){
                    Cell currentCell = cellIterator.next();
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    if(currentCell.getCellTypeEnum() == CellType.STRING){
                        System.out.print(currentCell.getStringCellValue() + "--");
                    } else if(currentCell.getCellTypeEnum() == CellType.NUMERIC){
                        System.out.print(currentCell.getNumericCellValue() + "--");
                    }
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
複製代碼

你的控制檯將會輸出如下代碼:

爲何某些人會一直比你優秀,是由於他自己就很優秀還一直在持續努力變得更優秀,而你是否是還在知足於現狀心裏在竊喜!

合理利用本身每一分每一秒的時間來學習提高本身,不要再用"沒有時間「來掩飾本身思想上的懶惰!趁年輕,使勁拼,給將來的本身一個交代!

To-陌霖Java架構
複製代碼

分享互聯網最新文章 關注互聯網最新發展

相關文章
相關標籤/搜索