【示例】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 "";
        }
    }
}
相关推荐
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭24 分钟前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
超爱吃士力架1 小时前
邀请逻辑
java·linux·后端
AskHarries3 小时前
Spring Cloud OpenFeign快速入门demo
spring boot·后端
isolusion4 小时前
Springboot的创建方式
java·spring boot·后端
Yvemil74 小时前
《开启微服务之旅:Spring Boot Web开发举例》(一)
前端·spring boot·微服务
zjw_rp4 小时前
Spring-AOP
java·后端·spring·spring-aop
TodoCoder4 小时前
【编程思想】CopyOnWrite是如何解决高并发场景中的读写瓶颈?
java·后端·面试
凌虚5 小时前
Kubernetes APF(API 优先级和公平调度)简介
后端·程序员·kubernetes
星河梦瑾5 小时前
SpringBoot相关漏洞学习资料
java·经验分享·spring boot·安全
机器之心6 小时前
图学习新突破:一个统一框架连接空域和频域
人工智能·后端