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


}
  • 代码中读取的表格数据
相关推荐
·云扬·7 分钟前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
Bug退退退1231 小时前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
小皮侠1 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github
Zz_waiting.1 小时前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象
全栈凯哥1 小时前
02.SpringBoot常用Utils工具类详解
java·spring boot·后端
兮动人1 小时前
获取终端外网IP地址
java·网络·网络协议·tcp/ip·获取终端外网ip地址
呆呆的小鳄鱼1 小时前
cin,cin.get()等异同点[面试题系列]
java·算法·面试
独立开阀者_FwtCoder2 小时前
"页面白屏了?别慌!前端工程师必备的排查技巧和面试攻略"
java·前端·javascript
Touper.2 小时前
JavaSE -- 泛型详细介绍
java·开发语言·算法
静若繁花_jingjing2 小时前
Redis线程模型
java·数据库·redis