Easyexcel(7-自定义样式)

相关文章链接

  1. Easyexcel(1-注解使用)
  2. Easyexcel(2-文件读取)
  3. Easyexcel(3-文件导出)
  4. Easyexcel(4-模板文件)
  5. Easyexcel(5-自定义列宽)
  6. Easyexcel(6-单元格合并)
  7. Easyexcel(7-自定义样式)

注解

@ContentStyle

用于设置内容格式注解,可作用于类和字段上

  1. dataFormat:日期格式
  2. hidden:设置单元格使用此样式隐藏
  3. locked:设置单元格使用此样式锁定
  4. quotePrefix:在单元格前面增加`符号,数字或公式将以字符串形式展示
  5. horizontalAlignment:设置是否水平居中
  6. wrapped:设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
  7. verticalAlignment:设置是否垂直居中
  8. rotation:设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
  9. indent:设置单元格中缩进文本的空格数
  10. borderLeft:设置左边框的样式
  11. borderRight:设置右边框样式
  12. borderTop:设置上边框样式
  13. borderBottom:设置下边框样式
  14. leftBorderColor:设置左边框颜色
  15. rightBorderColor:设置右边框颜色
  16. topBorderColor:设置上边框颜色
  17. bottomBorderColor:设置下边框颜色
  18. fillPatternType:设置填充类型
  19. fillBackgroundColor:设置背景色
  20. fillForegroundColor:设置前景色
  21. shrinkToFit:设置自动单元格自动大小

@ContentFontStyle

用于设置单元格内容字体格式的注解,可作用于类和字段上

  1. fontName:字体名称
  2. fontHeightInPoints:字体高度
  3. italic:是否斜体
  4. strikeout:是否设置删除水平线
  5. color:字体颜色
  6. typeOffset:偏移量
  7. underline:下划线
  8. bold:是否加粗
  9. charset:编码格式

@HeadStyle

用于设置标题样式,可作用于类和字段上

  1. dataFormat:日期格式
  2. hidden:设置单元格使用此样式隐藏
  3. locked:设置单元格使用此样式锁定
  4. quotePrefix:在单元格前面增加`符号,数字或公式将以字符串形式展示
  5. horizontalAlignment:设置是否水平居中
  6. wrapped:设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
  7. verticalAlignment:设置是否垂直居中
  8. rotation:设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
  9. indent:设置单元格中缩进文本的空格数
  10. borderLeft:设置左边框的样式
  11. borderRight:设置右边框样式
  12. borderTop:设置上边框样式
  13. borderBottom:设置下边框样式
  14. leftBorderColor:设置左边框颜色
  15. rightBorderColor:设置右边框颜色
  16. topBorderColor:设置上边框颜色
  17. bottomBorderColor:设置下边框颜色
  18. fillPatternType:设置填充类型
  19. fillBackgroundColor:设置背景色
  20. fillForegroundColor:设置前景色
  21. shrinkToFit:设置自动单元格自动大小

@HeadFontStyle

用于定制标题字体格式,可作用于类和字段上

  1. fontName:设置字体名称
  2. fontHeightInPoints:设置字体高度
  3. italic:设置字体是否斜体
  4. strikeout:是否设置删除线
  5. color:设置字体颜色
  6. typeOffset:设置偏移量
  7. underline:设置下划线
  8. charset:设置字体编码
  9. bold:设置字体是否加粗

类方法

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

AbstractCellStyleStrategy

通过继承AbstractCellStyleStrategy类,实现其setHeadCellStyle和setContentCellStyle方法可以自定义设置表头和单元格内容样式

public abstract class AbstractCellStyleStrategy implements CellWriteHandler {@Overridepublic int order() {return OrderConstant.DEFINE_STYLE;}@Overridepublic void afterCellDispose(CellWriteHandlerContext context) {if (context.getHead() == null) {return;}if (context.getHead()) {setHeadCellStyle(context);} else {setContentCellStyle(context);}}/*** 设置表头样式*/protected void setHeadCellStyle(CellWriteHandlerContext context) {setHeadCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex());}/*** 设置表头样式*/protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {throw new UnsupportedOperationException("Custom styles must override the setHeadCellStyle method.");}/*** 设置单元格内容样式*/protected void setContentCellStyle(CellWriteHandlerContext context) {setContentCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex());}/*** 设置单元格内容样式*/protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {throw new UnsupportedOperationException("Custom styles must override the setContentCellStyle method.");}
}

HorizontalCellStyleStrategy

HorizontalCellStyleStrategy 是提供的一个水平样式策略,只需实现CellWriteHandlerContext类自定义样式即可,通过设置构建这个策略对象基本上可以满足一般的要求了,比如:设置表头和内容的边框、底色、对齐方式、文字字体、文字颜色、文字大小等。设置完之后,需要创建 HorizontalCellStyleStrategy 对象,然后在导出文件时注册这个策略的 handler 即可。

