当前位置:   article > 正文

Springboot基于easyexcel实现一个excel文件包含多个sheet表格的数据导出_easyexcel导出多个sheet

easyexcel导出多个sheet

前言

EasyExcel 是一款基于Java的开源Excel操作工具,它提供了简单且强大的 API,使开发人员可以轻松地读写、操作和生成Excel文件。

EasyExcel 支持 Excel 文件的导入和导出,可以处理大量数据,具有高性能和低内存占用。它可以读取 Excel 文件中的数据,并将数据转换为 Java 对象,也可以将Java对象写入Excel文件。

EasyExcel 还提供了丰富的格式化选项和功能,如设置单元格样式、合并单元格、设置公式等。同时,EasyExcel 还支持多线程操作,可以在处理大量数据时提高处理效率。由于其简单易用的特点,EasyExcel 被广泛应用于数据导入导出、报表生成、数据分析等领域。

一、引入easyexcel依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>easyexcel</artifactId>
  4. <version>3.2.1</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>poi-ooxml-schemas</groupId>
  8. <artifactId>org.apache.poi</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>

二、创建实体类

  1. package com.ruoyi.exportData.vo;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import com.alibaba.excel.annotation.write.style.*;
  4. import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Builder;
  7. import lombok.Data;
  8. import lombok.NoArgsConstructor;
  9. import lombok.experimental.Accessors;
  10. @Data
  11. @NoArgsConstructor
  12. @AllArgsConstructor
  13. @Accessors(chain = true)
  14. @ColumnWidth(25)
  15. @ContentRowHeight(30)
  16. @HeadRowHeight(50)
  17. @Builder
  18. @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)
  19. @HeadFontStyle(fontHeightInPoints = 12)
  20. @ContentFontStyle(fontHeightInPoints = 11)
  21. public class ExportListVo {
  22. @ExcelProperty(value = "名称")
  23. private String name;
  24. @ExcelProperty(value = "数据")
  25. private String value;
  26. }

easyexcel常用注解

@ExcelProperty 用于标识excel中的字段,可以指定字段在Excel中的列索引或列名

@ColumnWith::设置列宽

@ColumnWidth: 全局列宽

@ContentFontStyle: 用于设置单元格内容字体格式的注解

               

@ContentLoopMerge:用于设置合并单元格

                

@ContentRowHeight:用于设置行高

                

@ContentStyle:设置内容格式

               

@HeadFontStyle:用于定制标题字体格式

                

@HeadRowHeight:设置标题行行高

                

@HeadStyle:设置标题样式

                

@ExcelIgnore:不将该字段转换成Excel

@ExcelIgnoreUnannotated:没有注解的字段都不转换

