1


如何在打开的 xml 中将我的表格对齐在 word 文档的中间?
这是我的代码,我想在中间对齐表格。

这是我的代码。我正在尝试使用 TableJustification,但它似乎不起作用

using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());

                Run run = new Run();
                // Goes through all of the forms
                foreach (var form in forms)
                {

                    Table table = new Table();
                    // Initialize all of the table properties
                    TableProperties tblProp = new TableProperties(
                        new TableBorders(
                            new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                            new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                            new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
                            new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 }
                        )

                    );

                    // Align the table to the center
                    TableJustification justs_center = new TableJustification() { Val = _____  };

                    table.AppendChild(justs_center);
                    table.AppendChild<TableProperties>(tblProp);

                    Paragraph para = body.AppendChild(new Paragraph());
                    Run run_header = para.AppendChild(new Run());
                    RunProperties runProps = run_header.AppendChild(new RunProperties(new Bold()));


                    string username = form.Username;
                    string proces_header = form.HeaderTitle;

                    run_header.AppendChild(new Text(proces_header + " | " + username));

                    for (int i = 0; i < form.FieldList.Count; i++)
                    {
                        if (!(form.FieldList[i].Token == "USR" || form.FieldList[i].Token == "SNT"))
                        {
                            TableRow tr = new TableRow();

                            TableCell header_cell = new TableCell();

                            header_cell.Append(new Paragraph(new Run(new Text(form.FieldList[i].Label + ": " + form.FieldList[i].Value))));

                            tr.Append(header_cell);
                            table.Append(tr);
                        }
                    }
                    wordDoc.MainDocumentPart.Document.Body.Append(table);

                }

                mainPart.Document.Save();
                wordDoc.Close();
                return "Success";
            }
        }
4

3 回答 3

4
Table t = new Table(
    new TableProperties(
        ...
        new TableJustification() { Val =  TableRowAlignmentValues.Center},
        ...),
    new TableRow(...),
);
于 2012-12-06T21:14:52.083 回答
2

试图以接受的答案居中表格似乎不起作用。我还需要将中心对齐应用于所有行

var t = new Table();
var tableProps = new TableProperties();
tableProps.TableJustification = new TableJustification { Val = TableRowAlignmentValues.Center };
t.Append(tableProps);

var row = new TableRow();
var rowProps = new TableRowProperties();
rowProps.Append(new TableJustification { Val = TableRowAlignmentValues.Center });
row.Append(rowProps);

使用 Open XML Productivity Tool 找到

于 2017-05-01T16:33:17.170 回答
0

实际上,您只需要将中心对齐应用于 TableRowProperties 中的所有行。即使您不在 TableProperties 中应用对齐方式,表格也将居中。如果有人知道,我很想知道在 TableRowProperties 中证明的目的是什么。

于 2021-03-18T08:53:00.933 回答