相关依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

一、 POI简介

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

二、POI有三种API:

  • POI-HSSF
  • POI-XSSF
  • SXSSF
  1. HSSF是POI项目的Excel 2003(.xls)文件格式的纯Java实现。。
    (长度65536,读写快)
  2. XSSF是POI Project的Excel 2007 OOXML(.xlsx)文件格式的纯Java实现。
    (长度不限,但是当进行大数据的读写,会很慢)
  3. SXSSF是在XSSF的基础上,支持导出大批量的excel数据。
    (长度不限,读写快)

三、HSSF写:

    @Test
    public void WritePOITest() throws IOException {
        //创建工作簿,对应整个xls文件
        Workbook workbook = new HSSFWorkbook();
        //创建sheet工作表,对应excel的单个sheet
        Sheet sheet = workbook.createSheet("第一个工作簿03版");
        //创建行,对应excel中的一行
        Row row = sheet.createRow(0);
        //创建单元格,对应row中的一格。即:‘列’
        Cell cell = row.createCell(0);
        //单元格设置值
        cell.setCellValue("张三");
        //创建单元格,第一行的第二格
        Cell cell1 = row.createCell(1);
        //单元格设置值
        cell1.setCellValue(666);
        //保存工作簿
        FileOutputStream outputStream = new FileOutputStream("D:\\workspaceIdea\\threadStudy\\工作簿03版.xls");
        workbook.write(outputStream);
        outputStream.close();
    }

HSSF读:

简单类型的读取(String)
image.png

    String path = "D:\\workspaceIdea\\threadStudy\\";
    @Test
    public void ReadHSSFTest() throws IOException {
        FileInputStream file = new FileInputStream(path+"工作簿HSSF.xls");
        //创建工作簿,对应整个xls文件
        Workbook workbook = new HSSFWorkbook(file);
        //拿到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
        Cell cell2 = row.getCell(1);
        System.out.println(cell2.getStringCellValue());
    }

image.png

四、XSSF写:

    @Test
    public void WritePOITest() throws IOException {
        //创建工作簿,对应整个xls文件
        Workbook workbook = new XSSFWorkbook();
        //创建sheet工作表,对应excel的单个sheet
        Sheet sheet = workbook.createSheet("第一个工作簿07版");
        //创建行,对应excel中的一行
        Row row = sheet.createRow(0);
        //创建单元格,对应row中的一格。即:‘列’
        Cell cell = row.createCell(0);
        //单元格设置值
        cell.setCellValue("张三");
        //创建单元格,第一行的第二格
        Cell cell1 = row.createCell(1);
        //单元格设置值
        cell1.setCellValue(666);
        //保存工作簿
        FileOutputStream outputStream = new FileOutputStream("D:\\workspaceIdea\\threadStudy\\工作簿07版.xlsx");
        workbook.write(outputStream);
        outputStream.close();
    }

XSSF读:

@Test
public void ReadXSSFTest() throws IOException {
    FileInputStream file = new FileInputStream(path+"工作簿XSSF.xlsx");
    //创建工作簿,对应整个xls文件
    Workbook workbook = new XSSFWorkbook(file);
    //拿到第一个工作表
    Sheet sheet = workbook.getSheetAt(0);
    Row row = sheet.getRow(0);
    Cell cell = row.getCell(0);
    System.out.println(cell.getStringCellValue());
    Cell cell2 = row.getCell(1);
    System.out.println(cell2.getNumericCellValue());
    file.close();
}

五、复杂数据类型读取(HSSF为例):

package cn.hm1006.ExcelDemo.POI;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ReadPOITest {
    String path = "D:\\workspaceIdea\\threadStudy\\";
    //HSSF读
    @Test
    public void ReadHSSFTest() throws IOException {
        FileInputStream file = new FileInputStream(path+"工作簿HSSF.xls");
        //创建工作簿,对应整个xls文件
        Workbook workbook = new HSSFWorkbook(file);
        //拿到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
        Cell cell2 = row.getCell(1);
        System.out.println(cell2.getStringCellValue());
        file.close();
    }
    //XSSF读
    @Test
    public void ReadXSSFTest() throws IOException {
        FileInputStream file = new FileInputStream(path+"工作簿XSSF.xlsx");
        //创建工作簿,对应整个xls文件
        Workbook workbook = new XSSFWorkbook(file);
        //拿到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        System.out.println(cell.getStringCellValue());
        Cell cell2 = row.getCell(1);
        System.out.println(cell2.getNumericCellValue());
        file.close();
    }
    //HSSF读 ---- 复杂类型读取
    @Test
    public void ReadHSSF() throws IOException {
        FileInputStream file = new FileInputStream(path+"工作簿HSSF.xls");
        //创建工作簿,对应整个xls文件
        Workbook workbook = new HSSFWorkbook(file);
        //拿到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        Row rowTitle = sheet.getRow(0);
        //读取表中的标题
        if (rowTitle!=null){
            //拿到一共多少列
            int callCount = rowTitle.getPhysicalNumberOfCells();
            System.out.println("共:"+callCount+"列");
            for (int i = 0; i < callCount; i++) {
                //拿到列
                Cell cell = rowTitle.getCell(i);
                if (cell!=null){
                    CellType cellType = cell.getCellType();
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue+"|");
                }
            }
            System.out.println();
        }
        //读取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();//获取表中的总行数
        System.out.println("总行数:"+rowCount);
        //从1开始计算,0是表中标题所在行
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            Row rowDate = sheet.getRow(rowNum);//拿到行数据
            if(rowDate!=null){
                int cellCount = rowTitle.getPhysicalNumberOfCells();//读取列
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    Cell cell = rowDate.getCell(cellNum);
                    if(cell!=null){
                        CellType cellType = cell.getCellType();//拿到列的类型
                        String cellValue = "";
                        switch (cellType){
                            case NUMERIC:
                                System.out.println("[数字类型]");
                                if (HSSFDateUtil.isCellDateFormatted(cell)){
                                    System.out.println("[日期类型]");
                                    Date date = cell.getDateCellValue();
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                    cellValue = sdf.format(date);
                                }else {
                                    //将数字转换为字符
                                    System.out.println("[数字转换为字符]");
                                    cell.setCellType(CellType.STRING);
                                    cellValue = cell.toString();
                                }
                                break;
                            case STRING://String
                                System.out.println("[字符类型]");
                                cellValue = cell.getStringCellValue();
                                break;
                            case BLANK://值不存在时,为空
                                break;
                            case BOOLEAN://布尔
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case ERROR://数据类型错误
                                System.out.println("[数据类型错误-ERROR]");
                                break;
                        }
                        System.out.println("---------"+cellValue+"---------");
                    }
                }
            }
        }
        file.close();
    }
}

五、计算公式FORMULA(HSSF为例):

image.png

    public void ReadHSSFNum() throws IOException {
        FileInputStream file = new FileInputStream(path + "工作簿HSSF.xls");
        //创建工作簿,对应整个xls文件
        Workbook workbook = new HSSFWorkbook(file);
        //拿到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(11);
        Cell cell = row.getCell(4);

        FormulaEvaluator FormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
        CellType cellType = cell.getCellType();
        switch (cellType){
            case FORMULA://公式
                String cellFormula = cell.getCellFormula();
                System.out.println(cellFormula);//输出公式
                CellValue evaluate = FormulaEvaluator.evaluate(cell);
                System.out.println(evaluate.formatAsString());//输出计算结果
                break;
        }
    }

image.png

Q.E.D.


如人饮水、冷暖自知