我如何修改的OpenXML的TableCell的前景色和背景色? [英] How can I modify the foreground and background color of an OpenXML TableCell?
问题描述
我在创建表的单元格如下:
I'm creating the table cell as follows:
private static TableCell GetHeaderCell(string cellText)
{
var tc = new TableCell(new Paragraph(new Run(new Text(cellText))));
return tc;
}
我想它是蓝色的,白色的文字。
I want it to be blue with white text.
我试过以下,但它不工作;当我尝试打开该文档,我得到一个错误,没有与内容的问题:
I've tried the following, but it doesn't work; when I try to open the document I get an error that there is a problem with the contents:
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;
}
完整的错误如下:
The full error is as follows:
推荐答案
这是一个问题的两个部分:
This is a two part question:
1)我如何修改的OpenXML的TableCell的前景
但是OpenXML 的TableCell
的前景是由运行的属性定义
,叫做 RunProperties
。要将颜色添加到运行,你必须使用瓦尔
属性添加颜色
对象。
The foreground of an OpenXML TableCell
is defined by the properties of a Run
, called the RunProperties
. To add a color to a run, you have to add the Color
object using the Val
property.
// 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的
的TableCell
背景可以使用 TableCellProperties 修改code>,上述
运行
,它使用 RunProperties
。但是,您应用阴影
对象到你的 TableCellProperties
。
The TableCell
background can be modified using the TableCellProperties
, similar to the above Run
, which uses RunProperties
. However, you apply a Shading
object to your 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))));
请参阅的引用问题。
这篇关于我如何修改的OpenXML的TableCell的前景色和背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!