使用hutool给excel单元格标黄和添加批注
  2sqDzWaoi9Ck 2023年11月01日 54 0
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);
    }
}

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   51   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   104   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
2sqDzWaoi9Ck