2012-08-02 58 views
5

我想在使用OpenXML的表格的表格單元格中應用文本對齊。使用OpenXML SDK 2.0從tableCell獲取文本的理由

我不明白爲什麼它不適用。

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 for you help!

+0

看起來很好,您是否嘗試過從JustificationValues更改類型?到理由價值 – Kiru 2012-08-08 00:33:22

+0

我做到了,但沒有任何變化 – Aelios 2012-08-27 07:19:40

回答

14

如果您要將paragraphProperties應用於父單元格而不是段落,它會起作用嗎?

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

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

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

我是相當新的OpenXML的。結果是否保存到單詞文檔並用單詞查看?

+0

不錯,現在沒事!謝謝 – Aelios 2012-08-30 07:07:50

+0

當使用Productivity Tool打開時,它會呈現爲OpenXmlUnknownElement。因此不能轉換爲PDF等任何方式來解決這個問題? – 2015-06-15 09:38:41

+0

新問題,請發佈一個單獨的線程。 – Nicodemeus 2015-06-15 19:35:47