EasyExcel文件导出简洁版

1. EasyExcel简介

EasyExcel是一个基于Java的简单、快速、lightweight的Excel处理库。它的主要特点包括:

  • 轻量级设计:
    EasyExcel的jar包大小仅约1MB,相比较其他Excel处理库如Apache POI来说更加轻量。
    采用内存友好的流式读写模式,无需一次性加载整个Excel文件到内存,大大减少了内存占用。
  • 高性能读写:
    通过事件驱动的读写模式,逐行处理数据,避免了对整个Excel文件的加载。
    在处理大Excel文件时,EasyExcel表现出优异的性能,远超传统的Excel操作库。
  • 使用简单:
    EasyExcel抽象了Excel操作的底层细节,提供了简单易用的API。
    开发者只需编写少量代码就可完成数据的读写操作,大幅提升了开发效率。
  • 丰富功能:
    除了基础的读写操作,EasyExcel还支持样式设置、公式计算、合并单元格等功能。
    同时还提供了Web导入导出的解决方案,简化了Web应用中的Excel交互。
  • 活跃社区:
    EasyExcel拥有良好的社区支持,有丰富的使用文档和示例代码。
    同时也有大量第三方插件和工具,为开发者提供全面的生态支持。

2. 依赖包引入

sh 复制代码
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>easyexcel</artifactId>
   <version>4.0.2</version>
</dependency>

3. 核心代码

java 复制代码
public void exportExcel(String scriptId, HttpServletResponse response) {
        try {
            List<ScenesExportDto> scenesExportDtos = ***
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            EasyExcel.write(response.getOutputStream(), ScenesExportDto.class).sheet("场景").doWrite(scenesExportDtos);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
}
java 复制代码
@Data
// 设置头样式
@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 55)
@ColumnWidth(25)
public class ScenesExportDto {
    // 设置列长
    @ColumnWidth(15)
    @ExcelProperty("场景序号")
    private Integer sort;

    // 设置该列内容粗体
    @ContentFontStyle(bold = BooleanEnum.TRUE)
    private String name;

    @ExcelProperty("时长(秒)")
    @ColumnWidth(10)
    private Integer duration;
}

更多示例,请查看官方文档

相关推荐
.格子衫.1 天前
Spring Boot 原理篇
java·spring boot·后端
多云几多1 天前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
Jabes.yang1 天前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪1 天前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人1 天前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR221 天前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠1 天前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
笨手笨脚の1 天前
设计模式-迭代器模式
java·设计模式·迭代器模式·行为型设计模式
spencer_tseng1 天前
Eclipse 4.7 ADT (Android Development Tools For Eclipse)
android·java·eclipse
聪明的笨猪猪1 天前
Java Spring “AOP” 面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试