当前位置:   article > 正文

EasyExcel导出(单Sheet导出、多Sheet导出、多文件导出)_easyexcel导出文件

easyexcel导出文件

EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel

一、单Sheet导出

  1. @ApiOperation("对话数据导出(单话题)")
  2. @PostMapping("downloadByChatDialogueId")
  3. public void downloadByChatDialogueId(@RequestParam String chatDialogueId, HttpServletResponse response) throws IOException {
  4. // 话题基本信息
  5. ChatDialogue dialogue = chatDialogueService.getById(chatDialogueId);
  6. // 对话数据列表
  7. Result<List<ChatDialogueMappingMagic>> listResult = getById(chatDialogueId);
  8. List<ChatDialogueMappingMagic> mappingMagicList = listResult.getData();
  9. List<ChatDialogueMappingExport> exportList = converter.convert(mappingMagicList, ChatDialogueMappingExport.class);
  10. // 设置响应头信息
  11. response.setContentType("application/vnd.ms-excel");
  12. response.setCharacterEncoding("utf-8");
  13. response.setHeader("Content-disposition", "attachment;filename=chatEduExport.xlsx");
  14. // 使用EasyExcel进行导出
  15. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), ChatDialogueMappingExport.class).build();
  16. WriteSheet writeSheet = EasyExcel.writerSheet(dialogue.getTitle()).build();
  17. excelWriter.write(exportList, writeSheet);
  18. excelWriter.finish();
  19. }

二、多Sheet导出

  1. @ApiOperation("对话数据导出(单用户)")
  2. @PostMapping("downloadByUserId")
  3. public void downloadByUserId( @RequestParam Integer userId, HttpServletResponse response) throws IOException {
  4. ChatUser chatUser = chatUserService.getById(userId);
  5. Assert.notNull(chatUser, "用户不存在");
  6. // 设置响应头信息
  7. response.setContentType("application/vnd.ms-excel");
  8. response.setCharacterEncoding("utf-8");
  9. response.setHeader("Content-disposition", "attachment;filename=chatEduExport.xlsx");
  10. // 使用EasyExcel进行导出
  11. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), ChatDialogueMappingExport.class).build();
  12. // 查出用户的所有话题
  13. List<ChatDialogue> dialogueList = chatDialogueService.lambdaQuery()
  14. .eq(ChatDialogue::getUserId, userId)
  15. .orderByDesc(ChatDialogue::getChatDialogueFunctionUsedId)
  16. .list();
  17. // 循环写入多个Sheet
  18. for (ChatDialogue dialogue : dialogueList) {
  19. Integer functionUsedId = dialogue.getChatDialogueFunctionUsedId();
  20. if (functionUsedId == null) {
  21. continue;
  22. }
  23. ChatDialogueFunctionUsed functionUsed = chatDialogueFunctionUsedService.getById(functionUsedId);
  24. if (functionUsed == null) {
  25. continue;
  26. }
  27. // 对话数据列表
  28. Result<List<ChatDialogueMappingMagic>> listResult = getById(dialogue.getId());
  29. List<ChatDialogueMappingMagic> mappingMagicList = listResult.getData();
  30. List<ChatDialogueMappingExport> exportList = converter.convert(mappingMagicList, ChatDialogueMappingExport.class);
  31. WriteSheet writeSheet = EasyExcel.writerSheet("(" + functionUsed.getTitle() + ")" + dialogue.getTitle()).build();
  32. excelWriter.write(exportList, writeSheet);
  33. }
  34. excelWriter.finish();
  35. }

三、多文件导出

  1. @ApiOperation("对话数据导出(所有用户)")
  2. @PostMapping("downloadAll")
  3. public void downloadAll(HttpServletResponse response) throws IOException {
  4. // 所有用户
  5. List<ChatUser> chatUsers = chatUserService.list();
  6. response.setContentType("application/zip");
  7. response.setCharacterEncoding("utf-8");
  8. response.setHeader("Content-disposition", "attachment;filename=chatEduExports.zip");
  9. ServletOutputStream outputStream = response.getOutputStream();
  10. ZipOutputStream zipOut = new ZipOutputStream(outputStream);
  11. // 循环写入多个文件
  12. for (ChatUser chatUser : chatUsers) {
  13. // 查出用户的所有话题
  14. List<ChatDialogue> dialogueList = chatDialogueService.lambdaQuery()
  15. .eq(ChatDialogue::getUserId, chatUser.getId())
  16. .isNotNull(ChatDialogue::getChatDialogueFunctionUsedId)
  17. .orderByDesc(ChatDialogue::getChatDialogueFunctionUsedId)
  18. .list();
  19. if (dialogueList.size() == 0) {
  20. continue;
  21. }
  22. // 创建一个临时的ByteArrayOutputStream用于存储生成的Excel文件数据
  23. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  24. // 使用EasyExcel进行导出
  25. ExcelWriter excelWriter = EasyExcel.write(byteArrayOutputStream, ChatDialogueMappingExport.class).build();
  26. // 循环写入多个Sheet
  27. for (ChatDialogue dialogue : dialogueList) {
  28. Integer functionUsedId = dialogue.getChatDialogueFunctionUsedId();
  29. if (functionUsedId == null) {
  30. continue;
  31. }
  32. ChatDialogueFunctionUsed functionUsed = chatDialogueFunctionUsedService.getById(functionUsedId);
  33. if (functionUsed == null) {
  34. continue;
  35. }
  36. // 对话数据列表
  37. Result<List<ChatDialogueMappingMagic>> listResult = getById(dialogue.getId());
  38. List<ChatDialogueMappingMagic> mappingMagicList = listResult.getData();
  39. List<ChatDialogueMappingExport> exportList = converter.convert(mappingMagicList, ChatDialogueMappingExport.class);
  40. WriteSheet writeSheet = EasyExcel.writerSheet(IdUtil.fastUUID()).build();
  41. excelWriter.write(exportList, writeSheet);
  42. }
  43. excelWriter.finish();
  44. // 创建文件
  45. zipOut.putNextEntry(new ZipEntry(chatUser.getUsername()+".xlsx"));
  46. zipOut.write(byteArrayOutputStream.toByteArray());
  47. zipOut.closeEntry();
  48. // 关闭临时的ByteArrayOutputStream
  49. byteArrayOutputStream.close();
  50. }
  51. zipOut.finish();
  52. zipOut.close();
  53. outputStream.flush();
  54. outputStream.close();
  55. }

四、扫一扫下方微信名片即可+博主徽信哦 ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓  ↓↓↓

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/216044
推荐阅读
相关标签
  

闽ICP备14008679号