使用的OpenXML SDK 2.0在Word水平文本对齐 [英] Horizontal Text alignment in Word using OpenXml Sdk 2.0

查看:777
本文介绍了使用的OpenXML SDK 2.0在Word水平文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要别人来帮助...我的导出功能导出我的报告中的字表。
我需要应用水平对齐属性每个单元格。我写了出口守则如下。 TBL是我用我的报告,其中一个文本块。
我写到这里对齐的代码。但不工作。请帮我使用的OpenXML SDK来完成这个任务2.0



 使用Word = DocumentFormat.OpenXml.Wordprocessing ; 

WordprocessingDocument WordDoc = WordprocessingDocument.Create(SavePath,WordprocessingDocumentType.Document);
MainDocumentPart mainDocument = WordDoc.AddMainDocumentPart();
mainDocument.Document =新Word.Document();
StyleDefinitionsPart StylesDefs = mainDocument.AddNewPart< StyleDefinitionsPart>();
StylesDefs.Styles =新Word.Styles();
Word.Body体=新Word.Body();
Word.Table WordTable =新Word.Table();
Word.TableRow行;

Word.TableCell细胞=新Word.TableCell();
Word.Style ParaStyle =新Word.Style(新Word.Name(){瓦尔= Tbl.GetHashCode()的ToString()});
Word.RunProperties ParaRunProperties =新Word.RunProperties();
ParaRunProperties.Append(新Word.RunFonts(){Ascii码= Tbl.FontFamily.ToString()});
如果(Tbl.Horizo​​ntalAlignment == Horizo​​ntalAlignment.Center)
ParaRunProperties.Append(新Word.Justification(){瓦尔= Word.JustificationValues.Center});
,否则如果(Tbl.Horizo​​ntalAlignment == Horizo​​ntalAlignment.Right)
ParaRunProperties.Append(新Word.Justification(){瓦尔= Word.JustificationValues.Right});
,否则
ParaRunProperties.Append(新Word.Justification(){瓦尔= Word.JustificationValues.Left});
ParaStyle.Append(ParaRunProperties);
StylesDefs.Styles.Append(ParaStyle);
Word.ParagraphProperties ParaProperties =新Word.ParagraphProperties(){ParagraphStyleId =新Word.ParagraphStyleId(){瓦尔= Tbl.GetHashCode()的ToString()}};
Cell.Append(新Word.Paragraph(ParaProperties,新Word.Run(新Word.Text(Tbl.Text))));

Row.Append(小区);
WordTable.Append(行);
body.Append(WordTable);
mainDocument.Document.Append(体);
mainDocument.Document.Save();
WordDoc.Close();


解决方案

您应该使用W¯¯ :您的段落JC 元素(W :p )的属性( W:PPR )定义你想要的水平对齐方式:

 < W:TR> 
< W:TC>< - 你的表格单元格 - >!
< W:P>
< W:PPR>
< W:JC W:!VAL =右/>< - 水平对齐=右 - >
< / W:PPR>
< W:R>
< W:T>富巴≤; / W:T>
< / W:R>
< / W:P>
< / W:TC>
< / W:TR>

您可以随时保存一个Word文档的OpenXML,将其重命名为.zip并解压缩检查如何做OpenXML的东西。


I need another help... My export function exports my report as a table in word. I need to apply horizontal alignment property for each cell. The Code I wrote for exporting is given below. Tbl is a textblock which I am using in my report. I wrote Alignment code here. But doesn't works.. Please help me to accomplish this task using OpenXML SDk 2.0

 using Word = DocumentFormat.OpenXml.Wordprocessing;

 WordprocessingDocument WordDoc = WordprocessingDocument.Create(SavePath,  WordprocessingDocumentType.Document);
 MainDocumentPart mainDocument = WordDoc.AddMainDocumentPart();
 mainDocument.Document = new Word.Document();
 StyleDefinitionsPart StylesDefs = mainDocument.AddNewPart<StyleDefinitionsPart>();
 StylesDefs.Styles = new Word.Styles();
 Word.Body body = new Word.Body();
 Word.Table WordTable = new Word.Table();
 Word.TableRow Row;

 Word.TableCell Cell = new Word.TableCell();
 Word.Style ParaStyle = new Word.Style(new Word.Name() { Val = Tbl.GetHashCode().ToString() });
 Word.RunProperties ParaRunProperties = new Word.RunProperties();
 ParaRunProperties.Append(new Word.RunFonts() { Ascii = Tbl.FontFamily.ToString() });
 if (Tbl.HorizontalAlignment == HorizontalAlignment.Center)
     ParaRunProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Center });
 else if (Tbl.HorizontalAlignment == HorizontalAlignment.Right)
      ParaRunProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Right });
 else
      ParaRunProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Left });
 ParaStyle.Append(ParaRunProperties);
 StylesDefs.Styles.Append(ParaStyle);
 Word.ParagraphProperties ParaProperties = new Word.ParagraphProperties() { ParagraphStyleId = new Word.ParagraphStyleId() { Val = Tbl.GetHashCode().ToString() } };
 Cell.Append(new Word.Paragraph(ParaProperties, new Word.Run(new Word.Text(Tbl.Text))));

  Row.Append(Cell);
  WordTable.Append(Row);
  body.Append(WordTable);
  mainDocument.Document.Append(body);
  mainDocument.Document.Save();
  WordDoc.Close();

解决方案

You should to use w:jc element for your paragraph (w:p) properties (w:pPr) to define your desired horizontal alignment:

<w:tr>
  <w:tc><!-- your table cell -->
    <w:p>
      <w:pPr>
        <w:jc w:val="right"/><!-- horizontal alignment = right -->
      </w:pPr>
      <w:r>
        <w:t>Foo bar</w:t>
      </w:r>
    </w:p>
  </w:tc>
</w:tr>

You always can to save a Word document as OpenXML, rename it to .zip and unpack it to inspect how to do something in OpenXML.

这篇关于使用的OpenXML SDK 2.0在Word水平文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