2012-08-02 13 views
5

Ich möchte eine Textausrichtung in einer Tabellenzelle in einer Tabelle mit OpenXML anwenden.Begründung in Text aus TableCell mit OpenXML SDK 2.0

Ich verstehe nicht, warum es nicht angewendet wird.

Table table = new Table(); 
TableRow tableHeader = new TableRow(); 
table.AppendChild<TableRow>(tableHeader); 
TableCell tableCell = new TableCell(); 
tableHeader.AppendChild<TableCell>(tableCell); 
Paragraph paragraph = new Paragraph(new Run(new Text("test"))); 
ParagraphProperties paragraphProperties = new ParagraphProperties(); 
JustificationValues? justification = GetJustificationFromString("centre"); 
if (justification != null) 
{ 
    paragraphProperties.AppendChild<Justification>(new Justification() { Val = justification }); 
} 
paragraph.AppendChild<ParagraphProperties>(paragraphProperties); 
tableCell.AppendChild<Paragraph>(paragraph); 


public static JustificationValues? GetJustificationFromString(string alignment) 
{ 
    switch(alignment) 
    { 
     case "centre" : return JustificationValues.Center; 
     case "droite" : return JustificationValues.Right; 
     case "gauche" : return JustificationValues.Left; 
     default: return null; 
    } 
} 

Thx für Sie helfen!

+0

Sieht gut aus, haben Sie versucht, indem Sie den Typ aus JustificationValues ​​ändern? zu JustificationValues ​​ – Kiru

+0

Ich tat es, aber nichts ändert sich – Aelios

Antwort

14

Funktioniert es, wenn Sie die paragraphProperties auf die übergeordnete Zelle und nicht auf den Absatz anwenden?

Table table = new Table(); 
TableRow tableHeader = new TableRow(); 
table.AppendChild<TableRow>(tableHeader); 
TableCell tableCell = new TableCell(); 
tableHeader.AppendChild<TableCell>(tableCell); 
ParagraphProperties paragraphProperties = new ParagraphProperties(); 
Paragraph paragraph = new Paragraph(new Run(new Text("test"))); 
JustificationValues? justification = GetJustificationFromString("centre"); 

// Use System.Nullable<T>.HasValue instead of the null check. 
if (justification.HasValue) 
{ 
    // Using System.Nullable<T>.Value to obtain the value and resolve a warning 
    // that occurs when using 'justification' by itself. 
    paragraphProperties.AppendChild<Justification>(new Justification() { Val = justification.Value }); 
} 

// append the paragraphProperties to the tableCell rather than the paragraph element 
tableCell.AppendChild<ParagraphProperties>(paragraphProperties); 
tableCell.AppendChild<Paragraph>(paragraph); 
Console.WriteLine(table.OuterXml); 

table.OuterXml vor:

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:tr> 
    <w:tc> 
     <w:p> 
     <w:r> 
      <w:t>test</w:t> 
     </w:r> 
     <w:pPr> 
      <w:jc w:val="center" /> 
     </w:pPr> 
     </w:p> 
    </w:tc> 
    </w:tr> 
</w:tbl> 

table.OuterXml nach:

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> 
    <w:tr> 
    <w:tc> 
     <w:pPr> 
     <w:jc w:val="center" /> 
     </w:pPr> 
     <w:p> 
     <w:r> 
      <w:t>test</w:t> 
     </w:r> 
     </w:p> 
    </w:tc> 
    </w:tr> 
</w:tbl> 

bin ich ziemlich neu in OpenXml. Wird das Ergebnis in einem Word-Dokument gespeichert und in Word angezeigt?

+0

Schön, es ist jetzt in Ordnung! Danke – Aelios

+0

Beim Öffnen mit dem Productivity Tool wird dies als OpenXmlUnknownElement gerendert. Also kann nicht in pdf umgewandelt werden, etc. Irgendeine Möglichkeit, dies zu lösen? –

+0

Neue Ausgabe, bitte poste einen separaten Thread. – Nicodemeus

Verwandte Themen