三、controller层接口

  1. package com.ruoyi.web.controller.zx.export;
  2. import com.alibaba.excel.EasyExcelFactory;
  3. import com.alibaba.excel.ExcelWriter;
  4. import com.alibaba.excel.write.metadata.WriteSheet;
  5. import com.alibaba.excel.write.metadata.style.WriteCellStyle;
  6. import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
  7. import com.ruoyi.baseconsulttopics.service.IBaseconsultTopicsService;
  8. import com.ruoyi.committee.service.ICommitteeService;
  9. import com.ruoyi.common.domain.ResultVo;
  10. import com.ruoyi.document.service.IDocumentService;
  11. import com.ruoyi.exportData.vo.ExportListVo;
  12. import com.ruoyi.meet.service.IMeetService;
  13. import com.ruoyi.network.service.INetworkService;
  14. import com.ruoyi.news.service.INewsService;
  15. import com.ruoyi.proposal.service.IProposalService;
  16. import com.ruoyi.publicopinion.service.IPublicopinionService;
  17. import com.ruoyi.scholarly.service.IScholarlyLearningService;
  18. import com.ruoyi.scholarly.service.IScholarlyReadService;
  19. import com.ruoyi.transformdocument.service.TransformDocumentService;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.OutputStream;
  26. import java.net.URLEncoder;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. @RestController
  30. @RequestMapping("/export")
  31. @Slf4j
  32. public class ExportDataIndexController {
  33. @Autowired
  34. private IProposalService proposalService;
  35. @Autowired
  36. private IPublicopinionService publicopinionService;
  37. @Autowired
  38. private INetworkService networkService;
  39. @Autowired
  40. private ICommitteeService committeeService;
  41. @Autowired
  42. private IMeetService meetService;
  43. @Autowired
  44. private IDocumentService documentService;
  45. @Autowired
  46. private INewsService newsService;
  47. @Autowired
  48. private IScholarlyLearningService scholarlyLearningService;
  49. @Autowired
  50. private TransformDocumentService transformDocumentService;
  51. @Autowired
  52. private IBaseconsultTopicsService baseconsultTopicsService;
  53. @GetMapping("/exportDataIndex")
  54. public void exportExcel(HttpServletResponse response) {
  55. try (OutputStream out = response.getOutputStream()) {
  56. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  57. response.setCharacterEncoding("utf-8");
  58. String fileName = URLEncoder.encode("云南省政协数据可视化中心", "UTF-8").replaceAll("\\+", "%20");
  59. response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
  60. handleExcel(out);
  61. out.flush();
  62. } catch (Exception e) {
  63. log.error(e.getMessage());
  64. }
  65. }
  66. private void handleExcel(OutputStream out) {
  67. try (ExcelWriter excelWriter = EasyExcelFactory.write(out).build()) {
  68. //设置内容样式
  69. WriteCellStyle contentStyle = new WriteCellStyle();
  70. contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//居中
  71. contentStyle.setWrapped(true);//自动换行
  72. //设置头部样式
  73. WriteCellStyle headerStyle = new WriteCellStyle();
  74. headerStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
  75. //设置策略
  76. HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headerStyle,contentStyle);
  77. WriteSheet proposalSheet = EasyExcelFactory.writerSheet(0, "委员提案").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  78. WriteSheet publicopinionSheet = EasyExcelFactory.writerSheet(1, "社情民意").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  79. WriteSheet consultationSheet = EasyExcelFactory.writerSheet(2, "协商议政").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  80. WriteSheet committeeSheet = EasyExcelFactory.writerSheet(3, "委员信息").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  81. WriteSheet meetSheet = EasyExcelFactory.writerSheet(4, "会议活动").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  82. WriteSheet documentSheet = EasyExcelFactory.writerSheet(5, "公文流转").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  83. WriteSheet provinceSheet = EasyExcelFactory.writerSheet(6, "云南全省政协").head(ExportListVo.class).registerWriteHandler(horizontalCellStyleStrategy).build();
  84. excelWriter.write(getProposal(), proposalSheet);
  85. excelWriter.write(getPublicopinion(), publicopinionSheet);
  86. excelWriter.write(getConsultation(),consultationSheet);
  87. excelWriter.write(getCommittee(),committeeSheet);
  88. excelWriter.write(getMeet(),meetSheet);
  89. excelWriter.write(getDocument(),documentSheet);
  90. excelWriter.write(getProvinceWide(),provinceSheet);
  91. }
  92. }
  93. //首页-委员提案
  94. private List<ExportListVo> getProposal() {
  95. List<ExportListVo> list = new ArrayList<>();
  96. int proposalCount = proposalService.proposalCount(null, area);
  97. List<ResultVo> proposalKind = proposalService.proposalKind(null, area);
  98. ExportListVo vo = new ExportListVo("委员提案总数",proposalCount+" 条");
  99. list.add(vo);
  100. for (ResultVo result : proposalKind) {
  101. ExportListVo kind = new ExportListVo();
  102. kind.setName(result.getName());
  103. kind.setValue(result.getValue()+" 条");
  104. list.add(kind);
  105. }
  106. return list;
  107. }
  108. //首页-社情民意
  109. private List<ExportListVo> getPublicopinion() {
  110. List<ExportListVo> list = new ArrayList<>();
  111. int opinionCount = publicopinionService.opinionCount(null, area, null);
  112. List<ResultVo> opinionType = publicopinionService.opinionType(null, area);
  113. ExportListVo vo = new ExportListVo("社情民意总数",opinionCount+" 条");
  114. list.add(vo);
  115. for (ResultVo result : opinionType) {
  116. ExportListVo type = new ExportListVo(result.getName(),result.getValue()+" 条");
  117. list.add(type);
  118. }
  119. return list;
  120. }
  121. //首页-网络议政/远程协商
  122. public List<ExportListVo> getConsultation(){
  123. List<ExportListVo> list = new ArrayList<>();
  124. List<ResultVo> netWorkCount = networkService.netWorkCount(null, area);
  125. List<ResultVo> remoteCount = networkService.remoteCount(null, area);
  126. List<ResultVo> networkConduct = networkService.networkConduct(area);
  127. List<ResultVo> networkClosed = networkService.networkClosed(area);
  128. List<ResultVo> remoteConduct = networkService.remoteConduct(area);
  129. List<ResultVo> remoteClosed = networkService.remoteClosed(area);
  130. ExportListVo vo = new ExportListVo("网络议政总数",netWorkCount.get(0).getValue()+" 条");
  131. ExportListVo vo1 = new ExportListVo("远程协商总数",remoteCount.get(0).getValue()+" 条");
  132. ExportListVo vo2 = new ExportListVo("网络议政-进行中",networkConduct.get(0).getValue()+" 条");
  133. ExportListVo vo3 = new ExportListVo("网络议政-已结束",networkClosed.get(0).getValue()+" 条");
  134. ExportListVo vo4 = new ExportListVo("远程协商-进行中",remoteConduct.get(0).getValue()+" 条");
  135. ExportListVo vo5 = new ExportListVo("远程协商-已结束",remoteClosed.get(0).getValue()+" 条");
  136. list.add(vo);
  137. list.add(vo1);
  138. list.add(vo2);
  139. list.add(vo3);
  140. list.add(vo4);
  141. list.add(vo5);
  142. return list;
  143. }
  144. //首页-委员信息
  145. public List<ExportListVo> getCommittee(){
  146. List<ExportListVo> list = new ArrayList<>();
  147. int committeeCount = committeeService.committeeCount(area,null);//委员人数
  148. int standingCommitteeCount = committeeService.standingCommmitteeCount(area,null);//常委人员
  149. int officeCount = committeeService.officeCount(area);//机关人
  150. ExportListVo vo = new ExportListVo("委员人数",committeeCount+" 人");
  151. ExportListVo vo1 = new ExportListVo("常委人数",standingCommitteeCount+" 人");
  152. ExportListVo vo2 = new ExportListVo("机关人数",officeCount+" 人");
  153. list.add(vo);
  154. list.add(vo1);
  155. list.add(vo2);
  156. List<ResultVo> partiesCount = committeeService.partiesCount(area,null);//委员构成
  157. List<ResultVo> ageCount = committeeService.ageCount(area,null);//年龄
  158. List<ResultVo> genderCount = committeeService.genderCount(area,null);//性别
  159. List<ResultVo> nationCount = committeeService.nationCount(area,null);//民族
  160. list.add(new ExportListVo("党派",null));
  161. for (ResultVo result : partiesCount) {
  162. ExportListVo parties = new ExportListVo(result.getName(), result.getValue() + " 人");
  163. list.add(parties);
  164. }
  165. list.add(new ExportListVo("年龄",null));
  166. for (ResultVo result : ageCount) {
  167. ExportListVo age = new ExportListVo(result.getName(), result.getValue() + " 人");
  168. list.add(age);
  169. }
  170. list.add(new ExportListVo("性别",null));
  171. for (ResultVo result : genderCount) {
  172. ExportListVo gender = new ExportListVo(result.getName(), result.getValue() + " 人");
  173. list.add(gender);
  174. }
  175. list.add(new ExportListVo("民族",null));
  176. for (ResultVo result : nationCount) {
  177. ExportListVo nation = new ExportListVo(result.getName(), result.getValue() + "人");
  178. list.add(nation);
  179. }
  180. return list;
  181. }
  182. //首页-会议活动
  183. public List<ExportListVo> getMeet(){
  184. List<ExportListVo> list = new ArrayList<>();
  185. List<ResultVo> meetCount = meetService.meetCount(null,area);
  186. list.add(new ExportListVo("会议活动总数",meetCount.get(0).getValue()+" 次"));
  187. List<ResultVo> yearCount = meetService.yearCount(area);
  188. for (ResultVo result : yearCount) {
  189. list.add(new ExportListVo(result.getName(),result.getValue()+" 次"));
  190. }
  191. return list;
  192. }
  193. //首页-公文流转
  194. public List<ExportListVo> getDocument(){
  195. List<ExportListVo> list = new ArrayList<>();
  196. int documentCount = documentService.documentCount(null, area);
  197. list.add(new ExportListVo("公文流转总数",documentCount+" 条"));
  198. List<ResultVo> yearCount = documentService.yearCount(null, area);
  199. for (ResultVo result : yearCount) {
  200. list.add(new ExportListVo(result.getName(),result.getValue()+" 条"));
  201. }
  202. return list;
  203. }
  204. //首页-云南全省政协
  205. public List<ExportListVo> getProvinceWide(){
  206. List<ExportListVo> list = new ArrayList<>();
  207. int committeeCount = committeeService.committeeCount(area,null);//委员人数
  208. int standingCommitteeCount = committeeService.standingCommmitteeCount(area,null);//常委人员
  209. int officeCount = committeeService.officeCount(area);//机关人
  210. int opinionCount = publicopinionService.opinionCount(null, area, null);//社情民意
  211. int newsCount = newsService.newsCount(null, area);//政协新闻
  212. int proposalCount = proposalService.proposalCount(null, area);//委员提案
  213. List<ResultVo> netWorkCount = networkService.netWorkCount(null, area);//网络议政
  214. List<ResultVo> remoteCount = networkService.remoteCount(null, area);//远程协商
  215. Integer readCount = scholarlyLearningService.getCount("", area);
  216. List<ResultVo> meetCount = meetService.meetCount(null,area);//会议活动
  217. int documentCount = documentService.documentCount(null, area);//公文流转
  218. int exchangeDocumentCount = transformDocumentService.exchangeDocumentCount();//公文交换
  219. int baseTopicCount = baseconsultTopicsService.baseTopicCount(area);//协商在基层
  220. list.add(new ExportListVo("委员人数",committeeCount+" 人"));
  221. list.add(new ExportListVo("常委人数",standingCommitteeCount+" 人"));
  222. list.add(new ExportListVo("机关人数",officeCount+" 人"));
  223. list.add(new ExportListVo("政协新闻",newsCount+" 条"));
  224. list.add(new ExportListVo("社情民意",opinionCount+"条"));
  225. list.add(new ExportListVo("委员提案",proposalCount+" 条"));
  226. list.add(new ExportListVo("网络议政",netWorkCount.get(0).getValue()+" 次"));
  227. list.add(new ExportListVo("远程协商",remoteCount.get(0).getValue()+ " 次"));
  228. list.add(new ExportListVo("书香政协",readCount+" 条"));
  229. list.add(new ExportListVo("会议活动",meetCount.get(0).getValue()+" 次"));
  230. list.add(new ExportListVo("公文流转",documentCount+" 条"));
  231. list.add(new ExportListVo("公文交换",exchangeDocumentCount+" 条"));
  232. list.add(new ExportListVo("协商在基层",baseTopicCount+" 条"));
  233. return list;
  234. }
  235. }

四、在浏览器地址栏输入地址:http://localhost:8997/export/exportDataIndex   

进行数据导出

最后导出的文件如下所示:

至此,一个工作簿多个工作表导出的功能就完成了。

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

闽ICP备14008679号