把前端传来的数据导入到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();
    }
相关推荐
马猴烧酒.2 分钟前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
sino爱学习38 分钟前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端
风生u1 小时前
activiti7 详解
java
岁岁种桃花儿1 小时前
SpringCloud从入门到上天:Nacos做微服务注册中心(二)
java·spring cloud·微服务
Word码1 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
TT哇1 小时前
【实习 】银行经理端两个核心功能的开发与修复(银行经理绑定逻辑修复和线下领取扫码功能开发)
java·vue.js
逝水如流年轻往返染尘2 小时前
Java中的数组
java
java1234_小锋2 小时前
Java高频面试题:BIO、NIO、AIO有什么区别?
java·面试·nio
用户8307196840822 小时前
Java IO三大模型(BIO/NIO/AIO)超详细总结
java
sheji34162 小时前
【开题答辩全过程】以 基于SSM的花店销售管理系统为例,包含答辩的问题和答案
java