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"));
    }


}
  • 代码中读取的表格数据
相关推荐
面朝大海,春不暖,花不开16 分钟前
自定义Spring Boot Starter的全面指南
java·spring boot·后端
得过且过的勇者y16 分钟前
Java安全点safepoint
java
夜晚回家1 小时前
「Java基本语法」代码格式与注释规范
java·开发语言
斯普信云原生组1 小时前
Docker构建自定义的镜像
java·spring cloud·docker
wangjinjin1801 小时前
使用 IntelliJ IDEA 安装通义灵码(TONGYI Lingma)插件,进行后端 Java Spring Boot 项目的用户用例生成及常见问题处理
java·spring boot·intellij-idea
wtg44521 小时前
使用 Rest-Assured 和 TestNG 进行购物车功能的 API 自动化测试
java
白宇横流学长1 小时前
基于SpringBoot实现的大创管理系统设计与实现【源码+文档】
java·spring boot·后端
fat house cat_2 小时前
【redis】线程IO模型
java·redis
stein_java3 小时前
springMVC-10验证及国际化
java·spring
weixin_478689763 小时前
C++ 对 C 的兼容性
java·c语言·c++