10

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

DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • Can you please let us know the full error message and the line that throws error. – Microsoft DN Jul 16 '13 at 11:43
  • I've included an image of the error. There isn't really much info there. Can I find that somewhere else? – DaveDev Jul 16 '13 at 11:45
  • May be your file get corrupted. Try opening the same file manually. If it opens then there might be some mistake in code itself. – Microsoft DN Jul 16 '13 at 11:48
  • I'm trying to open the file with Word. That's what shows me the message. – DaveDev Jul 16 '13 at 12:45
  • There is one solution available for MS Word: Open and Repair. This will repair your corrupt .docx file and then you can open it – Microsoft DN Jul 16 '13 at 13:01
  • Unfortunately, too much documentation is written as "This code accomplishes that effect." But users need "To get that effect, write this code." I've been looking for this info all over learn.microsoft.com, and was about to ask it on stackoverflow.com. Maybe Microsoft (and others) will get the hint. – TheBick Oct 15 '19 at 18:50

2 Answers2

23

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.

Community
  • 1
  • 1
Bob.
  • 3,894
  • 4
  • 44
  • 76
-2
DocumentFormat.OpenXml.WordProcessingRunProperties rp = 
    new DocumentFormat.OpenXml.WordProcessingRunProperties();

=

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

??

slavoo
  • 5,798
  • 64
  • 37
  • 39
Guest
  • 1