首页 > 其他分享 >Springboot 使用easyexcel 实现Excel导出功能

Springboot 使用easyexcel 实现Excel导出功能

时间:2023-01-30 19:34:28浏览次数:38  
标签:task Springboot Excel easyexcel excel 导出 new response


Springboot 使用easyexcel 实现Excel导出

导入easyexcel依赖

<!--alibaba easyexcel  excel导出依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.3</version>
</dependency>

封装类上面的 @ExcelProperty(value = “设备号”, index = 0)

是excel的列名 和位置

Springboot 使用easyexcel 实现Excel导出功能_java

代码

@ExcelProperty(value = "设备号", index = 0)

导出excel 控制层

/**历史任务  EXCEL导出功能
* 参数 模糊查询 封装类
* */
@RequestMapping("/downloadEasyExcel")
public void downloadEasyExcel(HttpServletResponse response,Task task) throws IOException {
String fileName = URLEncoder.encode("历史任务列表", "UTF-8") + ConFig.newDate();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
// excel头策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 11);
headWriteFont.setBold(false);
headWriteCellStyle.setWriteFont(headWriteFont);
// excel内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short)11);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 设置handler
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
/** 导出数据查询方法*/
List<TaskExcel> tasks = taskService.selectHistoricalMissionEXCEL(task);
EasyExcel.write(response.getOutputStream(), TaskExcel.class)
.sheet("下载excel服务")
.registerWriteHandler(styleStrategy)
.doWrite(tasks);
}

在导出excel的时候经常会有这种需求 数据库里面存放的是数字 但是导出excel的时候不能显示数字

Springboot 使用easyexcel 实现Excel导出功能_java_02

在sql使用 CASE方法

Springboot 使用easyexcel 实现Excel导出功能_java_03


代码

(CASE task_status WHEN '3' THEN '完成' end ) as task_status,

最后导出样式

Springboot 使用easyexcel 实现Excel导出功能_excel_04


标签:task,Springboot,Excel,easyexcel,excel,导出,new,response
From: https://blog.51cto.com/u_15948415/6027758

相关文章

  • 基于Java+SpringBoot+vue+elementUI私人健身教练预约管理系统设计实现
    文章目录​​基于Java+SpringBoot+vue+elementUI私人健身教练预约管理系统设计实现​​​​一、前言介绍:​​​​二、系统设计:​​​​2.1性能需求分析​​​​2.2B/S架构......
  • java springboot+mybatis电影售票网站管理系统前台+后台设计和实现
    文章目录​​javaspringboot+mybatis电影售票网站管理系统前台+后台设计和实现​​​​主要技术框架:spring、springmvc、springboot、mybatis、jquery、templates模板......
  • springboot整合activeMq简单实现
    ActiveMQ下载启动首先第一步肯定是下载了,自己笔记本下载window版,服务器上下载linux版,不详细赘述了,安装好了,启动activemq.bat。2.pom引入相关依赖<dependency><groupId>......
  • SpringBoot整合ActiveMQ的详细步骤
    pom文件引入activemq依赖123456789101112131415161718192021222324<!--activeMq配置-->    <dependency>        <grou......
  • springboot配置activemq
    前言网上有好多介绍springboot集成activemq的文章,看了一些文章感觉比较零散,还是抽时间自己详细总结一个如何使用,需要注意哪些点。尤其是关于连接池的配置,需要重点关注,否则......
  • springboot整合activemq(三)配置文件
    #服务端口,8080被另一服务占用server.port=9090spring.activemq.broker-url=tcp://127.0.0.1:61616#在考虑结束之前等待的时间#spring.activemq.close-timeout=15s#默认代......
  • 随笔(十五)『SpringBoot 整合 Redis』
    一、添加依赖<!--redis启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>......
  • java hutool 导出excel 合并单元格
    packagepdf;importlombok.Data;importjava.math.BigDecimal;@DatapublicclassVerifyPlatform{ privatestaticfinallongserialVersionUID=1L; /**......
  • springboot集成swagger的坑
    1、端口问题无法访问此网站网址为 http://localhost:6666/swagger-ui.html 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。ERR_UNSAFE_PORT 如图:......
  • ORACLE BIPUBILSHER EXCEL模板相关问题
    1.BIPublisher介绍OracleBIpublisher,它的前身是oraclexmlpublisher。它是对一数据集(数据集简单说就是一张表)的展现定义多个模板。业务用户可以通过使用通用桌面工具......