EasyExcel 导出

文章目录

  • 1.EasyExcel 导出
    • 1.1. Response流导出单sheet页
      • 1.1.1 response流处理(防止中文乱码):
      • 1.1.2 根据excel映射对象导出(适合结构化数据:列表台账)
      • 1.1.3. 根据单元格集合导出(适合非结构化数据:详情页)
    • 1.2. 单元格样式(背景色、加粗、字体、边框)
    • 1.3. 单元格合并策略
      • 1.3.1 只合并一次
      • 1.3.2. 循环合并
    • 1.4. 列宽样式策略
      • 1.4.1. 根据每个列标题返回列宽
      • 1.4.2. 以最长列的宽度作为所有列的宽度
      • 1.4.3. 定长列宽
      • 1.4.4. 自动调整列宽(推荐)
    • 1.5. 转换器
      • 1.5.1. 转换器注入方式
      • 1.5.2. 转换器 注解
    • 1.6. RowWriteHandler 行处理器
    • 1.7. CellWriteHandler 单元格处理器
    • 1.8. 图片导出

1.EasyExcel 导出

1.1. Response流导出单sheet页

1.1.1 response流处理(防止中文乱码):

		response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");String fileName = URLEncoder.encode(getTitle(), "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

1.1.2 根据excel映射对象导出(适合结构化数据:列表台账)

excel导出实体:

@Data
@NoArgsConstructor
//@ContentRowHeight(50)//内容单元格高度
//@HeadRowHeight(50)//表头单元格高度
//@ColumnWidth(50)//单元格宽度
public class DemoExcelBO {@ExcelProperty(value = {"导出台账","序号"})private Integer no;@ExcelProperty(value = {"导出台账","编号"})private  String  code;
}

执行实际的导出动作:

        //要导出的excel 数据List<DemoExcelBO> dataList = new ArrayList<>();//指定outputStream和 excel映射实体EasyExcel.write(response.getOutputStream(), DemoExcelBO.class)//注册 WriteHandler ,getDefaultWriteHandler() 见下.registerWriteHandler(getDefaultWriteHandler())//注入原生 或 自定义转换器.registerConverter(new DateStringConverter()).registerConverter(new BigDecimalStringConverter()).sheet("这是指定要写入的sheet页").doWrite(dataList );

1.1.3. 根据单元格集合导出(适合非结构化数据:详情页)

		//执行excel写入对象ExcelWriter excelWriter = EasyExcel.write(url).build();//当前sheet页 写入对象创建者ExcelWriterSheetBuilder sheetBuilder = EasyExcel.writerSheet("测试sheet");//todo sheetBuilder  可以注入 writeHandlerWriteSheet writeSheet = sheetBuilder.build();//实际要写入的数据 ,List<Object> 为每一行的合并前的数据,放入List中为整个要写入的数据List<List<Object>> dataList = new ArrayList<>();//可以多次调用  excelWriter.write() 方法 。 写入同一sheet 的话 ,WriteSheet 对象不变excelWriter.write(list, writeSheet);//关流excelWriter.finish();

1.2. 单元格样式(背景色、加粗、字体、边框)

HorizontalCellStyleStrategy 分别设置 表头 和 表体 的样式:

	//默认设置的样式 (表头样式 和 表体样式 不同)default WriteHandler getDefaultWriteHandler() {WriteCellStyle headWriteCellStyle = new WriteCellStyle();headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());WriteFont headWriteFont = new WriteFont();
//        headWriteFont.setBold(true);// 加粗headWriteFont.setFontHeightInPoints((short) 11);//字体大小headWriteCellStyle.setWriteFont(headWriteFont);headWriteCellStyle.setWrapped(false);//自动换行
//        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);//CENTERWriteCellStyle contentWriteCellStyle = new WriteCellStyle();contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);contentWriteCellStyle.setBorderBottom(BorderStyle.NONE);//THINcontentWriteCellStyle.setBorderLeft(BorderStyle.NONE);contentWriteCellStyle.setBorderRight(BorderStyle.NONE);contentWriteCellStyle.setBorderTop(BorderStyle.NONE);contentWriteCellStyle.setWrapped(false);//自动换行HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);return horizontalCellStyleStrategy;}

1.3. 单元格合并策略

easyExcel 抽象父类类 :AbstractMergeStrategy

