【示例】SpringBoot读取Excel

ExcelController

java 复制代码
    @PostMapping("/dev/readExcel")
    public List<Map<String, Object>> readExcel(@RequestParam("cellHeader") String cellHeader, @RequestParam("file") MultipartFile file) {
        if (StringUtils.isEmpty(cellHeader)) {
            logger.info("列信息为空");
            return new ArrayList<>();
        }
        JSONArray cellHeaderInfoArray = JSONArray.parseArray(cellHeader);
        return ExcelUtil.readExcel(file, cellHeaderInfoArray);
    }

ExcelUtil

java 复制代码
public class ExcelUtil {

    private static Workbook getWorkbook(MultipartFile file) {
        Workbook workbook = null;
        try {
            String suffix = getFileSuffix(file);
            if ("xls".equals(suffix)) {
                workbook = new HSSFWorkbook(file.getInputStream());
            } else {
                workbook = new XSSFWorkbook(file.getInputStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }

    public static List<String> getCellFileIdList(JSONArray cellHeaderInfoArray) {
        List<String> fileIdList = new ArrayList<>();
        JSONObject cellHeaderInfo;
        for (int i = 0; i < cellHeaderInfoArray.size(); i++) {
            cellHeaderInfo = cellHeaderInfoArray.getJSONObject(i);
            fileIdList.add(cellHeaderInfo.getString("fileId"));
        }
        return fileIdList;
    }

    public static List<String> getCellFileNameList(JSONArray cellHeaderInfoArray) {
        List<String> fileNameList = new ArrayList<>();
        JSONObject cellHeaderInfo;
        for (int i = 0; i < cellHeaderInfoArray.size(); i++) {
            cellHeaderInfo = cellHeaderInfoArray.getJSONObject(i);
            fileNameList.add(cellHeaderInfo.getString("fileName"));
        }
        return fileNameList;
    }


    public static List<Map<String, Object>> readExcel(MultipartFile file,  JSONArray cellHeaderInfoArray) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> fileIdList;
        Workbook workbook = null;
        Sheet sheet = null;
        int firstRowNum, lastRowNum, cellNum;
        Row row;
        Cell cell;
        String fileId;
        //创建Excel,读取文件内容
        try {
            workbook = getWorkbook(file);
            //获取第一个工作表
            sheet = workbook.getSheetAt(0);
            //获取sheet中第一行行号(首行是0)
            firstRowNum = sheet.getFirstRowNum();
            //获取sheet中最后一行行号
            lastRowNum = sheet.getLastRowNum();
            //循环插入数据
            Map<String, Object> rowMap;
            fileIdList = getCellFileIdList(cellHeaderInfoArray);
            for (int i = firstRowNum + 1; i <= lastRowNum; i++) {
                rowMap = new HashMap<>();
                row = sheet.getRow(i);
                // 列数从1开始(只有一列有数据时,返回1)
                cellNum = row.getLastCellNum();
                if (cellNum > fileIdList.size()) {
                    cellNum = fileIdList.size();
                }
                for (int j = 0; j < cellNum; j++) {
                    cell = row.getCell(i);
                    fileId = fileIdList.get(0);
                    if (cell != null) {
                        if (cell.getCellType().equals(CellType.STRING)) {
                            rowMap.put(fileId, cell.getStringCellValue());
                        } else if (cell.getCellType().equals(CellType.NUMERIC)) {
                            rowMap.put(fileId, cell.getNumericCellValue());
                        } else {
                            rowMap.put(fileId, cell.getStringCellValue());
                        }
                    } else {
                        rowMap.put(fileId, "");
                    }
                }
                list.add(rowMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    private static String getFileSuffix(MultipartFile file) {
        String originalFilename = file.getOriginalFilename(); // 原始文件名(包含后缀)
        if (StringUtils.isEmpty(originalFilename)) {
            return "";
        }
        int index = originalFilename.lastIndexOf('.'); // 最后一个点号的索引位置
        if (index != -1 && index < originalFilename.length() - 1) {
            return originalFilename.substring(index + 1); // 提取后缀部分
        } else {
            return "";
        }
    }
}
相关推荐
Pandaconda13 分钟前
【Golang 面试题】每日 3 题(三十九)
开发语言·经验分享·笔记·后端·面试·golang·go
是梦终空15 分钟前
JAVA毕业设计210—基于Java+Springboot+vue3的中国历史文化街区管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·历史文化街区管理·景区管理
荆州克莱20 分钟前
Golang的图形编程基础
spring boot·spring·spring cloud·css3·技术
m0_7482350731 分钟前
springboot中配置logback-spring.xml
spring boot·spring·logback
m0_5127446440 分钟前
springboot使用logback自定义日志
java·spring boot·logback
编程小筑1 小时前
R语言的编程范式
开发语言·后端·golang
技术的探险家1 小时前
Elixir语言的文件操作
开发语言·后端·golang
ss2731 小时前
【2025小年源码免费送】
前端·后端
Ai 编码助手1 小时前
Golang 中强大的重试机制,解决瞬态错误
开发语言·后端·golang
齐雅彤2 小时前
Lisp语言的区块链
开发语言·后端·golang