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

Java + easyexcel 新旧数据对比,单元格值标红

说明:Java + easyexcel 复制excel模版样式及表头,写入新数据,并对比旧数据,数据不一致标红

pom

		<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>4.0.3</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.5</version></dependency>

数据对比处理器

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.example.entity.Equipment;
import org.apache.poi.ss.usermodel.*;import java.util.HashMap;
import java.util.List;
import java.util.Map;public class CustomValueHandler implements CellWriteHandler {private final Map<Integer, Map<Integer, String>> originalValues;public CustomValueHandler(List<Equipment> originalList) {this.originalValues = new HashMap<>();for (int i = 0; i < originalList.size(); i++){Map<Integer, String> rowMap = new HashMap<>();Equipment item = originalList.get(i);rowMap.put(0, item.getEquipCode());rowMap.put(1, item.getEquipName());rowMap.put(2, item.getEquipRule());rowMap.put(3, item.getEquipState());rowMap.put(4, item.getEquipNum());//我这里的表头是4行,所以序号要加4this.originalValues.put(i + 4, rowMap);}}@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder,WriteTableHolder writeTableHolder,List<WriteCellData<?>> cellDataList,Cell cell, Head head,Integer relativeRowIndex,Boolean isHead) {String stringCellValue = cell.getStringCellValue();int rowIndex = cell.getRowIndex();int columnIndex = cell.getColumnIndex();if(originalValues.containsKey(rowIndex)) {String original = originalValues.get(rowIndex).get(columnIndex);if(original != null && !original.trim().equals(stringCellValue.trim())) {Workbook workbook = writeSheetHolder.getSheet().getWorkbook();CellStyle originalStyle = cell.getCellStyle();CellStyle style = workbook.createCellStyle();//复制原有样式属性style.cloneStyleFrom(originalStyle);Font font = workbook.createFont();font.setColor(IndexedColors.RED.getIndex());style.setFont(font);// 保留原有背景色(不设置背景色)cell.setCellStyle(style);}}}
}

excel处理器

public class EasyExcelDynamicFilter implements SheetWriteHandler {private final int rowIndex;public EasyExcelDynamicFilter(int rowIndex) {this.rowIndex = rowIndex;}@Overridepublic void afterSheetCreate(SheetWriteHandlerContext context) {Sheet sheet = context.getWriteSheetHolder().getSheet();// 保留前4行(索引0-3),删除其余行for (int i = rowIndex; i <= sheet.getLastRowNum(); i++) {if (sheet.getRow(i) != null) {sheet.removeRow(sheet.getRow(i));}}}
}

业务处理

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
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.poi.ss.usermodel.Cell;
import org.example.handler.DynamicFilter;import java.io.IOException;
import java.util.List;public class ExcelHeaderUtil {/*** 复制模板前4行并清除后续内容** @param templatePath 模板路径* @param outputPath 输出路径* @param originalList 初始数据* @param data 新数据*/public static void copyRows(String templatePath,String outputPath,List<Equipment> data, List<Equipment> originalList) throws IOException {// 创建样式拦截器CellWriteHandler styleHandler = new CellWriteHandler() {@Overridepublic void afterCellCreate(WriteSheetHolder sheetHolder,WriteTableHolder tableHolder,Cell cell, Head head,Integer relativeRowIndex,Boolean isHead) {}};// 构建写入器ExcelWriter writer = EasyExcel.write(outputPath).withTemplate(templatePath).registerWriteHandler(new EasyExcelDynamicFilter(4)).registerWriteHandler(new CustomValueHandler(originalList)).registerWriteHandler(styleHandler).build();// 触发模板处理writer.write(data, EasyExcel.writerSheet().build());writer.finish();  // 必须显式关闭}
}


文章转载自:

http://jiFO06UP.tsfLw.cn
http://930wMgyu.tsfLw.cn
http://EbmV9jPt.tsfLw.cn
http://wfX1KNOC.tsfLw.cn
http://LGMCjDKZ.tsfLw.cn
http://dzfCseX0.tsfLw.cn
http://0JHQuuZt.tsfLw.cn
http://7GFTfoLq.tsfLw.cn
http://fKxTYIVt.tsfLw.cn
http://BXrQkRwM.tsfLw.cn
http://6PhlwPzz.tsfLw.cn
http://RN8FyuXO.tsfLw.cn
http://f2xCwdzv.tsfLw.cn
http://BIFrcLAM.tsfLw.cn
http://i3OEoWmy.tsfLw.cn
http://W06b2HhT.tsfLw.cn
http://nrpeyK0o.tsfLw.cn
http://d2zC5w5a.tsfLw.cn
http://U2SieVgM.tsfLw.cn
http://od8Xzzt1.tsfLw.cn
http://rk4vvXNH.tsfLw.cn
http://PZYsLM4f.tsfLw.cn
http://zi4NX3CR.tsfLw.cn
http://lEoiYuKf.tsfLw.cn
http://X0yRZZqV.tsfLw.cn
http://aVnJ7qyE.tsfLw.cn
http://6Qy6SsBM.tsfLw.cn
http://rl3OUuwq.tsfLw.cn
http://aKY42wus.tsfLw.cn
http://FjQpzc7M.tsfLw.cn
http://www.dtcms.com/a/248297.html

相关文章:

  • 穿越时空的刀剑之旅:走进VR刀剑博物馆​
  • 720云vr全景怎么制作?720全景制作费用?
  • PHP基础-控制结构
  • 【Zephyr 系列 22】从单机开发到平台化:构建你自己的 Zephyr 物联网开发平台
  • 实战案例-JESD204B 多器件同步
  • 网络原理:网段划分
  • 北斗导航 | 基于MATLAB的卫星导航单点定位算法(卡尔曼滤波增强)
  • 对称加密和非对称加密
  • Python 数据分析10
  • 【Python 进阶系列】第4篇:初探 Python Pandas 数据分析的世界
  • 大模型驱动数据分析革新:美林数据智能问数解决方案破局传统 BI 痛点
  • 【60 Pandas+Pyecharts | 箱包订单数据分析可视化】
  • Spring Boot自动配置原理与实践
  • 一文掌握 Windows 文件传输:5 种命令行工具的原理、参数与示例
  • UR机器人解锁关节扭矩控制:利用英伟达Isaac Lab框架,推动装配自动化的Sim2Real迁移
  • electron-builder打包配置(应用名、安装包、图标、快捷方式、自定义文件关联启动等)
  • 全新RA2L2 MCU超低功耗、支持CAN及USB-C、拓展工业及便携式应用
  • 护城河尚浅,理想汽车驶入慢车道
  • torch 高维矩阵乘法分析,一文说透
  • Linux set 命令
  • Vue的双向绑定魔法:如何让数据与视图‘心有灵犀’?
  • Spring Boot 集成国内AI,包含文心一言、通义千问和讯飞星火平台实战教程
  • python学习打卡day52
  • 哈希表三种数据结构在leetcode中的使用情况分析
  • 前端给一行文字不设置宽度 ,不拆分 ,又能让某几个字在视觉下方居中显示
  • 打破语言壁垒!DHTMLX Gantt 与 Scheduler 文档正式上线中文等多语言版本!
  • Vue3本地存储实现方案
  • vue配置代理的两种方式
  • 洛谷:B3799 [NICA #1] 序列
  • Go语言中的可重入函数与不可重入函数