gpt4 book ai didi

c# - 将表(行)与 OpenXML SDK 2.5 一起保存

转载 作者:行者123 更新时间:2023-11-30 20:48:10 25 4
gpt4 key购买 nike

我想在 word 文档中生成多个表格,每个表格有 2 行。但我想将这两行放在一起(如果可能的话)。

  1. new KeepNext()在第一行不起作用
  2. new KeepNext()在第一行的最后一段不起作用
  3. new CantSplit() 在 table 上不起作用

在所有情况下,第二行(如果太大)位于第二页。最后一个单元格(内容较大的单元格)上的 new CantSplit() 避免了单元格的中断。但是没有一个选项可以避免表格的拆分(按行)。

最佳答案

您需要为每一行 添加一个 KeepNext 以将它们保持在一起。 document.xml 中的 XML 输出应该类似于:

enter image description here

这段代码成功地创建了一个包含 2 行的表格,这些行将跨页面保持在一起:

Table table = wordDoc.MainDocumentPart.Document.Body.AppendChild(new Table());

TableRow row1 = table.AppendChild(new TableRow());
TableCell cell1 = row1.AppendChild(new TableCell());
Paragraph para1 = cell1.AppendChild(new Paragraph());
PreviousParagraphProperties prop1 = para1.AppendChild(new PreviousParagraphProperties());
KeepNext k = prop1.AppendChild(new KeepNext());
Run run1 = para1.AppendChild(new Run());
run1.AppendChild(new Text("This is some long text"));

TableRow row2 = table.AppendChild(new TableRow());
TableCell cell2 = row2.AppendChild(new TableCell());
Paragraph para2 = cell2.AppendChild(new Paragraph());
PreviousParagraphProperties prop2 = para1.AppendChild(new PreviousParagraphProperties());
KeepNext k2 = prop2.AppendChild(new KeepNext());
Run run2 = para2.AppendChild(new Run());
run2.AppendChild(new Text("This is some even longer text"));

关于c# - 将表(行)与 OpenXML SDK 2.5 一起保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24995999/

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