把前端传来的数据导入到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();
    }
相关推荐
拾忆,想起9 分钟前
Dubbo异步调用实战指南:提升微服务并发性能
java·服务器·网络协议·微服务·云原生·架构·dubbo
q***318923 分钟前
微服务生态组件之Spring Cloud LoadBalancer详解和源码分析
java·spring cloud·微服务
敏姐的后花园3 小时前
模考倒计时网页版
java·服务器·前端
gCode Teacher 格码致知4 小时前
Python基础教学:Python的openpyxl和python-docx模块结合Excel和Word模板进行数据写入-由Deepseek产生
python·excel
Dcs5 小时前
Java 中 UnaryOperator 接口与 Lambda 表达式的应用示例
java·后端
bagadesu7 小时前
使用Docker构建Node.js应用的详细指南
java·后端
没有bug.的程序员7 小时前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud
洛_尘8 小时前
JAVA EE初阶 2: 多线程-初阶
java·开发语言
Slow菜鸟8 小时前
Java 开发环境安装指南(五) | Git 安装
java·git