当前位置: 首页 > news >正文

EasyExcel 文件导出:表头与内容样式简单设置

文章目录

    • EasyExcel 文件导出 - 最终效果
    • 使用的 EasyExcel的版本
    • 设置表头样式和内容样式
    • 设置自动列宽

EasyExcel 文件导出 - 最终效果

具体的效果可通过修改代码来自行调整。经过调整后的样式与默认样式相比,美观程度大幅提升。

在这里插入图片描述
下面是默认的样式。丑的一批。

在这里插入图片描述

使用的 EasyExcel的版本

我使用的版本是4.0.0+

在这里插入图片描述

设置表头样式和内容样式

首先创建一个写入处理器把表头和内容的样式传入进去,再使用registerWriteHandler进行注册。

            HorizontalCellStyleStrategy horizontalCellStyleStrategy =new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());excelWriter = EasyExcel.write(outputStream, clazz).registerWriteHandler(horizontalCellStyleStrategy).build();

上面的样式工具类。

public class ExcelStyleUtils {/*** 表头样式*/public static WriteCellStyle getHeadStyle() {WriteCellStyle headWriteCellStyle = new WriteCellStyle();// 字体WriteFont headWriteFont = new WriteFont();headWriteFont.setFontName("宋体");//设置字体名字headWriteFont.setFontHeightInPoints((short) 13);//设置字体大小headWriteFont.setBold(true);//字体加粗// 设置字体颜色为白色headWriteFont.setColor(IndexedColors.WHITE.getIndex());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);//设置文本收缩至合适// 设置单元格背景色headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);return headWriteCellStyle;}/*** 内容样式*/public static WriteCellStyle getContentStyle() {WriteCellStyle contentWriteCellStyle = new WriteCellStyle();// 设置字体WriteFont contentWriteFont = new WriteFont();contentWriteFont.setFontHeightInPoints((short) 11);//设置字体大小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;}}

设置自动列宽

下面的ExcelCellWidthStyleStrategy 这个类就是自定义实现自动列宽的类。

            HorizontalCellStyleStrategy horizontalCellStyleStrategy =new HorizontalCellStyleStrategy(ExcelStyleUtils.getHeadStyle(), ExcelStyleUtils.getContentStyle());ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();excelWriter = EasyExcel.write(outputStream, clazz).registerWriteHandler(horizontalCellStyleStrategy).registerWriteHandler(widthStyleStrategy).build();

下面的MAX_COLUMN_WIDTH 参数根据自己的需求进行调整。

public class ExcelCellWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {// 可以根据这里的最大宽度,按自己需要进行调整,搭配单元格样式实现类中的,自动换行,效果更好// 定义最大列宽常量private static final int MAX_COLUMN_WIDTH = 50;// 创建一个用于缓存的哈希表private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap(8);@Overrideprotected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {// 判断是否需要设置列宽,如果是表头或者单元格数据列表不为空则需要设置列宽boolean needSetWidth = isHead ||!CollectionUtils.isEmpty(cellDataList);if (needSetWidth) {// 获取指定工作表编号对应的最大列宽映射表,如果没有则创建一个新的Map<Integer, Integer> maxColumnWidthMap = (Map) CACHE.get(writeSheetHolder.getSheetNo());if (maxColumnWidthMap == null) {maxColumnWidthMap = new HashMap(16);CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);}// 计算列宽Integer columnWidth = this.dataLength(cellDataList, cell, isHead);if (columnWidth >= 0) {// 如果列宽大于最大列宽,则将列宽设置为最大列宽if (columnWidth > MAX_COLUMN_WIDTH) {columnWidth = MAX_COLUMN_WIDTH;}// 获取当前列的最大列宽Integer maxColumnWidth = (Integer) ((Map) maxColumnWidthMap).get(cell.getColumnIndex());if (maxColumnWidth == null || columnWidth > maxColumnWidth) {// 更新最大列宽映射表中的当前列的最大列宽((Map) maxColumnWidthMap).put(cell.getColumnIndex(), columnWidth);// 设置工作表中当前列的宽度writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);}}}}// 计算数据长度的方法private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {if (isHead) {// 如果是表头,返回表头字符串的字节长度return cell.getStringCellValue().getBytes().length;} else {// 获取单元格数据CellData cellData = (CellData) cellDataList.get(0);// 获取单元格数据类型CellDataTypeEnum type = cellData.getType();if (type == null) {// 如果数据类型为空,返回 -1return -1;} else {// 根据不同的数据类型返回相应的值的字节长度switch (type) {case STRING:return cellData.getStringValue().getBytes().length;case BOOLEAN:return cellData.getBooleanValue().toString().getBytes().length;case NUMBER:return cellData.getNumberValue().toString().getBytes().length;default:return -1;}}}}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【Tools】什么是基座模型
  • 机械学习—零基础学习日志(Python做数据分析02)
  • ✨机器学习笔记(三)—— 多元线性回归、特征缩放、Scikit-Learn(未完待续)
  • 大腾智能出席龙华云创中心启动与鸿蒙园揭牌仪式
  • 《花100块做个摸鱼小网站! 》第六篇—将小网站部署到云服务器上
  • 【前端面试】Webpack、Rollup 和 Gulp 构建工具了解
  • 收藏:B站相当精彩的关于向量数据库的2个视频
  • 《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)
  • [数据集][图像分类]熊分类数据集309张5类别黑熊泰迪北极熊等
  • 动手学深度学习(pytorch)学习记录25-汇聚层(池化层)[学习记录]
  • AIGC与数据分析融合,引领商业智能新变革(TOP企业实践)
  • iOS——GCD再学习
  • rexroth拧紧系统
  • 使用EDM邮件群发营销管理平台发送推广邮件在什么时间段发信效果最好
  • 【C++ Primer Plus习题】14.1
  • [Vue CLI 3] 配置解析之 css.extract
  • 【翻译】babel对TC39装饰器草案的实现
  • E-HPC支持多队列管理和自动伸缩
  • es6--symbol
  • ESLint简单操作
  • Java 网络编程(2):UDP 的使用
  • Java精华积累:初学者都应该搞懂的问题
  • Laravel Mix运行时关于es2015报错解决方案
  • PermissionScope Swift4 兼容问题
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • 阿里云前端周刊 - 第 26 期
  • 闭包,sync使用细节
  • 从零搭建Koa2 Server
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 基于web的全景—— Pannellum小试
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 容器服务kubernetes弹性伸缩高级用法
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 深入 Nginx 之配置篇
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 数据仓库的几种建模方法
  • 学习ES6 变量的解构赋值
  • ​【已解决】npm install​卡主不动的情况
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # 数据结构
  • #pragam once 和 #ifndef 预编译头
  • #宝哥教你#查看jquery绑定的事件函数
  • (done) 两个矩阵 “相似” 是什么意思?
  • (JS基础)String 类型
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (九)One-Wire总线-DS18B20
  • (每日一问)设计模式:设计模式的原则与分类——如何提升代码质量?
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (一)appium-desktop定位元素原理
  • (转)Windows2003安全设置/维护
  • .bat批处理(九):替换带有等号=的字符串的子串