当前位置: 首页 > 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();  // 必须显式关闭}
}

相关文章:

  • 穿越时空的刀剑之旅:走进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 命令
  • 做网站赚多少/抖音营销软件
  • 视频素材交易网站建设/品牌推广与传播怎么写
  • 做网站的软件dw/免费二级域名生成网站
  • 营销导向的网站建设的主要流程/网络运营具体做什么
  • 网站建设公司营业执照/海外营销公司
  • 徐州网站建设公司官网/做电商一个月能挣多少钱