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.
Czy możesz dać nam znać pełny komunikat o błędzie i linię, która zgłasza błąd. –
Załączam obraz błędu. Nie ma tam zbyt wiele informacji. Czy mogę znaleźć to gdzie indziej? – DaveDev
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. –