如果可能,防止表格越过一页

spj*_*323 2 c# ms-word openxml

我正在使用 OpenXML 生成一个包含数千个表的 word 文档。其中一些跨越整个页面的长度,这很好,因为无法防止这种情况发生,但是,许多表只包含几行。有没有我可以设置的属性来防止这些表被破坏?

当只有两行的表格被拆分到两页时,或者当关键行是上一页上唯一拆分的行时,这看起来很糟糕。我花了大量时间浏览 MSDN 页面,但没有运气……希望之前有人这样做过。

mcd*_*ams 5

这有点痛苦,但有一种方法可以做到。如果您直接在 Word 中编辑文档并希望实现此行为,则可以使用“段落”>“行和分页符”下的“保持下一个”和“将行保持在一起”属性。基本上,您将选中该框为表中的每一行启用此属性(技术上除了最后一行之外的所有行,尽管设置所有行也可能有效)。由于在 Word 中可以完成的任何事情也可以使用 OOXML API 来完成,因此只需弄清楚它。这是一个代码片段,说明了所需的代码。

首先,下面的代码生成一个带有表格的 Word 文档,该表格跨页。它首先为表格添加一个简单的样式,然后添加一系列段落将表格向下推到页面,最后在底部添加表格。

using (WordprocessingDocument document = WordprocessingDocument.Create("TableBreaksAcrossPage.docx", WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();

            #region Styles

            Styles styles = new Styles();

            Style style = new Style() { Type = StyleValues.Table, StyleId = "TableGrid" };
            StyleName styleName10 = new StyleName() { Val = "Table Grid" };
            BasedOn basedOn2 = new BasedOn() { Val = "TableNormal" };
            UIPriority uIPriority8 = new UIPriority() { Val = 59 };
            Rsid rsid7 = new Rsid() { Val = "003B7411" };

            StyleParagraphProperties styleParagraphProperties2 = new StyleParagraphProperties();
            SpacingBetweenLines spacingBetweenLines4 = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

            styleParagraphProperties2.Append(spacingBetweenLines4);

            StyleTableProperties styleTableProperties4 = new StyleTableProperties();
            TableIndentation tableIndentation4 = new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa };

            TableBorders tableBorders2 = new TableBorders();
            TopBorder topBorder2 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            LeftBorder leftBorder2 = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            BottomBorder bottomBorder2 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            RightBorder rightBorder2 = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideHorizontalBorder insideHorizontalBorder2 = new InsideHorizontalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideVerticalBorder insideVerticalBorder2 = new InsideVerticalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };

            tableBorders2.Append(topBorder2);
            tableBorders2.Append(leftBorder2);
            tableBorders2.Append(bottomBorder2);
            tableBorders2.Append(rightBorder2);
            tableBorders2.Append(insideHorizontalBorder2);
            tableBorders2.Append(insideVerticalBorder2);

            TableCellMarginDefault tableCellMarginDefault4 = new TableCellMarginDefault();
            TopMargin topMargin4 = new TopMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellLeftMargin tableCellLeftMargin4 = new TableCellLeftMargin() { Width = 108, Type = TableWidthValues.Dxa };
            BottomMargin bottomMargin4 = new BottomMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellRightMargin tableCellRightMargin4 = new TableCellRightMargin() { Width = 108, Type = TableWidthValues.Dxa };

            tableCellMarginDefault4.Append(topMargin4);
            tableCellMarginDefault4.Append(tableCellLeftMargin4);
            tableCellMarginDefault4.Append(bottomMargin4);
            tableCellMarginDefault4.Append(tableCellRightMargin4);

            styleTableProperties4.Append(tableIndentation4);
            styleTableProperties4.Append(tableBorders2);
            styleTableProperties4.Append(tableCellMarginDefault4);

            style.Append(styleName10);
            style.Append(basedOn2);
            style.Append(uIPriority8);
            style.Append(rsid7);
            style.Append(styleParagraphProperties2);
            style.Append(styleTableProperties4);

            styles.Append(style);

            StyleDefinitionsPart styleDefinitionsPart = mainDocumentPart.AddNewPart<StyleDefinitionsPart>("Styles");
            styleDefinitionsPart.Styles = styles;

            #endregion

            mainDocumentPart.Document =
            new Document(
                new Body(
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Table(
                        new TableProperties(
                            new TableStyle() { Val = "TableGrid" },
                            new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }),
                        new TableGrid(
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" }),
                        new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 1"))))),
                       new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 2"))))),
                      new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(

                                        ),
                                    new Run(
                                    new Text("Table Row 3"))))),
                     new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4")))),
                            new TableCell(
                                new Paragraph(
                                    new Run(
                                    new Text("Table Row 4"))))))));
        }
