NPOI word 文字背景色

发布时间 2023-12-04 11:07:20作者: 0Behavior

需求描述:当结论不合格时,文字的背景色需要为灰色,效果如下图

核心代码,paragraph为XWPFParagraph段落。

if(entity.Conclusion=="不合格")
{
    XWPFRun run = paragraph.Runs[0];
    CT_RPr ctrpr = run.GetCTR().rPr;
    if(ctrpr == null)
    {
        ctrpr = run.GetCTR().AddNewRPr();
    }
    CT_Shd ctshd = ctrpr.shd;
    if (ctshd == null)
    {
        ctshd = ctrpr.AddNewShd();
    }
    ctshd.val = ST_Shd.pct50;
    ctshd.color = "#B7B7B7";
}