如果可能,防止表格突破页面

时间:2014-07-02 12:21:19

标签: c# ms-word openxml

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

当只有两行的表在两个页面之间拆分时,或者当一个键行是前一页上唯一的一个拆分时,看起来很糟糕。我花了很多时间来浏览MSDN页面而没有运气......希望有人之前已经这样做了。

2 个答案:

答案 0 :(得分:4)

这是一种痛苦,但有一种方法可以做到这一点。如果您直接在Word中编辑文档并希望实现此行为,则可以使用" Keep with next"和#34;保持在一起"段落下的属性>行和分页符。基本上,您应该选中该框以为表中的每一行启用此属性(技术上除了最后一行之外的所有行,尽管设置所有行也可以正常工作)。因为在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"))))))));
        }

下面的代码会生成一个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(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 1"))))),
                       new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 2"))))),
                      new TableRow(
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    new Run(
                                    new Text("Table Row 3")))),
                            new TableCell(
                                new Paragraph(
                                    new ParagraphProperties(
                                        new KeepNext(),
                                        new KeepLines()),
                                    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"))))))));
        }

答案 1 :(得分:0)

虽然这可能对你有用,但它并没有为我解决问题。我们一直在寻找导致“允许行跨页”的属性。经过一些额外的挖掘后,发现了以下内容(示例在其他答案示例代码的上下文中)。

DocumentFormat.OpenXml.Wordprocessing.TableRowProperties

DocumentFormat.OpenXml.Wordprocessing.CantSplit (表格行不能跨页)

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 TableRowProperties(
            new CantSplit()),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 1")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 1")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 1")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 1"))))),
   new TableRow(
        new TableRowProperties(
            new CantSplit()),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 2")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 2")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 2")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 2"))))),
  new TableRow(
        new TableRowProperties(
            new CantSplit()),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 3")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 3")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 3")))),
        new TableCell(
            new Paragraph(
                new Run(
                new Text("Table Row 3"))))),
 new TableRow(
        new TableRowProperties(
            new CantSplit()),
        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"))))))));

参考: http://msdn.microsoft.com/EN-US/library/office/documentformat.openxml.wordprocessing.cantsplit(v=office.15).aspx

P.S。 @mcdonams - 就像你活着的头像一样!

编辑 :抱歉,我从一开始就误读了这个问题。