c# – 带有OpenXML SDK 2.0的tableCell文本中的对齐

前端之家收集整理的这篇文章主要介绍了c# – 带有OpenXML SDK 2.0的tableCell文本中的对齐前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在表格单元格中使用Open XML应用文本对齐方式.

我不明白为什么它没有应用.

  1. Table table = new Table();
  2. TableRow tableHeader = new TableRow();
  3. table.AppendChild<TableRow>(tableHeader);
  4. TableCell tableCell = new TableCell();
  5. tableHeader.AppendChild<TableCell>(tableCell);
  6. Paragraph paragraph = new Paragraph(new Run(new Text("test")));
  7. ParagraPHProperties paragraPHProperties = new ParagraPHProperties();
  8. JustificationValues? justification = GetJustificationFromString("centre");
  9. if (justification != null)
  10. {
  11. paragraPHProperties.AppendChild<Justification>(new Justification() { Val = justification });
  12. }
  13. paragraph.AppendChild<ParagraPHProperties>(paragraPHProperties);
  14. tableCell.AppendChild<Paragraph>(paragraph);
  15.  
  16.  
  17. public static JustificationValues? GetJustificationFromString(string alignment)
  18. {
  19. switch(alignment)
  20. {
  21. case "centre" : return JustificationValues.Center;
  22. case "droite" : return JustificationValues.Right;
  23. case "gauche" : return JustificationValues.Left;
  24. default: return null;
  25. }
  26. }

谢谢你的帮助!

解决方法

如果您将paragraPHProperties应用于父单元而不是段落,它是否有效?
  1. Table table = new Table();
  2. TableRow tableHeader = new TableRow();
  3. table.AppendChild<TableRow>(tableHeader);
  4. TableCell tableCell = new TableCell();
  5. tableHeader.AppendChild<TableCell>(tableCell);
  6. ParagraPHProperties paragraPHProperties = new ParagraPHProperties();
  7. Paragraph paragraph = new Paragraph(new Run(new Text("test")));
  8. JustificationValues? justification = GetJustificationFromString("centre");
  9.  
  10. // Use System.Nullable<T>.HasValue instead of the null check.
  11. if (justification.HasValue)
  12. {
  13. // Using System.Nullable<T>.Value to obtain the value and resolve a warning
  14. // that occurs when using 'justification' by itself.
  15. paragraPHProperties.AppendChild<Justification>(new Justification() { Val = justification.Value });
  16. }
  17.  
  18. // append the paragraPHProperties to the tableCell rather than the paragraph element
  19. tableCell.AppendChild<ParagraPHProperties>(paragraPHProperties);
  20. tableCell.AppendChild<Paragraph>(paragraph);
  21. Console.WriteLine(table.OuterXml);

table.OuterXml之前:

  1. <w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  2. <w:tr>
  3. <w:tc>
  4. <w:p>
  5. <w:r>
  6. <w:t>test</w:t>
  7. </w:r>
  8. <w:pPr>
  9. <w:jc w:val="center" />
  10. </w:pPr>
  11. </w:p>
  12. </w:tc>
  13. </w:tr>
  14. </w:tbl>

table.OuterXml之后:

  1. <w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  2. <w:tr>
  3. <w:tc>
  4. <w:pPr>
  5. <w:jc w:val="center" />
  6. </w:pPr>
  7. <w:p>
  8. <w:r>
  9. <w:t>test</w:t>
  10. </w:r>
  11. </w:p>
  12. </w:tc>
  13. </w:tr>
  14. </w:tbl>

我是OpenXml的新手.结果是保存到word文档中还是用单词查看?

猜你在找的C#相关文章