EasyExcel设置动态表头的时候,动态设置字体颜色不生效问题解决

EasyExcel设置动态表头的时候,动态设置字体颜色不生效的问题

需求:是动态的设置excel表头,并且请求体的内容需要使用字体标红

官方没有例子,并且因为是动态表头,所以在初始化ExcelWriterBuilder的时候,没有办法直接注册为pojo类继而使用@ContentStyle注解来处理字体样式,使用了网上大多数的例子,但是都无法达到我的需求。

官方链接:

https://easyexcel.opensource.alibaba.com

1.首先尝试使用了registerWriteHandler方法,但是并不生效

EasyExcel.write(fileName).registerWriteHandler(new CellWriteHandler(){
	//并实现了所有内部方法
})

2.查查源码看源码里面设置的是如何生效的。

①在com.alibaba.excel.util.ClassUtils里读取了Class上标注的信息,之后和ExcelContentProperty产生了联系

②在com.alibaba.excel.util.ClassUtils-184L看见了,向ExcelContentProperty中设置了contentFontProperty属性

③查询contentFontProperty属性的方法的过程中,在com.alibaba.excel.write.metadata.holder.AbstractWriteHolder中的dealStyle中获取了getContentFontProperty属性

④通过看上下文,是new了一个com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy抽象类,然后放入了一个list中。

⑤通过官方API中可以发现**EasyExcel.write(fileName).registerWriteHandler(WriteHandler writehandler)**中,注册handler的时候是一个list中。

⑥初始化字体的时候使用了AbstractVerticalCellStyleStrategy抽象类,我们也使用试一下,试一下之后就成功了。
请添加图片描述

直接看答案

效果

请添加图片描述

代码

项目地址:https://gitee.com/wangyiSelf/LearnToGrow/tree/master/%E6%BA%90%E7%A0%81/EasyExcelSetDynamicHeaderAndContentFont

maven仓库

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
    </dependencies>

静态代码

package com.wangyi.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.ImmutableList;
import com.wangyi.excel.handler.ExampleDataRedColorHandler;
import org.junit.Test;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 通过EasyExcel生成动态表头和设置红色字体
 */
public class EasyExcelSetDynamicHeaderAndContentFont {

    @Test
    public void generateExcelAndSetDynamicHeaderAndContentFont() throws UnsupportedEncodingException {
        String path = EasyExcelSetDynamicHeaderAndContentFont.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String parentPath = new File(path).getParent();
        parentPath = URLDecoder.decode(parentPath, "utf-8");
        ExcelWriter excelWriter = EasyExcel.write(parentPath + "/DynamicHeaderContentFont" + System.currentTimeMillis() + ".xlsx").registerWriteHandler(new ExampleDataRedColorHandler()).build();
        List<String> column1 = ImmutableList.of("表头1");
        List<String> column2 = ImmutableList.of("表头2");
        ImmutableList<List<String>> head = ImmutableList.of(column1, column2);
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "模板").head(head).build();
        List<String> value1 = ImmutableList.of("表头1","表头1");
        List<String> value2 = ImmutableList.of("表头2","表头2");
        List<List<String>> writeData = ImmutableList.of(value1, value2);
        excelWriter.write(writeData, writeSheet);
        excelWriter.finish();
    }
}

package com.wangyi.excel.handler;

import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.FontProperty;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * 示例数据红色标记实现类
 */
public class ExampleDataRedColorHandler extends AbstractVerticalCellStyleStrategy {

    @Override
    protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
        ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
        FontProperty contentFontProperty = excelContentProperty.getContentFontProperty();
        if (contentFontProperty == null) {
            contentFontProperty = new FontProperty();
        }
        contentFontProperty.setColor(IndexedColors.RED.getIndex());
        return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(),
                contentFontProperty);
    }
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值