OpenXML ASP.表格单元格垂直对齐问题

本文关键字:对齐 问题 垂直 单元格 ASP 表格 OpenXML | 更新日期: 2023-09-27 17:50:19

我正在尝试对齐我的*.docx-documents的表格单元格中的文本。一切都很好,直到我将tablecellproperty附加到tablecell本身。

TableCell tc = new TableCell();
TableCellProperties tcpVA = new TableCellProperties();
TableCellVerticalAlignment tcVA= new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };
tcpVA.Append(tcVA);
tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Pct, Width = columnwidths[i] }), tcpVA);

附加cellwidth,颜色等工作很好,但它只是TableCellVerticalAlignment不会工作。

TableCellProperty的值设置为:

Val = "center"

但是在将TableCellProperties添加到tablecell之后,verticalalignment的属性:

TableCellVerticalAlignment = null

OpenXML ASP.表格单元格垂直对齐问题

将两个TableCellProperties添加到TableCell中,一个用于垂直对齐,另一个用于单元格宽度。模式只允许一个TableCellProperties

TableCellVerticalAlignmentTableCellWidth都应该被附加到相同的 TableCellProperties,然后只有TableCellProperties应该被添加到单元格。

下面的方法是一个示例,它将创建一个文档,其中包含一个表格,该表格包含一个单元格,其中设置了宽度和对齐属性,并包含文本"Hello World!"

public static void CreateWordDoc(string filename)
{
    using (var wordDocument = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document))
    {
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
        // Create the document structure
        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());
        //add a table, row and column
        Table table = body.AppendChild(new Table());
        TableRow row = table.AppendChild(new TableRow());
        TableCell tc = row.AppendChild(new TableCell());
        //create the cell properties
        TableCellProperties tcp = new TableCellProperties();
        //create the vertial alignment properties
        TableCellVerticalAlignment tcVA = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };
        //create the cell width
        TableCellWidth tcW = new TableCellWidth() { Type = TableWidthUnitValues.Pct, Width = "100" };
        //append the vertical alignment and cell width objects to the TableCellProperties
        tcp.Append(tcW);
        tcp.Append(tcVA);
        //append ONE TableCellProperties object to the cell
        tc.Append(tcp);
        //add some text to the cell to test.
        Paragraph para = tc.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text("Hello World!"));
        mainPart.Document.Save();
    }
}