gpt4 book ai didi

c# - 如果可能,防止表格跨页

转载 作者:太空宇宙 更新时间:2023-11-03 21:30:31 26 4
gpt4 key购买 nike

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

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

最佳答案

这有点痛苦,但有一种方法可以做到。如果您直接在 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"))))))));
}

下面的代码生成一个 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"))))))));
}

关于c# - 如果可能,防止表格跨页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24530954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com