public abstract class AbstractMergeStrategy implements CellWriteHandler {@Overridepublic void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {//在创建单元格之前调用}@Overridepublic void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,Head head, Integer relativeRowIndex, Boolean isHead) {//创建单元格后调用}@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {//在单元格上的所有操作完成后调用if (isHead) {return;}merge(writeSheetHolder.getSheet(), cell, head, relativeRowIndex);}//合并策略protected abstract void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex);
}

1.3.1 只合并一次

easyExcel 预制子类:OnceAbsoluteMergeStrategy

//创建单元格时只合并一次
public class OnceAbsoluteMergeStrategy extends AbstractMergeStrategy {private int firstRowIndex;private int lastRowIndex;private int firstColumnIndex;private int lastColumnIndex;
}

1.3.2. 循环合并

easyExcel 预制子类:LoopMergeStrategy

//循环区域合并
public class LoopMergeStrategy extends AbstractMergeStrategy {private int eachRow;private int columnCount;private int columnIndex;
}

继承 AbstractMergeStrategy 重写 merge()方法 ,可自定义合并策略

1.4. 列宽样式策略

easyExcel 抽象父类:

public abstract class AbstractColumnWidthStyleStrategy implements CellWriteHandler, NotRepeatExecutor {@Overridepublic String uniqueValue() {return "ColumnWidthStyleStrategy";}@Overridepublic void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {//在创建单元格之前调用
}@Overridepublic void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,Head head, Integer relativeRowIndex, Boolean isHead) {//在创建单元格之后调用
}@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {//在单元格上的所有操作完成后调用setColumnWidth(writeSheetHolder, cellDataList, cell, head, relativeRowIndex, isHead);}//列宽样式策略protected abstract void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head,Integer relativeRowIndex, Boolean isHead);}

1.4.1. 根据每个列标题返回列宽

easyExcel 预制子类:AbstractHeadColumnWidthStyleStrategy

//根据每个列标题返回列宽
public abstract class AbstractHeadColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
}

1.4.2. 以最长列的宽度作为所有列的宽度

easyExcel 预制子类:LongestMatchColumnWidthStyleStrategy

//以最长列的宽度作为宽度
public class LongestMatchColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
}

1.4.3. 定长列宽

easyExcel 预制子类:SimpleColumnWidthStyleStrategy

//所有列的宽度相同
public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy {
}

继承 AbstractColumnWidthStyleStrategy 重写 setColumnWidth()方法 ,可自定义列宽策略

1.4.4. 自动调整列宽(推荐)

在这里插入代码片

1.5. 转换器

easyExcel 转换器 父接口:

public interface Converter<T> {// 写入excel时 ,指定一个class ,该class 会在写入时 被拦截执行转换Class supportJavaTypeKey();// 读取excel时 ,指定一个excel字段类型,该类型 会在读取时 被拦截执行转换CellDataTypeEnum supportExcelTypeKey();//写入时 , java 类型 转换 excel数据类型 动作T convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) throws Exception;//读取时 , excel数据类型 转换  java 类型动作CellData convertToExcelData(T value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration)throws Exception;
}

有预制转换器,均实现改接口,如需自定义转换器,可实现 Converter 接口。

1.5.1. 转换器注入方式

方式一: 在excel实体上指定 某些特定字段生效

public class ContractAccountExcelBO {@ExcelProperty(value = {"合同导出台账","","序号"} ,converter = IntegerStringConverter.class)private Integer no;
}

方式二: .registerConverter() 方法注入 ,本次读取或写入时,均生效

 //要导出的excel 数据
List<DemoExcelBO> dataList = new ArrayList<>();
EasyExcel.write(response.getOutputStream(),DemoExcelBO.class ).registerConverter(new BigDecimalStringConverter()).doWrite(dataList);//

1.5.2. 转换器 注解

