把前端传来的数据导入到excel文件

传来的数据

{"nameFirst": "test1", "nameSecond": "test1", "nameThird": "test1"}, {"nameFirst": "test2", "nameSecond": "test2", "nameThird": "test2"}

代码

controller

java 复制代码
@GetMapping("/excel/fail")  
@Operation(summary = "把失败数据打印成excel导出")  
public void exportFailedData(@RequestParam String jsonData, HttpServletResponse response) throws IOException {  
    ObjectMapper objectMapper = new ObjectMapper();  
    List<Map<String, String>> list = objectMapper.readValue(jsonData, new TypeReference<List<Map<String, String>>>() {});  
    technologyCategoryService.exportFailedData(list, response);  
}

service

java 复制代码
public void exportFailedData(List<Map<String, String>> list, HttpServletResponse response) throws IOException {
        Workbook workbook = new XSSFWorkbook();

        // 获取Sheet对象
        Sheet sheet = workbook.createSheet("失败数据"); // 只有一个Sheet

        // 设置标题行样式
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("一级分类");
        headerRow.createCell(1).setCellValue("二级分类");
        headerRow.createCell(2).setCellValue("三级分类");

        int rowNum = 1;
        for (Map<String, String> rowData : list) {
            if (rowData.size() >= 3) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(rowData.get("nameFirst"));
                row.createCell(1).setCellValue(rowData.get("nameSecond"));
                row.createCell(2).setCellValue(rowData.get("nameThird"));
            }
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=technology_category_failed_data.xlsx");

        try (OutputStream outputStream = response.getOutputStream()) {
            workbook.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        workbook.close();
    }
相关推荐
皮皮林5517 小时前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot
顺风尿一寸11 小时前
从 Java NIO poll 到 Linux 内核 poll:一次系统调用的完整旅程
java
程途知微11 小时前
JVM运行时数据区各区域作用与溢出原理
java
华仔啊13 小时前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
xiaoye201815 小时前
Lettuce连接模型、命令执行、Pipeline 浅析
java
beata18 小时前
Java基础-18:Java开发中的常用设计模式:深入解析与实战应用
java·后端
Seven9719 小时前
剑指offer-81、⼆叉搜索树的最近公共祖先
java
雨中飘荡的记忆2 天前
保证金系统入门到实战
java·后端
Nyarlathotep01132 天前
Java内存模型
java