2012-08-02 16 views
5

Tablo hücresine OpenXML ile bir metin hizalaması uygulamak istiyorum.OpenXML SDK 2.0 ile tablonun metninde gerekçe

Neden uygulanmadığını anlamıyorum.

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

Yardımınız için teşekkürler!

+0

İyi görünüyor, JustificationValues ​​türünü değiştirerek denediniz mi? JustificationValues ​​için – Kiru

+0

Yaptım ama hiçbir şey değişmiyor – Aelios

cevap

14

ParagrafProperties'i paragraf yerine ana hücreye uygularsanız işe yarıyor mu?

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 önce:

<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 sonra:

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

Ben openxml için oldukça yeni. Sonuç bir kelime belgesine kaydedildi ve kelime ile görüntülendi mi?

+0

Güzel, şimdi tamam! Teşekkürler – Aelios

+0

Productivity Tool ile açıldığında, OpenXmlUnknownElement olarak oluşturulur. Böylece pdf'ye dönüştürülemez, vb. Bunu çözmek için herhangi bir yol var mı? –

+0

Yeni sayı, lütfen ayrı bir iş parçacığı gönderin. – Nicodemeus