传来的数据
[ {"nameFirst": "test1", "nameSecond": "test1", "nameThird": "test1"}, {"nameFirst": "test2", "nameSecond": "test2", "nameThird": "test2"}]
文章来源地址https://www.toymoban.com/news/detail-814275.html
代码
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();
}
文章来源:https://www.toymoban.com/news/detail-814275.html
到了这里,关于把前端传来的数据导入到excel文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!