使用hutool给excel单元格标黄和添加批注
package com.yc.cloud.excel.util; import cn.hutool.poi.excel.ExcelWriter; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFRichTextString; /** * Excel工作类扩展 * * @author wanghuidong * 时间: 2022/10/10 18:58 */ @Slf4j public class ExcelUtilExt { /** * 给单元格标黄 * * @param excelWriter excel写对象 * @param x 列号 * @param y 行号 */ public static void markCellYellow(ExcelWriter excelWriter, int x, int y) { Cell cell = excelWriter.getCell(x, y); CellStyle cellStyleSrc = cell.getCellStyle(); //必须新创建单元格样式,直接修改原单元格样式可能影响到其它单元格,因为样式可以复用的 CellStyle cellStyleDest = excelWriter.createCellStyle(x, y); //原单元格样式不为空,先拷贝原单元格样式至新创建的单元格样式 if (cellStyleSrc != null) { cellStyleDest.cloneStyleFrom(cellStyleSrc); } cellStyleDest.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyleDest.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); } /** * 给Cell添加批注 * * @param cell 单元格 * @param value 批注内容 * @param isXlsx 是否是xlsx格式的文档 */ public static void addCellComment(Cell cell, String value, boolean isXlsx) { Sheet sheet = cell.getSheet(); cell.removeCellComment(); Drawing drawing = sheet.createDrawingPatriarch(); Comment comment; if (isXlsx) { // 创建批注 comment = drawing.createCellComment(new XSSFClientAnchor(1, 1, 1, 1, 1, 1, 1, 1)); // 输入批注信息 comment.setString(new XSSFRichTextString(value)); // 将批注添加到单元格对象中 } else { // 创建批注 comment = drawing.createCellComment(new HSSFClientAnchor(1, 1, 1, 1, (short) 1, 1, (short) 1, 1)); // 输入批注信息 comment.setString(new HSSFRichTextString(value)); // 将批注添加到单元格对象中 } cell.setCellComment(comment); } }
分类:
java
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库