Apache POI 操作Excel表格

1、Apache POI介绍

Apache POI 是一个处理Miscrosoft Office文件格式的开源项目,在Java中可以用来对Miscrosoft Office的各种文件进行读写操作,本文是介绍Apache POI操作Excel的基础案例。

2、Apache POI操作Excel

  • 引入pom依赖

    <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.0.1</version> </dependency>
  • 编写测试代码

说明:excel文件有两个版本,分为2003版本和2007版本,区别在于文件的后缀,分别对应xls和xlsx,操作时对应的实现类不一样,分别是HSSFWorkbook和XSSFWorkbook,其它用法都一样。

java 复制代码
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@SpringBootTest
class TestApachePOI {

    /**
     * 读取Excel表格所有数据
     */
    @Test
    public void readAllRowTest() throws IOException {
        // 1. 读取Excel资源的输入流,路径是当前文件的相对路径
        InputStream inputStream = TestApachePOI.class.getResourceAsStream("/excel/上传货物模板2.xls");


        // 2. 创建一个工作铺
        // 注意:03版本(.xls后缀)的Excel对应的实现类是 HSSFWorkbook
        //      04版本(.xlsx后缀)的Excel对应的实现类是 XSSFWorkbook
        Workbook workbook = new HSSFWorkbook(inputStream);

        // 3. 获取一个工作单位
        Sheet sheet = workbook.getSheetAt(0);

        // 4. 获取一行
        Row row = sheet.getRow(0);

        // 5. 得到单元格
        System.out.print(row.getCell(1).getStringCellValue() + "\t");
        System.out.print(row.getCell(2).getStringCellValue() + "\t");
        System.out.print(row.getCell(3).getStringCellValue() + "\t");
        System.out.print(row.getCell(4).getStringCellValue() + "\t");
        System.out.print(row.getCell(5).getStringCellValue() + "\t");
        System.out.print(row.getCell(6).getStringCellValue() + "\t");
        System.out.print(row.getCell(7).getStringCellValue() + "\t");
        System.out.print(row.getCell(8).getStringCellValue() + "\t");
        System.out.print(row.getCell(9).getStringCellValue() + "\t");
        System.out.println();

        // 得到excel表格的所有行数
        int rows = sheet.getPhysicalNumberOfRows();
        // 遍历读取其它所有行数
        for (int i = 1; i < rows; i++) {
            // 得到行
            row = sheet.getRow(i);
            // 输入单元格数据
            System.out.print(row.getCell(1).getStringCellValue() + "\t");
            System.out.print(row.getCell(2).getStringCellValue() + "\t");
            System.out.print(row.getCell(3).getNumericCellValue() + "\t");
            System.out.print(row.getCell(4).getStringCellValue() + "\t");
            System.out.print(row.getCell(5).getNumericCellValue() + "\t");
            System.out.print(row.getCell(6).getNumericCellValue() + "\t");
            System.out.print(row.getCell(7).getNumericCellValue() + "\t");
            System.out.print(row.getCell(8).getStringCellValue() + "\t");
            System.out.print(row.getCell(9).getStringCellValue() + "\t");
            System.out.println();
        }

    }

    /**
     *  创建表格并且写入数据 
     */
    @Test
    public void createExcel() throws Exception {
        // 1. 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 2. 创建工作单元
        Sheet sheet = workbook.createSheet("最酷男生表");
        // 3. 创建行
        Row row = sheet.createRow(0);
        // 4. 创建单元格并设置内容
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("年龄");
        row.createCell(2).setCellValue("身高");
        row.createCell(3).setCellValue("收入");
        row.createCell(4).setCellValue("人品");
        // 5. 写出工作簿
        workbook.write(new FileOutputStream("E:/contact.xlsx"));
    }


}
  • 代码中读取的表格数据
相关推荐
初学小白...1 天前
泛型-泛型方法
java·开发语言
LQ深蹲不写BUG1 天前
深挖三色标记算法的底层原理
java·算法
上官浩仁1 天前
springboot knife4j 接口文档入门与实战
java·spring boot·spring
启山智软1 天前
商城源码后端性能优化:JVM 参数调优与内存泄漏排查实战
java·电子商务·商城开发
wuk9981 天前
在Spring MVC中使用查询字符串与参数
java·spring·mvc
YXWik61 天前
java 使用 spring AI 实战 RAG (Chroma 向量数据库+Advisor)
java·人工智能·spring
间彧1 天前
Stream.collect(Collectors.toList())和Stream.toList()
java
We....1 天前
Java集合---Collection接口和Map接口
java·开发语言
程序员鱼皮1 天前
Java 8 终于要被淘汰了!带你速通 Java 8~24 新特性 | 又能跟面试官吹牛皮了
java·后端·程序员
蓝倾9761 天前
1688拍立淘接口对接实战案例
java·开发语言·数据库·python·电商开放平台·开放api接口