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


}
  • 代码中读取的表格数据
相关推荐
非 白几秒前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼6 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒9 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
Forget the Dream38 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek41 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
帅的飞起来2 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式
張葒兵3 小时前
记一次命令行启动springboot项目的问题 java -jar的问题
java·spring boot·jar