ExcelController

    @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

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

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部