使用 OpenXml Sdk 2.0 在 Word 中水平文本对齐

发布于 2024-08-12 12:17:46 字数 1884 浏览 8 评论 0原文

我需要另一个帮助...我的导出功能将我的报告导出为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜灵血窟げ 2024-08-19 12:17:46

您应该为段落 (w:p) 属性 (w:pPr) 使用 w:jc 元素来定义所需的水平对齐方式:

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

您始终可以将 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:

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

嘦怹 2024-08-19 12:17:46

感谢 Rubens Farias,

我的问题在这里解决了。代码中的小改动使其工作。问题是我为运行属性提供了 Justification 属性,而不是段落属性。

我更改了代码,因为

Word.ParagraphProperties ParaProperties = new Word.ParagraphProperties() { ParagraphStyleId = new Word.ParagraphStyleId() { Val = Tbl.GetHashCode().ToString() } };
if (Tbl.HorizontalAlignment == HorizontalAlignment.Center)
     ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Center });
 else if (Tbl.HorizontalAlignment == HorizontalAlignment.Right)
      ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Right });
 else
      ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Left });

这解决了我的问题。再次感谢鲁本斯的帮助,通过您的帮助,我的错误被识别出来。

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

Word.ParagraphProperties ParaProperties = new Word.ParagraphProperties() { ParagraphStyleId = new Word.ParagraphStyleId() { Val = Tbl.GetHashCode().ToString() } };
if (Tbl.HorizontalAlignment == HorizontalAlignment.Center)
     ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Center });
 else if (Tbl.HorizontalAlignment == HorizontalAlignment.Right)
      ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Right });
 else
      ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Left });

This Solved my Problem.. Once again Thanks for Rubens for your help, with which my mistake was Identified.

猛虎独行 2024-08-19 12:17:46

在这里,您的样式表中有三种不同类型的对齐方式。您可以使用此构造函数来传递它:

public Stylesheet(params OpenXmlElement[] childElements);

这是可能的,因为CellFormats继承自OpenXmlElement。在我的代码中,我创建了自己的填充、字体和边框...如果不需要,请不要注意这一点。

new CellFormats(
                       //VALUE
                       // Index 0 - The default cell style - Alignment left
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 0, FillId = 0, BorderId = 0, ApplyBorder = true },

                       //HEADER
                       // Index 1 - Bold - Green background - align center
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true, ApplyBorder = true, ApplyFill = true },

                       //ERROR HEADER
                       //index 2 - bold text - align center
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 1, FillId = 0, BorderId = 0, ApplyFont = true, ApplyBorder = true, ApplyFill = true }
               )

因此,最后您可以通过以下方式将单元格设置为标题:

private enum CellStyleEnum
{
    Value = 0,
    Header = 1,
    Error = 2    
}

var cell = new Cell
{
    DataType = CellValues.InlineString,
    CellReference = header + index,
    StyleIndex = (UInt32)CellStyleEnum.Header
};

Here you have three different kind of alignment inside your Stylesheet. You can use this constructor to pass it:

public Stylesheet(params OpenXmlElement[] childElements);

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.

new CellFormats(
                       //VALUE
                       // Index 0 - The default cell style - Alignment left
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 0, FillId = 0, BorderId = 0, ApplyBorder = true },

                       //HEADER
                       // Index 1 - Bold - Green background - align center
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true, ApplyBorder = true, ApplyFill = true },

                       //ERROR HEADER
                       //index 2 - bold text - align center
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 1, FillId = 0, BorderId = 0, ApplyFont = true, ApplyBorder = true, ApplyFill = true }
               )

So, finally you can set your cell as a header in this way:

private enum CellStyleEnum
{
    Value = 0,
    Header = 1,
    Error = 2    
}

var cell = new Cell
{
    DataType = CellValues.InlineString,
    CellReference = header + index,
    StyleIndex = (UInt32)CellStyleEnum.Header
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文