package com.yc.cloud.excel.util; import cn.hutool.poi.excel.ExcelWriter; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFCl ...
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); } }