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

【EasyExcel】动态替换表头内容并应用样式

1.定义实体类

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.ContentStyle;
import com.alibaba.excel.metadata.BorderStyleEnum;
import com.alibaba.excel.metadata.VerticalAlignmentEnum;
import com.alibaba.excel.metadata.BooleanEnum;@Data
@HeadRowHeight(40)
@ContentRowHeight(30)
@ExcelIgnoreUnannotated
@ContentStyle(wrapped = BooleanEnum.TRUE,borderBottom = BorderStyleEnum.THIN,borderLeft = BorderStyleEnum.THIN,borderRight = BorderStyleEnum.THIN,borderTop = BorderStyleEnum.THIN,verticalAlignment = VerticalAlignmentEnum.CENTER,horizontalAlignment = HorizontalAlignmentEnum.CENTER
)
public class Product {@ColumnWidth(28)@ExcelProperty(value = {"${cellValue}经营统计表", "${cellWriterValue}", "产品名称"}, index = 0)private String productName;@ColumnWidth(28)@ExcelProperty(value = {"${cellValue}经营统计表", "${cellWriterValue}", "产品类别"}, index = 1)private String productCategory;@ColumnWidth(28)@ExcelProperty(value = {"${cellValue}经营统计表", "${cellWriterValue}", "价格"}, index = 2)private Double price;
}

2.自定义处理器

package com.example.util;import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.util.PropertyPlaceholderHelper;import java.util.List;
import java.util.Properties;public class DynamicValueCellWriteHandler implements CellWriteHandler {private String cellValue;private String cellWriterValue;private PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");public DynamicValueCellWriteHandler(String cellValue, String cellWriterValue) {this.cellValue = cellValue;this.cellWriterValue = cellWriterValue;}@Overridepublic void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {if (head != null) {List<String> headNameList = head.getHeadNameList();if (CollectionUtils.isNotEmpty(headNameList)) {Properties properties = new Properties();properties.setProperty("cellValue", cellValue);properties.setProperty("cellWriterValue", cellWriterValue);for (int i = 0; i < headNameList.size(); i++) {headNameList.set(i, propertyPlaceholderHelper.replacePlaceholders(headNameList.get(i), properties));}}}}
}

3.在写入时使用自定义处理器

 excelWriter.write(excelCategoryDataList, EasyExcel.writerSheet(sheetName).head(Product.class).registerWriteHandler(new DynamicValueCellWriteHandler(cellValue, cellWriterValue)).build());

封装成通用方法

public class DynamicValueCellWriteHandler implements CellWriteHandler {private Map<String, String> placeholders;private PropertyPlaceholderHelper propertyPlaceholderHelper = new PropertyPlaceholderHelper("${", "}");public DynamicValueCellWriteHandler(Map<String, String> placeholders) {this.placeholders = placeholders;}@Overridepublic void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {if (head != null) {List<String> headNameList = head.getHeadNameList();if (CollectionUtils.isNotEmpty(headNameList)) {Properties properties = new Properties();for (Map.Entry<String, String> entry : placeholders.entrySet()) {properties.setProperty(entry.getKey(), entry.getValue());}headNameList.replaceAll(s -> propertyPlaceholderHelper.replacePlaceholders(s, properties));}}}
}
Map<String, String> placeholders = new HashMap<>();
placeholders.put("value", "someValue");
placeholders.put("cellValue", "someCellValue");
placeholders.put("cellWriterValue", "someWriterValue");
placeholders.put("additionalPlaceholder1", "additionalValue1");
placeholders.put("additionalPlaceholder2", "additionalValue2");.registerWriteHandler(new DynamicValueCellWriteHandler(placeholders ))
http://www.lryc.cn/news/400297.html

相关文章:

  • RocketMQ实现分布式事务
  • 【Rust练习】2.数值类型
  • 通过 PPPOE 将 linux 服务器作为本地局域网 IPv4 外网网关
  • gin源码分析
  • 数学建模入门
  • 【学习笔记】无人机(UAV)在3GPP系统中的增强支持(十二)-无人机群在物流中的应用
  • 同三维T80006EH2-4K30编码器视频使用操作说明书:高清HDMI编码器,高清SDI编码器,4K超清HDMI编码器,双路4K超高清编码器
  • DHCP原理及配置
  • 异步日志:性能优化的金钥匙
  • matlab仿真 模拟调制(上)
  • 【数据结构】--- 堆的应用
  • 0基础学会在亚马逊云科技AWS上利用SageMaker、PEFT和LoRA高效微调AI大语言模型(含具体教程和代码)
  • 护网HW面试——redis利用方式即复现
  • C++ //练习 15.8 给出静态类型和动态类型的定义。
  • 阿里云ECS服务器安装jdk并运行jar包,访问成功详解
  • Windows系统上使用npm来安装和配置Yarn,在VSCode中使用
  • Unity ColorSpace 之 【颜色空间】相关说明,以及【Linear】颜色校正 【Gamma】的简单整理
  • JavaScript的学习(二)
  • 【接口自动化_06课_Pytest+Excel+Allure完整框架集成】
  • Profibus协议转Profinet协议网关模块连接智能电表通讯案例
  • 【学习笔记】无人机(UAV)在3GPP系统中的增强支持(九)-无人机服务区分离
  • acrobat 中 PDF 复制时不能精确选中所选内容所在行的一种解决方法
  • 安卓学习中遇到的问题【bug】
  • 【日常记录】【CSS】display:inline 的样式截断
  • 数据库系统安全
  • Qt MV架构-代理模型
  • WebSocket实现群聊功能、房间隔离
  • 顶顶通呼叫中心中间件实现随时启动和停止质检(mod_cti基于FreeSWITCH)
  • 基于conda包的环境创建、激活、管理与删除
  • 处理线程安全的列表CopyOnWriteArrayList 和Collections.synchronizedList