You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Apache POI在Java中将XSSFSheet转换为List<POJO>

如何将XSSFSheet转换为POJO列表?

当然可以实现!我们可以借助Apache POI读取Excel内容,逐行映射到你的Sprint POJO类中,最终得到和表格数据行数一致的列表。下面是具体的实现方案:

1. 先确保依赖到位

如果是Maven项目,需要引入Apache POI的核心和OOXML依赖,用于读取XLSX格式的Excel:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.5</version>
</dependency>

2. 完整实现代码

结合你给出的Sprint类和文件上传方法,我们扩展出完整的转换逻辑:

你的Sprint POJO类(补充完整getter/setter)

public class Sprint {
    private String name;
    private Integer age;
    private String phone;

    // Getters and Setters
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public Integer getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }
    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }
}

扩展后的文件上传与转换方法

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExcelHandler {
    private static final String UPLOAD_LOCATION = "/your/upload/directory/";

    public String singleFileUpload(FileBucket fileBucket) throws IOException {
        MultipartFile multipartFile = fileBucket.getFile();
        // 保存上传的Excel文件到本地
        File uploadedFile = new File(UPLOAD_LOCATION + multipartFile.getOriginalFilename());
        FileCopyUtils.copy(multipartFile.getBytes(), uploadedFile);

        // 调用转换方法得到POJO列表
        List<Sprint> sprintList = convertSheetToSprintList(uploadedFile);
        // 这里可以添加对列表的后续操作,比如存入数据库、业务处理等
        System.out.println("转换完成,共得到" + sprintList.size() + "条数据");

        return "文件上传并转换成功,数据条数:" + sprintList.size();
    }

    private List<Sprint> convertSheetToSprintList(File excelFile) throws IOException {
        List<Sprint> sprintList = new ArrayList<>();
        Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFile));
        // 获取第一个Sheet,也可以通过名称获取:workbook.getSheet("Sheet1")
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();

        // 跳过表头行(如果你的Excel有表头的话,不需要就注释掉这部分)
        if (rowIterator.hasNext()) {
            rowIterator.next();
        }

        // 遍历每一行数据,映射到Sprint对象
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Sprint sprint = new Sprint();

            // 按单元格索引读取内容,对应Excel列顺序:name(0)、age(1)、phone(2)
            Cell nameCell = row.getCell(0);
            if (nameCell != null) {
                sprint.setName(nameCell.getStringCellValue());
            }

            Cell ageCell = row.getCell(1);
            if (ageCell != null) {
                // 处理数值类型单元格,转为Integer
                if (ageCell.getCellType() == CellType.NUMERIC) {
                    sprint.setAge((int) ageCell.getNumericCellValue());
                }
            }

            Cell phoneCell = row.getCell(2);
            if (phoneCell != null) {
                sprint.setPhone(phoneCell.getStringCellValue());
            }

            sprintList.add(sprint);
        }

        workbook.close();
        return sprintList;
    }
}

// 你的FileBucket类(假设结构)
class FileBucket {
    private MultipartFile file;
    // Getter and Setter
    public MultipartFile getFile() { return file; }
    public void setFile(MultipartFile file) { this.file = file; }
}

3. 关键注意点

  • 如果Excel列顺序可能变化,可以先读取表头,通过字段名映射到POJO属性,这种方式更灵活。
  • 注意处理不同类型的单元格(数值、字符串、空白等),避免类型转换异常。
  • 最终列表的大小等于Excel中的数据行数(如果跳过了表头),如果需要包含表头行,只需移除跳过表头的代码即可。

内容的提问来源于stack exchange,提问作者Samir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:27:49