Run Code Online (Sandbox Code Playgroud)

下面的代码生成一个 Word 文档,其中包含一个不会跨页中断的表格。请注意,每个段落的 ParagraphProperties 都附加了 KeepNext() 和 KeepLines() 类的实例。据我所知,这需要为每个 TableCell 完成,这是痛苦的部分。不过,可能能够将单元格创建卸载到辅助方法,以避免所有重复的代码。运行代码并亲自查看,希望它有所帮助。

using (WordprocessingDocument document = WordprocessingDocument.Create("TableDoesNotBreakAcrossPage.docx", WordprocessingDocumentType.Document))
        {
            MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();

            #region Styles

            Styles styles = new Styles();

            Style style = new Style() { Type = StyleValues.Table, StyleId = "TableGrid" };
            StyleName styleName10 = new StyleName() { Val = "Table Grid" };
            BasedOn basedOn2 = new BasedOn() { Val = "TableNormal" };
            UIPriority uIPriority8 = new UIPriority() { Val = 59 };
            Rsid rsid7 = new Rsid() { Val = "003B7411" };

            StyleParagraphProperties styleParagraphProperties2 = new StyleParagraphProperties();
            SpacingBetweenLines spacingBetweenLines4 = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto };

            styleParagraphProperties2.Append(spacingBetweenLines4);

            StyleTableProperties styleTableProperties4 = new StyleTableProperties();
            TableIndentation tableIndentation4 = new TableIndentation() { Width = 0, Type = TableWidthUnitValues.Dxa };

            TableBorders tableBorders2 = new TableBorders();
            TopBorder topBorder2 = new TopBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            LeftBorder leftBorder2 = new LeftBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            BottomBorder bottomBorder2 = new BottomBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            RightBorder rightBorder2 = new RightBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideHorizontalBorder insideHorizontalBorder2 = new InsideHorizontalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };
            InsideVerticalBorder insideVerticalBorder2 = new InsideVerticalBorder() { Val = BorderValues.Single, Color = "auto", Size = (UInt32Value)4U, Space = (UInt32Value)0U };

            tableBorders2.Append(topBorder2);
            tableBorders2.Append(leftBorder2);
            tableBorders2.Append(bottomBorder2);
            tableBorders2.Append(rightBorder2);
            tableBorders2.Append(insideHorizontalBorder2);
            tableBorders2.Append(insideVerticalBorder2);

            TableCellMarginDefault tableCellMarginDefault4 = new TableCellMarginDefault();
            TopMargin topMargin4 = new TopMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellLeftMargin tableCellLeftMargin4 = new TableCellLeftMargin() { Width = 108, Type = TableWidthValues.Dxa };
            BottomMargin bottomMargin4 = new BottomMargin() { Width = "200", Type = TableWidthUnitValues.Dxa };
            TableCellRightMargin tableCellRightMargin4 = new TableCellRightMargin() { Width = 108, Type = TableWidthValues.Dxa };

            tableCellMarginDefault4.Append(topMargin4);
            tableCellMarginDefault4.Append(tableCellLeftMargin4);
            tableCellMarginDefault4.Append(bottomMargin4);
            tableCellMarginDefault4.Append(tableCellRightMargin4);

            styleTableProperties4.Append(tableIndentation4);
            styleTableProperties4.Append(tableBorders2);
            styleTableProperties4.Append(tableCellMarginDefault4);

            style.Append(styleName10);
            style.Append(basedOn2);
            style.Append(uIPriority8);
            style.Append(rsid7);
            style.Append(styleParagraphProperties2);
            style.Append(styleTableProperties4);

            styles.Append(style);

            StyleDefinitionsPart styleDefinitionsPart = mainDocumentPart.AddNewPart<StyleDefinitionsPart>("Styles");
            styleDefinitionsPart.Styles = styles;

            #endregion

            mainDocumentPart.Document =
            new Document(
                new Body(
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Paragraph(
                        new Run(
                            new Text("Test"))),
                    new Table(
                        new TableProperties(
                            new TableStyle() { Val = "TableGrid" },
                            new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto }),
                        new TableGrid(
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" },
                            new GridColumn() { Width = "2000" }),
                        new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(