2013-07-16 125 views
5

次のように私はテーブルのセルを作成しています:OpenXML TableCellの前景色と背景色を変更するにはどうすればよいですか?

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

は、私はそれが白いテキストと青になりたいです。

私は以下を試しましたが、動作しません。私は、ドキュメントを開こうとすると、私は内容に問題があるというエラーが表示されます。

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

次のように完全なエラーがある:

enter image description here

+0

完全なエラーメッセージとエラーを通知する行をお知らせください。 –

+0

私はエラーのイメージを含めました。そこには本当に多くの情報がありません。それをどこか別の場所で見つけることはできますか? – DaveDev

+0

ファイルが壊れている可能性があります。手動で同じファイルを開いてみてください。それが開くと、コード自体に何らかの間違いがあるかもしれません。 –

答えて

11

これは、二つの部分の質問です:私はOpenXMLのテーブルセル

foregrounのフォアグラウンドを変更するにはどうすればよい

1) dは、RunPropertiesと呼ばれるRunのプロパティによって定義されます。実行に色を追加するには、Valプロパティを使用してColorオブジェクトを追加する必要があります。

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

reference questionを参照してください。私はOpenXMLののTableCell

TableCell背景の背景を変更する方法

2)がRunPropertiesを使用して上記Runと同様TableCellPropertiesを使用して変更することができます。ただし、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)))); 

reference questionを参照してください。

+0

@DaveDev Haha、うーん。あなたはアイデアを持っている! –

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

=

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

関連する問題