当前位置: 首页 > 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://www.xdnf.cn/news/1017289.html

相关文章:

  • 优化 Excel 文件可以提升文件性能、减少文件大小并加快计算速度
  • mysql中替换字符串(正则)
  • mapbox进阶,切片网格生成实现
  • 深入理解Python协程:asyncio、异步并发、事件循环
  • 开疆智能ModbusTCP转Devicenet网关连接三菱PLC与ABB机器人配置案例
  • NAS 年中成果汇报:从入门到高阶的影视/音乐/小说/资源下载 等好玩Docker 全集合
  • Python让自动驾驶“看见未来”:环境建模那些事儿
  • AWS知识点和技术面试模拟题
  • 基于python大数据的nba球员可视化分析系统
  • 大模型驱动数据分析革新:美林数据智能问数解决方案破局传统 BI 痛点
  • CSS基础学习1
  • Python 数据分析10
  • 【Three.js】初识 Three.js
  • 【论文阅读33】滑坡易发性 PINN ( EG2025 )
  • 基于 SpaCy DependencyMatcher 编写复杂依存关系规则实战指南
  • java 将多张图片合成gif动态图
  • 国产数据库StarRocks在数栈轻量化数据开发的全流程实践
  • 普通人怎样用好Deepseek?
  • MySQL 8.0 OCP 英文题库解析(十九)
  • 26-数据结构-线性表2
  • linux alignment fault对齐造成设备挂死问题定位梳理
  • Leetcode 2604. 吃掉所有谷子的最短时间
  • 线性回归原理推导与应用(九):逻辑回归多分类问题的原理与推导
  • 用户通知服务,轻松实现应用与用户的多场景交互
  • 嵌套滚动交互处理总结
  • FastChat 架构拆解:打造类 ChatGPT 私有化部署解决方案的基石
  • python实现鸟类识别系统实现方案
  • Java实现Pdf转Word
  • 打破语言壁垒!DHTMLX Gantt 与 Scheduler 文档正式上线中文等多语言版本!
  • 使用 PolarProxy+Proxifier 解密 TLS 流量