把前端传来的数据导入到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();
    }
相关推荐
铁皮哥1 天前
【力扣题解】LeetCode 25. K 个一组翻转链表
java·数据结构·windows·python·算法·leetcode·链表
小新同学^O^1 天前
简单学习 --> 单例模式
java·学习·多线程
Henray20241 天前
LRU缓存设计与实现
java·面试
甲方大人请饶命1 天前
SSM-基础
java·数据库·spring
谷雨不太卷1 天前
Linux基础IO
java·开发语言
小新同学^O^1 天前
简单学习 --> 文件IO
java·学习·文件io
吴声子夜歌1 天前
Java——Arrays
java·算法·排序算法
fanzhonghong1 天前
javaWeb开发之Maven高级
java·开发语言·spring boot·spring cloud·私服
xu_ws1 天前
spring通过三级缓存解决循环依赖
java·spring·缓存·循环依赖
Chase_______1 天前
Java 基础语言 ③:流程控制与数组——从条件分支到数组遍历,一篇通关
java·数据库·python