指定日期格式注解:@DateTimeFormat(“yyyy-MM-dd”)
指定数字格式注解:@NumberFormat("#.00")
NumberFormat 中 format格式 遵循 Bigdecimal.format() 策略(#不补位,没有数字则空。0自动补位,没有数字自动补零)。

1.6. RowWriteHandler 行处理器

// 行创建 动作 拦截器
public interface RowWriteHandler extends WriteHandler {//行 创建前执行void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex,Integer relativeRowIndex, Boolean isHead);//行 创建后执行void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,Integer relativeRowIndex, Boolean isHead);//在该行的所有操作完成后调用。填充数据时不调用此方法。void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,Integer relativeRowIndex, Boolean isHead);
}

1.7. CellWriteHandler 单元格处理器

// 单元格创建 动作 拦截器
public interface CellWriteHandler extends WriteHandler {//单元格创建 前动作void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head,Integer columnIndex, Integer relativeRowIndex, Boolean isHead);//单元格创建 后动作void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head,Integer relativeRowIndex, Boolean isHead);//在单元格上的所有操作完成后调用void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead);
}

1.8. 图片导出

EasyExcel支持5种图片类型导出,根据1.1.3.把图片形式放入单元格中,写会即可

@Data
@ContentRowHeight(200)
@ColumnWidth(200 / 8)
public class ImageData {// 图片导出方式有5种private File file;private InputStream inputStream;/*** 如果string类型 必须指定转换器,string默认转换成string,该转换器是官方支持的*/@ExcelProperty(converter = StringImageConverter.class)private String string;private byte[] byteArray;/*** 根据url导出 版本2.1.1才支持该种模式*/private URL url;
}

Excel解析工具easyexcel全面探索
easyexcel复杂格式导出、自定义合并
图片导出

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/515721.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

视频豪横时代,应用如何快速构建视频点播能力?

QuestMobile2020数据显示&#xff0c;疫情发生以来&#xff0c;每个网民每天花在移动互联网的时长比年初增加了21.5%&#xff0c;对于视频类应用增长尤为突出。而短视频用户规模已超8.5亿&#xff0c;用户使用时长在移动互联网用户使用总时长占比已达10.5%&#xff0c;仅次于社…

大数据的下一站是什么?服务/分析一体化

作者&#xff1a;蒋晓伟&#xff08;量仔&#xff09; 阿里巴巴研究员 因为侧重点的不同&#xff0c;传统的数据库可以分为交易型的 OLTP 系统和分析型的 OLAP 系统。随着互联网的发展&#xff0c;数据量出现了指数型的增长&#xff0c;单机的数据库已经不能满足业务的需求。特…

No outgoing sequence flow of the exclusive gateway ‘XXX‘ could be selected for continuing the proces

不满足流程图的排他网关设置了【条件表达式】的条件&#xff0c;注意设置条件表达式值时&#xff0c;内容不要出现空格。 No outgoing sequence flow of the exclusive gateway sid-9B4912C2-EEA4-4076-886E-D185AB4CBDBB could be selected for continuing the process检查流…

系统架构设计师 - 构件

构件的获取方式 开发新构件 修改现有构件 继承现有功能 购买商业构件修改 构件的开发策略 分区 软件按问题情境进行空间分割 保证构件空间独立性 分割 软件按执流程行为进行时间分割 保证构件时间独立性 抽象 软件进行功能的抽取和分割 构件的标准 OMG公司 CORBA标准 微软公…

阿里云发布OAMKubernetes标准实现与核心依赖库

作者 | 张磊 阿里云高级技术专家、CNCF 官方大使&#xff0c;CNCF 应用交付领域 co-chair&#xff0c;Kubernetes 项目资深维护者 美国西部时间 2020 年 5 月 27 日&#xff0c;阿里云和微软云共同宣布&#xff0c;Open Application Model &#xff08;OAM&#xff09; 社区携…

Serverless 在大规模数据处理中的实践

来源 | Serverless作者 | 西流头图 | 下载于视觉中国前言当您第一次接触 Serverless 的时候&#xff0c;有一个不那么明显的新使用方式&#xff1a;与传统的基于服务器的方法相比&#xff0c;Serverless 服务平台可以使您的应用快速水平扩展&#xff0c;并行处理的工作更加有效…

flowable 控制台打印 sql 语句

配置文件中增加如下&#xff1a; logging:level:org.flowable.engine.impl.persistence.entity.*: debugorg.flowable.task.service.impl.persistence.entity.*: debug

阿里宜搭重磅发布专有云版本、精品应用市场,助力政企数字化转型

6月9日&#xff0c;在2020阿里云线上峰会上&#xff0c;“宜搭”重磅发布专有云版本和精品应用市场&#xff0c;为政企数字化转型提供高效、安全、可靠的服务。宜搭是阿里巴巴集团企业智能事业部自研的低代码应用开发PaaS平台&#xff0c;通过可视化拖拽的方式&#xff0c;传统…

系统机构设计师 - 软件质量属性

架构设计风险 潜在的 隐藏的 架构决策导致的隐患 敏感点 一个或多个构件的质量属性&#xff0c;输入方差小时&#xff0c;输出方差很大 权衡点 影响多个质量属性&#xff0c;且这些质量属性都是敏感点的 质量属性。

信息如何实现病毒式传播?一文看懂Gossip协议

来源 | 架构之美责编 | 寇雪芹头图 | 下载于视觉中国起源Gossip protocol 也叫 Epidemic Protocol &#xff08;流行病协议&#xff09;。Gossip protocol在1987年8月由施乐-帕洛阿尔托研究中心发表ACM上的论文《Epidemic Algorithms for Replicated Database Maintenance》中被…

勇于尝鲜,感受世界——对话阿里云 MVP黄坤

云栖号资讯&#xff1a;【点击查看更多行业资讯】 在这里您可以找到不同行业的第一手的上云资讯&#xff0c;还在等什么&#xff0c;快来&#xff01; 简介&#xff1a; 阿里云 MVP黄坤是个言简意赅的实干家&#xff0c;言谈中显示出的知识广度和技术深度令我钦佩折服&#xff…

Vue中时间日期格式化

封装格式化时间方法 创建一个js文件formatDate.js&#xff0c;内容如下&#xff1a; //方法一 export function formatDate(val) {var date new Date(Number(val)); //时间戳为10位需*1000&#xff0c;时间戳为13位的话不需乘1000var Y date.getFullYear() "-";v…

分布式应用,response导出error on submit request on future invoke、java.lang.OutOfMemoryError: Java heap space

背景 HSF 分布式框架 &#xff0c;基于EasyExcel 实现excel导出 。 控制层&#xff0c; GET请求 &#xff0c; 传递 HttpServletRequest 和 HttpServletRespose 到&#xff0c;业务中台时&#xff0c;异常 原因 分布式应用下&#xff0c;控制层传递response到中台时&#xf…

阿里云混合云管理平台发布帮您管好云

6月9日&#xff0c; 在2020阿里云线上峰会上阿里云混合云战略正式发布&#xff1a;全栈建云、智能管云、极致用云。同步发布专有云敏捷版&#xff08;Apsara Stack Agility&#xff09;、 混合云管理平台&#xff08;Apsara Uni-manager&#xff09;以及下一代企业级一站式DevO…

ApiPost6/Postman发送POST请求及日期格式的参数

Postman在发送POST请求的时候&#xff0c;所有参数写在Request Body&#xff08;请求体&#xff09;中&#xff0c;如果需要的参数类型的日期格式的&#xff0c;只需要将日期格式写为2000/01/01即可&#xff0c;Postman会自动识别为日期格式的数据。 如果是2000-01-01格式&…

Mendix入局中国低代码,开发者们你准备好了吗

作者 | 宋慧 出品 | CSDN云计算 头图 | 付费下载于视觉中国 在企业级软件与技术领域鼎鼎大名的Gartner魔力象限&#xff0c;几乎是全球公认的IT厂商实力的重要背书&#xff0c;进入魔力象限右上部分的领导者&#xff08;leaders&#xff09;更是能力全面的最优质厂商。 2021年…

OpenYurt开箱测评|一键让原生K8s集群具备边缘计算能力

作者| 郑超 阿里云高级开发工程师 随着物联网技术以及 5G 技术的高速发展&#xff0c;将云计算的能力延伸至边缘设备端&#xff0c;并通过中心进行统一交付、管控&#xff0c;已成为云计算的重要发展趋势。为服务更多开发者把握这一趋势&#xff0c;5 月 29 日&#xff0c;阿里…

系统架构设计师 - 主观题总结

文章目录2015软件架构评估系统建模嵌入式可靠性文件系统、关系型数据库、内存型数据库WEB应用持久层设计2017软件架构评估软件系统架构设计流行技术数据库访问层WEB系统架构设计2015 软件架构评估 质量属性效用树&#xff1a; 软件架构风险&#xff1a; 敏感点&#xff1a; 权…

pagehelper版本升级导致pageSize为0时无法查询全部数据

前言 pageSize为0无效,修改配置 springboot版本升级后&#xff0c;pagehelper插件由1.2.3版本升级到1.4.1版本&#xff1b;发现升级之后pageSize为0时无法查询全部数据 maven依赖引入 旧版本pom.xml配置1.2.3 <dependency><groupId>com.github.pagehelper</g…

技术运维的经营大法——对话阿里云MVP熊昌伟

云栖号资讯&#xff1a;【点击查看更多行业资讯】 在这里您可以找到不同行业的第一手的上云资讯&#xff0c;还在等什么&#xff0c;快来&#xff01; 简介&#xff1a; 不同于其他技术人的进阶经历&#xff0c;熊昌伟毕业后从未跳槽&#xff0c;在用友网络潜心修炼14年至今。从…