EasyExcel实现对应表格头带 “批注”

package com.ly.cloud.util;

import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

/**
 * @Author 
 * @Date Created in  2023/12/22 9:30
 * @DESCRIPTION: 对 EasyExcel 添加 批注
 * @Version V1.0
 */

public class CommentWriteHandler implements RowWriteHandler {
    private static final String PI_ZHU = "由系统自动带出,无需修改";
    private static final String PI_ZHU_TWO = "如果没有,可以不填;如果有多个审核人,用英文逗号“,”隔开";
    @Override
    public void afterRowDispose(RowWriteHandlerContext context) {
        if (BooleanUtils.isTrue(context.getHead())) {
            Sheet sheet = context.getWriteSheetHolder().getSheet();
            Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
            // 假设 drawingPatriarch 是 DrawingPatriarch 对象

            // 第一行第二列
            Comment comment1 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 1, 0, (short) 2, 1));
            comment1.setString(new XSSFRichTextString(PI_ZHU));
            // 将批注添加到单元格对象中
            sheet.getRow(0).getCell(1).setCellComment(comment1);

            // 第一行第四列
            Comment comment2 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 0, (short) 4, 1));
            comment2.setString(new XSSFRichTextString(PI_ZHU));
            sheet.getRow(0).getCell(3).setCellComment(comment2);
            // 第一行第六列
            Comment comment3 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 5, 0, (short) 6, 1));
            comment3.setString(new XSSFRichTextString(PI_ZHU_TWO));
            sheet.getRow(0).getCell(5).setCellComment(comment3);
            // 第一行第七列
            Comment comment4 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 6, 0, (short) 7, 1));
            comment4.setString(new XSSFRichTextString(PI_ZHU_TWO));
            sheet.getRow(0).getCell(6).setCellComment(comment4);

            // 第一行第八列
            Comment comment5 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 7, 0, (short) 8, 1));
            comment5.setString(new XSSFRichTextString(PI_ZHU_TWO));
            sheet.getRow(0).getCell(7).setCellComment(comment5);

            // 第一行第九列
            Comment comment6 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 8, 0, (short) 9, 1));
            comment6.setString(new XSSFRichTextString(PI_ZHU_TWO));
            sheet.getRow(0).getCell(8).setCellComment(comment6);

            // 第一行第十列
            Comment comment7 = drawingPatriarch.createCellComment(
                    new XSSFClientAnchor(0, 0, 0, 0, (short) 9, 0, (short) 10, 1));
            comment7.setString(new XSSFRichTextString(PI_ZHU_TWO));
            sheet.getRow(0).getCell(9).setCellComment(comment7);

        }
    }
}

 调用:

          //内部finish的时候会自动关闭流
            EasyExcel.write(response.getOutputStream(), PzrtExportDto.class)
 --添加批注
                    .inMemory(Boolean.TRUE).registerWriteHandler(new CommentWriteHandler())
                    .sheet(FILE_NAME)
                    .doWrite(pzry);

效果图:

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用EasyExcel自定义设置表头颜色和批注的步骤如下: 1. 导入相关的依赖包: ```java import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; ``` 2. 创建一个类来自定义样式,继承`CellStyle`类,并实现`setCellStyle`方法: ```java public class CustomCellStyle extends CellStyle { private static final short CUSTOM_COLOR = IndexedColors.YELLOW.getIndex(); @Override public void setCellStyle(Cell cell) { Workbook workbook = cell.getSheet().getWorkbook(); CellStyle style = workbook.createCellStyle(); // 设置背景颜色 style.setFillForegroundColor(CUSTOM_COLOR); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置批注 Drawing<?> drawing = cell.getSheet().createDrawingPatriarch(); CreationHelper factory = workbook.getCreationHelper(); ClientAnchor anchor = factory.createClientAnchor(); Comment comment = drawing.createCellComment(anchor); RichTextString richTextString = factory.createRichTextString("这是一个批注"); comment.setString(richTextString); cell.setCellComment(comment); cell.setCellStyle(style); } } ``` 3. 在代码中使用自定义样式: ```java public class Main { public static void main(String[] args) { // 读取Excel文件 List<List<Object>> data = EasyExcel.read("input.xlsx").sheet().doReadSync(); // 写入Excel文件 EasyExcel.write("output.xlsx") .sheet() .registerWriteHandler(new SimpleColumnWidthStyleStrategy()) .registerWriteHandler(new SimpleRowHeightStyleStrategy()) .registerWriteHandler(new CustomCellStyle()) // 注册自定义样式 .doWrite(data); } } ``` 以上代码将会将输入文件中的数据写入到输出文件中,并在表头单元格上设置黄色背景色和批注内容为"这是一个批注"。 注意:以上示例代码仅适用于`.xlsx`格式的Excel文件。如果需要处理`.xls`格式的Excel文件,需要使用`HSSFWorkbook`替代`XSSFWorkbook`。另外,如果需要自定义其他样式,可以参考`CellStyle`类的相关方法来设置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

入夏忆梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值