Getter
@Setter
@EqualsAndHashCode
public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy {private WriteCellStyle headWriteCellStyle;private List<WriteCellStyle> contentWriteCellStyleList;public HorizontalCellStyleStrategy() {}public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle,List<WriteCellStyle> contentWriteCellStyleList) {this.headWriteCellStyle = headWriteCellStyle;this.contentWriteCellStyleList = contentWriteCellStyleList;}public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {this.headWriteCellStyle = headWriteCellStyle;if (contentWriteCellStyle != null) {this.contentWriteCellStyleList = ListUtils.newArrayList(contentWriteCellStyle);}}/*** 设置表头样式*/@Overrideprotected void setHeadCellStyle(CellWriteHandlerContext context) {if (stopProcessing(context) || headWriteCellStyle == null) {return;}WriteCellData<?> cellData = context.getFirstCellData();WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle());}/*** 设置单元格内容样式*/@Overrideprotected void setContentCellStyle(CellWriteHandlerContext context) {if (stopProcessing(context) || CollectionUtils.isEmpty(contentWriteCellStyleList)) {return;}WriteCellData<?> cellData = context.getFirstCellData();if (context.getRelativeRowIndex() == null || context.getRelativeRowIndex() <= 0) {WriteCellStyle.merge(contentWriteCellStyleList.get(0), cellData.getOrCreateStyle());} else {WriteCellStyle.merge(contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size()),cellData.getOrCreateStyle());}}/*** 判断单元格是否为空*/protected boolean stopProcessing(CellWriteHandlerContext context) {return context.getFirstCellData() == null;}
}

DefaultStyle

Easyexcel在导出文件内容时默认使用DefaultStyle类来设置表头和单元格样式

public class DefaultStyle extends HorizontalCellStyleStrategy {@Overridepublic int order() {return OrderConstant.DEFAULT_DEFINE_STYLE;}public DefaultStyle() {super();WriteCellStyle headWriteCellStyle = new WriteCellStyle();headWriteCellStyle.setWrapped(true);headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);headWriteCellStyle.setLocked(true);headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());headWriteCellStyle.setBorderTop(BorderStyle.THIN);headWriteCellStyle.setBorderBottom(BorderStyle.THIN);headWriteCellStyle.setBorderLeft(BorderStyle.THIN);headWriteCellStyle.setBorderRight(BorderStyle.THIN);WriteFont headWriteFont = new WriteFont();headWriteFont.setFontName("宋体");headWriteFont.setFontHeightInPoints((short)14);headWriteFont.setBold(true);headWriteCellStyle.setWriteFont(headWriteFont);setHeadWriteCellStyle(headWriteCellStyle);}
}

Easyexcel的默认风格是,对于表头会显示灰色背景,但是如果取消使用默认风格之后,表头的背景颜色就会恢复白色

Easyexcel控制是否开启默认风格的属性方法是:useDefaultStyle(false)

注意:useDefaultStyle方法一定要放在sheet方法前面,否则会失效(放在后面会因为在构造ExcelWriter类时获取不到useDefaultStyle的值,从而导致使用的是默认风格)

