使用 OpenXml Sdk 2.0 在 Word 中水平文本对齐
我需要另一个帮助...我的导出功能将我的报告导出为 Word 中的表格。 我需要为每个单元格应用水平对齐属性。下面给出了我为导出编写的代码。 Tbl 是我在报告中使用的文本块。 我在这里写了对齐代码。但不起作用..请帮助我使用 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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该为段落 (
w:p
) 属性 (w:pPr
) 使用w:jc
元素来定义所需的水平对齐方式:您始终可以将 Word 文档另存为 OpenXML,将其重命名为 .zip 并解压缩以检查如何在 OpenXML 中执行某些操作。
You should to use
w:jc
element for your paragraph (w:p
) properties (w:pPr
) to define your desired horizontal alignment: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.
感谢 Rubens Farias,
我的问题在这里解决了。代码中的小改动使其工作。问题是我为运行属性提供了 Justification 属性,而不是段落属性。
我更改了代码,因为
这解决了我的问题。再次感谢鲁本斯的帮助,通过您的帮助,我的错误被识别出来。
Thanks Rubens Farias,
My problem solved here.. Small Change in Code made it work.. Problem was I gave Justification property for Run Properties Instead Paragraph Properties.
I Changed Code as
This Solved my Problem.. Once again Thanks for Rubens for your help, with which my mistake was Identified.
在这里,您的样式表中有三种不同类型的对齐方式。您可以使用此构造函数来传递它:
这是可能的,因为CellFormats继承自OpenXmlElement。在我的代码中,我创建了自己的填充、字体和边框...如果不需要,请不要注意这一点。
因此,最后您可以通过以下方式将单元格设置为标题:
Here you have three different kind of alignment inside your Stylesheet. You can use this constructor to pass it:
This is possible because CellFormats inherits from OpenXmlElement. In my code, I have created my own fill, font and border... don't pay attention to this if you don't need it.
So, finally you can set your cell as a header in this way: