Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify the foreground and background color of an OpenXML TableCell?

Tags:

c#

openxml

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:

enter image description here

like image 393
DaveDev Avatar asked Jul 16 '13 11:07

DaveDev


People also ask

How do I change the background color of a table in Word?

Add or change a fill colorOn the Tables tab, under Table Styles, click the arrow next to Fill. On the Fill menu, click the color you want.

How do I change the background color of a table cell in CSS?

The background-color property specifies the background color of an element. The background color of the cells can be specified by applying this property to the TABLE, TR, TD or TH elements. The default is "transparent".


1 Answers

This is a two part question:

1) How can I modify the foreground of an OpenXML TableCell

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"));

See reference question.

2) How can I modify the background of an OpenXML TableCell

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))));

See reference question.

like image 121
Bob. Avatar answered Oct 24 '22 07:10

Bob.