public void test01() {EasyExcel.write("test01.xlsx", DemoData.class)//禁用默认风格.useDefaultStyle(false).sheet("testSheet01").doWrite(demoData);
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

样式工具类

/*** EasyExcel 样式工具类*/
public class CustomExcelStyleUtil {/*** 标题样式** @return*/public static WriteCellStyle getHeadStyle() {// 头的策略WriteCellStyle headWriteCellStyle = new WriteCellStyle();// 背景颜色//headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE1.getIndex());//headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);// 字体WriteFont headWriteFont = new WriteFont();headWriteFont.setFontName("宋体");//设置字体名字headWriteFont.setFontHeightInPoints((short) 14);//设置字体大小headWriteFont.setBold(true);//字体加粗headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;// 样式headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;headWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;headWriteCellStyle.setWrapped(true);  //设置自动换行;headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适return headWriteCellStyle;}/*** 内容样式** @return*/public static WriteCellStyle getContentStyle() {// 内容的策略WriteCellStyle contentWriteCellStyle = new WriteCellStyle();// 背景颜色// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定//contentWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());//contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);// 设置字体WriteFont contentWriteFont = new WriteFont();contentWriteFont.setFontHeightInPoints((short) 12);//设置字体大小contentWriteFont.setFontName("宋体"); //设置字体名字contentWriteCellStyle.setWriteFont(contentWriteFont);//在样式用应用设置的字体;//设置样式;contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;contentWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;contentWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;contentWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;contentWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;contentWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;contentWriteCellStyle.setTopBorderColor((short) 0); ///设置顶边框颜色;contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平居中contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中contentWriteCellStyle.setWrapped(true); //设置自动换行;contentWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适return contentWriteCellStyle;}
}
@Data
public class User {@ExcelProperty(value = "用户Id")private Integer userId;@ExcelProperty(value = "姓名")private String name;@ExcelProperty(value = "手机")private String phone;@ExcelProperty(value = "邮箱")private String email;@ExcelProperty(value = "创建时间")private Date createTime;
}
@GetMapping("/download1")
public void download1(HttpServletResponse response) {try {response.setContentType("application/vnd.ms-excel");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");User user = new User();user.setUserId(123);user.setName("as");user.setPhone("15213");user.setEmail("5456");user.setCreateTime(new Date());EasyExcel.write(response.getOutputStream(), User.class).sheet("模板").registerWriteHandler(new HorizontalCellStyleStrategy(CustomExcelStyleUtil.getHeadStyle(), CustomExcelStyleUtil.getContentStyle())).doWrite(Arrays.asList(user));} catch (Exception e) {e.printStackTrace();}
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

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

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

相关文章

FFN层,全称为Feed-Forward Network层;Layer Normalization;Softmax;

目录 FFN层,全称为Feed-Forward Network层 Layer Normalization 操作步骤 归一化和Softmax 归一化解决量纲问题 归一化(Normalization) Softmax FFN层,全称为Feed-Forward Network层 是Transformer架构中的一个关键组件。它本质上是一个简单的多层感知机(MLP),用…

Android OTA 更新面试题及参考答案

目录 什么是 OTA 更新? 什么是 OTA 更新的主要目的? Android OTA 更新是如何与系统的分区机制相互配合的? 什么是 A/B 分区更新,它的优势是什么? Android 系统中的 “System Partition” 和 “Vendor Partition” 有什么区别? 请详细阐述 Android OTA 更新的基本原…

网络研讨会——如何使用Figma、Canva或Sketch设计Delphi移动应用程序

2024年11月30日星期六 - 北京午夜12点 如何使用Figma、Canva或Sketch设计Delphi移动应用程序 专业设计应用程序Figma、Sketch和Canva有大量优秀的应用程序设计等着你去实现。我们看看有什么可用的&#xff0c;并使用一些最好的设计来创建应用程序。。。 立即报名免费在线研讨会…

通用网络安全设备之【防火墙】

概念&#xff1a; 防火墙&#xff08;Firewall&#xff09;&#xff0c;也称防护墙&#xff0c;它是一种位于内部网络与外部网络之间的网络安全防护系统&#xff0c;是一种隔离技术&#xff0c;允许或是限制传输的数据通过。 基于 TCP/IP 协议&#xff0c;主要分为主机型防火…

对于GC方面,在使用Elasticsearch时要注意什么?

大家好&#xff0c;我是锋哥。今天分享关于【对于GC方面&#xff0c;在使用Elasticsearch时要注意什么&#xff1f;】面试题。希望对大家有帮助&#xff1b; 对于GC方面&#xff0c;在使用Elasticsearch时要注意什么&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java…

[仓颉Cangjie刷题模板] 优先队列(含小顶堆实现)

[TOC]([仓颉Cangjie刷题模板] 优先队列(含小顶堆实现) ) 一、 算法&数据结构 1. 描述 堆是一个可以维护实时最大/最小值的数据结构&#xff0c;相比treeset等常数优很多。 常用于维护一组数据的极值贪心问题。2. 复杂度分析 初始化O(n)查询O(1)修改O(lgn) 3. 常见应用…

解决 MySQL 5.7 安装中的常见问题及解决方案

目录 前言1. 安装MySQL 5.7时的常见错误分析1.1 错误原因及表现1.2 错误的根源 2. 解决方案2.1 修改YUM仓库配置2.2 重新尝试安装2.3 处理GPG密钥错误2.4 解决依赖包问题 3. 安装成功后的配置3.1 启动MySQL服务3.2 获取临时密码3.3 修改root密码 4. 结语 前言 在Linux服务器上…

计算机网络 网络安全基础——针对实习面试

目录 网络安全基础你了解被动攻击吗&#xff1f;你了解主动攻击吗&#xff1f;你了解病毒吗&#xff1f;说说基本的防护措施和安全策略&#xff1f; 网络安全基础 网络安全威胁是指任何可能对网络系统造成损害的行为或事件。这些威胁可以是被动的&#xff0c;也可以是主动的。…

oracle小技巧-解决特殊密码字符而导致的exp错误

在使用oracle数据库的时候&#xff0c;我们经常会利用exp工具对某些表进行导出。但有些时候&#xff0c;因我们用户密码为安全性设有特殊字符&#xff0c;导致exp导出时候报&#xff1a;“EXP-00056和ORA-12154”&#xff0c;今天我们就分享下如何通过设置符号隔离的小技巧解决…

【在 PyTorch 中使用 tqdm 显示训练进度条,并解决常见错误TypeError: ‘module‘ object is not callable】

在 PyTorch 中使用 tqdm 显示训练进度条&#xff0c;并解决常见错误TypeError: module object is not callable 在进行深度学习模型训练时&#xff0c;尤其是在处理大规模数据时&#xff0c;实时了解训练过程中的进展是非常重要的。为了实现这一点&#xff0c;我们可以使用 tq…

Taro 鸿蒙技术内幕系列(三) - 多语言场景下的通用事件系统设计

基于 Taro 打造的京东鸿蒙 APP 已跟随鸿蒙 Next 系统公测&#xff0c;本系列文章将深入解析 Taro 如何实现使用 React 开发高性能鸿蒙应用的技术内幕 背景 在鸿蒙生态系统中&#xff0c;虽然原生应用通常基于 ArkTS 实现&#xff0c;但在实际研发过程中发现&#xff0c;使用 C…

【计算机网络】C/C++实现解析Wireshark离线数据包,附源码

直接先上demo 以下是一个完整的示例代码&#xff0c;演示如何使用 pcap_open_offline 函数打开一个捕获文件并读取数据包。 #include <stdio.h> #include <pcap.h>int main(int argc, char **argv) {if (argc ! 2) {fprintf(stderr, "Usage: %s <capture…

PostgreSQL外键全解析:从概念到实践的进阶指南

全文目录&#xff1a; 开篇语目录前言&#xff1a;关于外键你真的懂了吗&#xff1f;&#x1f914;外键的定义和作用 &#x1f4da;如何在PostgreSQL中创建外键 &#x1f331;基本语法示例&#xff1a;建立简单的外键关系 外键约束的各种行为和选项 &#x1f9e9;ON DELETE 与 …

带有悬浮窗功能的Android应用

android api29 gradle 8.9 要求 布局文件 (floating_window_layout.xml): 增加、删除、关闭按钮默认隐藏。使用“开始”按钮来控制这些按钮的显示和隐藏。 服务类 (FloatingWindowService.kt): 实现“开始”按钮的功能&#xff0c;点击时切换增加、删除、关闭按钮的可见性。处…

Day 27 贪心算法 part01

贪心算法其实就是没有什么规律可言,所以大家了解贪心算法 就了解它没有规律的本质就够了。 不用花心思去研究其规律, 没有思路就立刻看题解。 基本贪心的题目 有两个极端,要不就是特简单,要不就是死活想不出来。 学完贪心之后再去看动态规划,就会了解贪心和动规的区别。…

PyQt5控件QWebEngineView(WebView)

PyQt5控件QWebEngineView(WebView) 下载依赖 PyQt5、PyQtWebEngine pip install --index-urlhttps://mirrors.aliyun.com/pypi/simple/ PyQt5 pip install --index-urlhttps://mirrors.aliyun.com/pypi/simple/ PyQtWebEngine加载外部网页 import sys from PyQt5.QtCore im…

ML 系列:第 36 节 — 统计学中的抽样类型

ML 系列&#xff1a;第 36 天 — 统计学中的抽样类型 文章目录 一、说明二、抽样方法三、简单随机抽样四、 Stratified Sampling分层抽样五、 Cluster Sampling 整群抽样六、Systematic Sampling系统抽样七、Convenience Sampling便利抽样八、结论 一、说明 统计学中的抽样类型…

godot游戏引擎_瓦片集和瓦片地图介绍

在 Godot 中&#xff0c;TileSet 和 TileMap 是用于处理瓦片地图的两个关键概念&#xff0c;它们的作用和用途有明显的区别。以下是两者的详细对比&#xff1a; 1. TileSet&#xff08;瓦片集&#xff09; TileSet 是资源&#xff0c;定义瓦片的内容和属性。 特点&#xff1a…

CGMA – Cloth Creation and Simulation for Real-Time

CGMA – 实时布料创建和模拟 Info&#xff1a; 本课程介绍如何将 Marvelous Designer 整合到布料工作流程中以实时创建角色&#xff0c;从软件基础知识到创建逼真和风格化服装的高级技术。本课程将首先介绍软件&#xff0c;通过创建现代、现代的服装&#xff0c;然后深入探讨使…

Springboot组合SpringSecurity安全插件基于密码的验证Demo

Springboot组合SpringSecurity安全插件基于密码的验证Demo!下面的案例&#xff0c;都是基于数据库mysql&#xff0c;用户密码&#xff0c;验证登录的策略demo。 1&#xff1b;引入maven仓库的坐标 <dependency><groupId>org.springframework.boot</groupId>…