如何修改OpenXML TableCell的前景色和背景色?

10

我正在按照如下方式创建表格单元格:

private static TableCell GetHeaderCell(string cellText)
{
    var tc = new TableCell(new Paragraph(new Run(new Text(cellText))));
    return tc;
}

我希望它是蓝色的,白色的字。

我尝试了以下方法,但不起作用。当我尝试打开文档时,出现内容有问题的错误:

private static TableCell GetHeaderCell(string cellText)
{
    var props = new TableCellProperties();
    var solidFill = new SolidFill();
    var rgbColorHex = new RgbColorModelHex() { Val = "FF0000" };//Red Background for Single TableCell.

    solidFill.Append(rgbColorHex);        
    props.Append(solidFill);

    var paragraph = new Paragraph(new Run(new Text(cellText)));

    var tc = new TableCell(paragraph, props);

    return tc;
}
完整的错误信息如下:

输入图像描述


请问您能否告诉我们完整的错误信息以及出错的代码行? - Microsoft DN
我已经包含了一个错误的图像。那里真的没有太多信息。我能在其他地方找到吗? - DaveDev
可能是你的文件损坏了。试着手动打开同一个文件。如果它能打开,那么代码本身可能有一些错误。 - Microsoft DN
我正在尝试用Word打开文件。这是消息显示的内容。 - DaveDev
有一个适用于MS Word的解决方案:打开并修复。这将修复您损坏的.docx文件,然后您就可以打开它了。 - Microsoft DN
不幸的是,太多文档都是写成“这段代码实现了那种效果。”但用户需要的是“要获得那种效果,请编写这段代码。”我一直在learn.microsoft.com上寻找这些信息,差点就要在stackoverflow.com上提问了。也许微软(和其他公司)会注意到这一点。 - TheBick
2个回答

23

这是一个由两个部分组成的问题:

1) 如何修改 OpenXML TableCell 的前景色

OpenXML TableCell 的前景色由一个称为RunPropertiesRun属性定义。要添加颜色到运行中,您必须使用Val属性添加Color对象。

// Create the RunProperties object for your run
DocumentFormat.OpenXml.Wordprocessing.RunProperties rp = 
    new DocumentFormat.OpenXml.Wordprocessing.RunProperties();
// Add the Color object for your run into the RunProperties
rp.Append(DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "ABCDEF" }); 
// Create the Run object
DocumentFormat.OpenXml.WordProcessing.Run run = 
    new DocumentFormat.OpenXml.WordProcessing.Run();
// Assign your RunProperties to your Run
run.RunProperties = rp;
// Add your text to your Run
run.Append(new Text("My Text"));

请参阅参考问题

2) 如何修改OpenXML TableCell的背景

您可以使用TableCellProperties来修改TableCell的背景,类似于上面使用RunProperties来修改Run一样。不过,您需要将Shading对象应用于您的TableCellProperties

// Create the TableCell object
DocumentFormat.OpenXml.Wordprocessing.TableCell tc = 
    new DocumentFormat.OpenXml.Wordprocessing.TableCell();
// Create the TableCellProperties object
TableCellProperties tcp = new TableCellProperties(
    new TableCellWidth { Type = TableWidthUnitValues.Auto, }
);
// Create the Shading object
DocumentFormat.OpenXml.Wordprocessing.Shading shading = 
    new DocumentFormat.OpenXml.Wordprocessing.Shading() {
    Color = "auto",
    Fill = "ABCDEF",
    Val = ShadingPatternValues.Clear
};
// Add the Shading object to the TableCellProperties object
tcp.Append(shading);
// Add the TableCellProperties object to the TableCell object
tc.Append(tcp);

// also need to ensure you include the text, otherwise it causes an error (it did for me!)
tc.Append(new Paragraph(new Run(new Text(cellText))));

请参考相关问题


@DaveDev 哈哈,好吧,糟糕了。你明白我的意思! - Bob.
DocumentFormat.OpenXml.Wordprocessing.Color的Val如何工作? 它是十六进制颜色代码吗? - Cirelli94

-2
DocumentFormat.OpenXml.WordProcessingRunProperties rp = 
    new DocumentFormat.OpenXml.WordProcessingRunProperties();

=

DocumentFormat.OpenXml.WordProcessing.RunProperties rp = 
    new DocumentFormat.OpenXml.WordProcessing.RunProperties();

??


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接