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


}
  • 代码中读取的表格数据
相关推荐
pianmian129 分钟前
类(JavaBean类)和对象
java
我叫小白菜1 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
Albert Edison2 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
超级小忍2 小时前
JVM 中的垃圾回收算法及垃圾回收器详解
java·jvm
weixin_446122462 小时前
JAVA内存区域划分
java·开发语言·redis
勤奋的小王同学~2 小时前
(javaEE初阶)计算机是如何组成的:CPU基本工作流程 CPU介绍 CPU执行指令的流程 寄存器 程序 进程 进程控制块 线程 线程的执行
java·java-ee
TT哇3 小时前
JavaEE==网站开发
java·redis·java-ee
2401_826097623 小时前
JavaEE-Linux环境部署
java·linux·java-ee
缘来是庄3 小时前
设计模式之访问者模式
java·设计模式·访问者模式
Bug退退退1234 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq