这是一个由两部分组成的问题:
1) 如何修改 OpenXML TableCell 的前景
OpenXML 的前景TableCell
由 a 的属性定义Run
, 叫做RunProperties
。要为运行添加颜色,您必须添加Color
对象使用Val
财产。
// 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"));
See 参考问题 https://stackoverflow.com/questions/13148679/change-font-color-in-openxml-word-document-c.
2) 如何修改 OpenXML TableCell 的背景
The TableCell
背景可以使用修改TableCellProperties
,与上面类似Run
,它使用RunProperties
。但是,您应用了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))));
See 参考问题 http://www.codeproject.com/Answers/326640/Color-fill-in-Table-first-row-openxml-csharp-net-m.