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

传来的数据

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

代码

controller

@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

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. 前端传来数据导入excel文件

    2024-01-19 08:26:02       33 阅读
  2. C#excel 数据导入sqlserver

    2024-01-19 08:26:02       21 阅读
  3. excel数据导入数据库方法

    2024-01-19 08:26:02       21 阅读
  4. excel xla文件怎么导入excel

    2024-01-19 08:26:02       42 阅读
  5. Python怎么数据从CSV文件导入MySQL数据库

    2024-01-19 08:26:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-19 08:26:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-19 08:26:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-19 08:26:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-19 08:26:02       20 阅读

热门阅读

  1. 软件工程客观题知识点简易汇总

    2024-01-19 08:26:02       35 阅读
  2. MongoDB面试系列-03

    2024-01-19 08:26:02       29 阅读
  3. C语言整型常量的存储形式是怎样的?

    2024-01-19 08:26:02       34 阅读
  4. Js面试之作用域与闭包

    2024-01-19 08:26:02       35 阅读
  5. STL-string

    2024-01-19 08:26:02       31 阅读
  6. c++学习之特殊类设计与类型转换

    2024-01-19 08:26:02       36 阅读
  7. Qt中ListWidget控件总结

    2024-01-19 08:26:02       40 阅读
  8. ArrayList和LinkedList的区别

    2024-01-19 08:26:02       38 阅读
  9. MATLAB Fundamentals>>>Creating Datetimes

    2024-01-19 08:26:02       33 阅读
  10. linux安装hadoop详细步骤

    2024-01-19 08:26:02       40 阅读
  11. SOH 均衡技术发展方向及建议

    2024-01-19 08:26:02       32 阅读