1. pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.fresher.boot</groupId><artifactId>spring-boot-parent-starter</artifactId><version>1.0-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.fresher.kkk</groupId><artifactId>kkk-bff-cater</artifactId><version>${release.version}</version><name>kkk-bff-cater</name><description>Cater Service</description><!-- 环境配置 --><profiles><profile><id>testing</id><activation><activeByDefault>true</activeByDefault></activation><properties><spring.profiles.active>testing</spring.profiles.active><service.provider.version>1.0-testing-SNAPSHOT</service.provider.version><release.version>1.0-testing-SNAPSHOT</release.version><common.provider.version>1.0-testing-SNAPSHOT</common.provider.version></properties></profile><profile><id>qa</id><activation><activeByDefault>false</activeByDefault></activation><properties><spring.profiles.active>qa</spring.profiles.active><service.provider.version>1.0-qa-SNAPSHOT</service.provider.version><release.version>1.0-qa-SNAPSHOT</release.version><common.provider.version>1.0-qa-SNAPSHOT</common.provider.version></properties></profile><profile><id>pre</id><activation><activeByDefault>false</activeByDefault></activation><properties><spring.profiles.active>pre</spring.profiles.active><service.provider.version>1.0-pre-SNAPSHOT</service.provider.version><release.version>1.0-pre-SNAPSHOT</release.version><common.provider.version>1.0-pre-SNAPSHOT</common.provider.version></properties></profile><profile><id>pro</id><activation><activeByDefault>false</activeByDefault></activation><properties><spring.profiles.active>pro</spring.profiles.active></properties></profile></profiles><dependencies><!-- Spring Cloud --><dependency><groupId>com.fresher.cloud</groupId><artifactId>spring-cloud</artifactId><version>1.0.2021011419</version></dependency><!-- Service Provider --><dependency><groupId>com.fresher.kkk</groupId><artifactId>kkk-service-provider</artifactId><version>${service.provider.version}</version></dependency><dependency><groupId>com.fresher.common</groupId><artifactId>common-provider</artifactId><version>${common.provider.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.7</version></dependency><!-- Spring Boot Test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion><!-- 排除日志包(log4j2),解决日志包冲突问题 --><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency></dependencies><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><includeSystemScope>true</includeSystemScope></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><executions><!-- 表示在执行package打包时,执行 assembly:single,所以可以直接使用mvn package打包 --><execution><id>bundle</id><phase>package</phase><goals><goal>single</goal></goals><configuration><finalName>${project.artifactId}</finalName><descriptors><descriptor>${basedir}/release/assembly.xml</descriptor></descriptors></configuration></execution></executions></plugin><!-- 资源文件拷贝插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><encoding>UTF-8</encoding><!-- 过滤后缀文件 --><nonFilteredFileExtensions><nonFilteredFileExtension>xlsx</nonFilteredFileExtension><nonFilteredFileExtension>xls</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin></plugins><resources><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources></build><!-- Nexus --><repositories><repository><id>nexus</id><url>http://139.224.21.88:8081/repository/maven-public/</url></repository></repositories>
</project>

2. 代码

controller@ApiOperation("下载")@GetMapping("/download")public void download(@Valid @Hashids ReportListRequest reportListRequest,HttpServletResponse response) {try {reportService.download(reportListRequest, response);} catch (IOException e) {e.printStackTrace();log.error("销售报表导出失败。msg = " + e.getMessage());}}service@Overridepublic void download(ReportListRequest reportListRequest, HttpServletResponse response) throws IOException {String mallName = getMallName(reportListRequest.getMallId());// 构造文件名String fileName = buildFileName(mallName);// 数据查询List<SaleRecordVO> saleRecordVOList = list(reportListRequest);// 汇总int total = getTotal(saleRecordVOList);response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");InputStream inputStream = this.getClass().getResourceAsStream("/template" + File.separator + "report_templates.xlsx");ZipSecureFile.setMinInflateRatio(-1.0d);ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).excelType(ExcelTypeEnum.XLSX).withTemplate(inputStream).build();WriteSheet writeSheet = EasyExcel.writerSheet().sheetNo(0).build();FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();excelWriter.fill(saleRecordVOList, fillConfig, writeSheet);Map<String, Object> map = new HashMap<String, Object>();map.put("mallName", mallName);map.put("dateType", getDateTypeName(reportListRequest));map.put("startTime", DateUtils.format(new Date(reportListRequest.getStartTime()), DateUtils.DATE_FORMAT_10));map.put("endTime", DateUtils.format(new Date(reportListRequest.getEndTime()), DateUtils.DATE_FORMAT_10));Integer itemType = reportListRequest.getItemType();map.put("itemType", Objects.isNull(itemType) ? BaseConstant.ALL_STR : ItemTypeEnum.of(itemType).getMessage());String categoryId = reportListRequest.getSpuCategoryId();map.put("categoryType", StringUtil.isBlank(categoryId) ? BaseConstant.ALL_STR : getCategoryName(reportListRequest.getSpuCategoryId()));map.put("total", total);excelWriter.fill(map, writeSheet);excelWriter.finish();}

阿里巴巴-easyexcel 下载案例相关推荐

  1. 基于阿里巴巴EasyExcel实现对复杂Excel模板的填充

    基于阿里巴巴EasyExcel实现对复杂Excel模板的填充 官方文档以及开源 对应修改 代码展示 测试 官方文档以及开源 EasyExcel是一个基于Java的简单.省内存的读写Excel的开源项目 ...

  2. 使用EasyExcel下载,文件名乱码问题处理

    使用阿里EasyExcel下载excle的简单样例记录,包含前后端核心代码,主要记录中文文件名称乱码问题的处理. EasyExcle的使用不再多做记录,EasyExcel教程-阿里云开发者社区 (al ...

  3. Java使用EasyExcel下载xls、xlsx 出现文件格式与扩展名不匹配(亲测)

    在使用easyexcel下载excel 文件,成功后打开文件出现了一下的情况: 经过实验发现是ContentType的问题 Content-Type,即内容类型,一般是指网页中存在的Content-T ...

  4. easyexcel下载工具

    优化excel下载功能,采用新的工具类降低内存: https://github.com/alibaba/easyexcel pom: <!-- easyexcel依赖包 --> <d ...

  5. easyExcel下载多个标题,多个sheet页Excel的案例

    1.前一段时间使用Eaexcel做了一个下载EXCEL的功能,发现网上有很多的分享不是很全面,我是查询后结合了很多的案例和博客,开发了这个功能,现将其分享给大家, (1)废话不多说,上代码:先看Esb ...

  6. easyExcel下载或导出

    pom <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <ve ...

  7. EasyExcel 下载demo(zip压缩包)

    前言 最近有关于excel下载的需求,写此文章记录技术要点. 功能需求: 要求根据页面选择数据下载成excel: 根据选择类型分组,下载成多个excel并打包成zip压缩文件并通过浏览器下载 一.项目 ...

  8. easyexcel下载excel文件名为空的问题

    @RestController public class StudentController {@RequestMapping("/download")public JsonRes ...

  9. 使用SpringMVC模拟文件上传与下载案例

    文件上传下载 SpringMVC封装了Tomcat的上传文件功能 MultipartResolver接口 MultipartResolver接口定义了文件上传过程中的相关操作,并对通用性操作进行了封装 ...

最新文章

  1. 【radar】毫米波雷达相关资料(文献综述列表、顶会研讨会资料列表、顶会workshops资料列表、工具书、使用手册)(2)
  2. Rhel6.0部署Oracle10g报错相关问题记录
  3. ForkJoinPool框架设计与实现
  4. .NET实现之(WebBrowser数据采集—续篇)
  5. spring3: 切面及通知实例 Aspectj的aop
  6. 硬核黑科技、技术大咖、AI 音乐节……科大讯飞全球 1024 开发者节太燃了!
  7. 为什么程序员对旧代码深恶痛绝?
  8. Windows 8 Directx 开发学习笔记(十三)利用模板实现木箱镜像
  9. linux命令大全-比较常用的
  10. 自然语言处理 -- NLP作业 1 :训练词向量
  11. Everything的使用-初级篇
  12. linux之SVN安装
  13. 免越狱免签名苹果ios webAPP打包生成网站APP教程附iphone配置实用工具
  14. 不想下载那么多音乐软件?全网音乐在线听和下载
  15. VS.NET(C#)-3.13_Panel控件
  16. 我的世界里 有你还不知道的秘密 边走边学习 且行且珍惜吧
  17. 《黑匣子思维:我们如何更理性地犯错》ipad部分
  18. 浅析 Hadoop 中的数据倾斜
  19. RestCloud API接口管理平台
  20. java中char数据类型的使用

热门文章

  1. 经常使用电脑易患焦虑抑郁症
  2. Kali渗透-NMAP高级使用技巧和漏洞扫描发现
  3. 求助selenium:百度文库充值弹窗怎么关闭
  4. python官方下载安装教程,python官方下载网站
  5. 一次K8S容器内存占用居高不下的排查案例
  6. 今日头条 频道管理(删除、添加、拖动)
  7. 关于——css3新增属性有哪些?css3中新增属性(部分总结)
  8. 计算机投诉信英语作文,关于英语作文投诉信集锦5篇
  9. ECLIPSE明明能跳到头文件,却提示Unresolved Inclusion,怎么办?
  10. iOS 淡入淡出转换rootViewController