读取excel不填充数据

    @PostMapping("/analyses/excel")
    public AjaxResult selectByconfig(@RequestParam(required = false) Integer configId) {
        System.out.println(configId);
        try {
            // 根据 configId 查询 ChartConfigs 对象
            ChartConfigs configs = chartConfigsService.getById(configId);
            if (configs == null) {
                return AjaxResult.error("配置对象不存在");
            }

            // 构造完整的 excelurl
            String templateUrl = configs.getTemplateUrl();
            String serverUrl = "https://www.topcharts.cn";
            String excelurl = serverUrl + templateUrl;
            System.out.println(excelurl);

            // 使用 Apache POI 读取 Excel 文件内容
            Workbook workbook = null;
            InputStream inputStream = null;
            try {
                URL url = new URL(excelurl);
                inputStream = url.openStream();
                workbook = new XSSFWorkbook(inputStream); // 使用 XSSFWorkbook 对象打开输入流
                // 假设只有一个工作表
                Sheet sheet = workbook.getSheetAt(0);
                // 读取 Excel 数据,从第9行开始读取
                List<Map<String, String>> data = readExcelData(sheet, 8); // 从第9行开始读取数据

                // 返回 AjaxResult 封装的数据
                return AjaxResult.success(data);
            } finally {
                if (workbook != null) {
                    workbook.close(); // 关闭工作簿
                }
                if (inputStream != null) {
                    inputStream.close(); // 关闭输入流
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            String errorMsg = (e.getMessage() != null) ? e.getMessage() : "null";
            return AjaxResult.error("读取 Excel 文件出现异常:" + errorMsg);
        }
    }
    private List<Map<String, String>> readExcelData(Sheet sheet, int startRow) {
        List<Map<String, String>> data = new ArrayList<>();
        try {
            // 遍历行
            for (int i = startRow; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue; // 如果行为空,跳过
                }

                Map<String, String> rowData = new LinkedHashMap<>();
                // 遍历单元格
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    String cellValue = "";
                    if (cell != null) {
                        CellType cellType = cell.getCellType();
                        if (cellType == CellType.STRING) {
                            cellValue = cell.getStringCellValue();
                        } else if (cellType == CellType.NUMERIC) {
                            cellValue = String.valueOf(cell.getNumericCellValue());
                        }
                    }
                    rowData.put("column" + (j + 1), cellValue);
                }
                // 将一行数据添加到整体数据列表中
                data.add(rowData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值