2013-07-16 20 views
5

Tworzę komórkę tabeli w następujący sposób:Jak mogę zmienić kolor pierwszego planu i tła komórki tabeli OpenXML?

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

chcę być niebieski z białym tekstem.

Próbowałem następujące, ale to nie działa; podczas próby otwarcia dokumentu pojawia się błąd, że nie ma problemu z treścią:

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

Pełne błędu jest następująca:

enter image description here

+0

Czy możesz dać nam znać pełny komunikat o błędzie i linię, która zgłasza błąd. –

+0

Załączam obraz błędu. Nie ma tam zbyt wiele informacji. Czy mogę znaleźć to gdzie indziej? – DaveDev

+0

Może być uszkodzony plik. Spróbuj otworzyć ten sam plik ręcznie. Jeśli się otworzy, może to oznaczać jakiś błąd w samym kodzie. –

Odpowiedz

11

To jest dwuskładnikową pytanie:

1) w jaki sposób można zmodyfikować przedpolach OpenXML TableCell

foregroun d of OpenXML TableCell jest zdefiniowany przez właściwości Run, o nazwie RunProperties. Aby dodać kolor do biegu, musisz dodać obiekt Color za pomocą właściwości 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")); 

Zobacz reference question.

2) Jak można zmodyfikować tâa OpenXML TableCell

The TableCell tle mogą być modyfikowane za pomocą TableCellProperties, podobny do wyżej Run, który wykorzystuje RunProperties. Jednak aplikujesz obiekt Shading do swojego 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)))); 

Zobacz reference question.

+0

@DaveDev Haha, no cóż, whoops. Masz pomysł! –

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

=

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

??

Powiązane problemy