格式如下:

  1. HttpServletResponse response = RequestAndResponseUtils.getResponse();
  2. HttpServletResponse response = RequestAndResponseUtils.getResponse();
  3. ExcelUtils.setResponseInfo(response, FILE_NAME+ System.currentTimeMillis()+XLSX);
  4. ServletOutputStream outputStream = response.getOutputStream();
  5. ExcelWriter excelWriter = EasyExcelFactory
  6. .write(outputStream)
  7. .build();
  8. WriteSheet writeSheet = null;
  9. for (int i : 5) {
  10. scProcesses.setType(key);
  11. //获取头部
  12. List<List<String>> head = new ArrayList<>();
  13. //获取策略
  14. List<WriteHandler> strategy = new ArrayList<>();
  15. //数据
  16. List data = new ArrayList<>() ;
  17. ExcelWriterSheetBuilder writerSheetBuilder = EasyExcelFactory
  18. .writerSheet(i, "sheet名称"+i)
  19. .needHead(true)
  20. .head(head);
  21. //设置策略
  22. if (!CollectionUtils.isEmpty(strategy)){
  23. for (WriteHandler writeHandler : strategy) {
  24. writerSheetBuilder.registerWriteHandler(writeHandler);
  25. }
  26. }
  27. writeSheet =writerSheetBuilder.build();
  28. //写入sheet
  29. excelWriter.write(data),writeSheet);
  30. }
  1. @Service("entiretyExcelExportService")
  2. @Slf4j
  3. public class EntiretyExcelExportServiceImpl implements EntiretyExcelExportService {
  4. @Resource
  5. private ExcelExpHandleFactory factory;
  6. /**
  7. * 导出的excel文件名称
  8. */
  9. private static final String FILE_NAME = "bia报表";
  10. private static final String XLSX = ".xlsx";
  11. /**
  12. * @param planId 计划id
  13. * @author zhuyijun
  14. * @date 2021/12/20 9:47
  15. * @return void
  16. */
  17. @SuppressWarnings("unchecked")
  18. @Override
  19. public void doExcelExp(String planId) {
  20. if (StringUtils.isEmpty(planId)){
  21. throw new HatechException("计划id不能为空");
  22. }
  23. ExcelExpProcessDTO scProcesses = new ExcelExpProcessDTO();
  24. scProcesses.setPlanId(planId);
  25. HttpServletResponse response = RequestAndResponseUtils.getResponse();
  26. ExcelUtils.setResponseInfo(response, FILE_NAME+ System.currentTimeMillis()+XLSX);
  27. ExcelWriter excelWriter = null;
  28. try(ServletOutputStream outputStream = response.getOutputStream()) {
  29. ExcelExpContent<String, BaseExcelExpHandleService> excelExpContent = factory.getExcelExpContent();
  30. Map<String, BaseExcelExpHandleService> serviceMap = excelExpContent.logHandleMap;
  31. Set<String> keys = serviceMap.keySet();
  32. Map<String,List<Object>> map = new ConcurrentHashMap<>(64);
  33. for (String key : keys) {
  34. scProcesses.setType(key);
  35. List data = serviceMap.get(key).getData(scProcesses);
  36. map.put(key,data);
  37. }
  38. excelWriter = EasyExcelFactory
  39. .write(outputStream)
  40. .build();
  41. WriteSheet writeSheet = null;
  42. Set<String> strings = map.keySet();
  43. int i=0;
  44. for (String key : strings) {
  45. scProcesses.setType(key);
  46. //获取头部
  47. List<List<String>> head = serviceMap.get(key).head(scProcesses);
  48. //获取策略
  49. List<WriteHandler> strategy = serviceMap.get(key).getStrategy();
  50. ExcelWriterSheetBuilder writerSheetBuilder = EasyExcelFactory
  51. .writerSheet(i, ExcelExpHandleTypeEnum.getNameByCode(key))
  52. .needHead(true)
  53. .head(head);
  54. //设置策略
  55. if (!CollectionUtils.isEmpty(strategy)){
  56. for (WriteHandler writeHandler : strategy) {
  57. writerSheetBuilder.registerWriteHandler(writeHandler);
  58. }
  59. }
  60. writeSheet =writerSheetBuilder.build();
  61. //写入sheet
  62. excelWriter.write(map.getOrDefault(key,null),writeSheet);
  63. ++i;
  64. }
  65. } catch (IOException e) {
  66. log.error("导出失败", e);
  67. }finally {
  68. if (excelWriter != null){
  69. excelWriter.finish();
  70. }
  71. }
  72. }
  73. }