简体   繁体   English

与OpenXML SDK 2.5一起保存表(行)

[英]keep table(rows) together with OpenXML SDK 2.5

I'd like to generate multiple tables with 2 rows each inside of a word document. 我想生成多个表,每个表在Word文档中各有2行。 But I want to keep these two rows together (if possible). 但是我想将这两行保持在一起(如果可能)。

  1. new KeepNext() on the first row doesn't work 第一行上的new KeepNext()不起作用
  2. new KeepNext() on the last paragraph of the first row doesn't work 第一行最后一段的new KeepNext()不起作用
  3. new CantSplit() on the table doesn't work new CantSplit()不起作用

In all cases the second row (if too large) lands on the second page. 在所有情况下,第二行(如果太大)将落在第二页上。 A new CantSplit() on the last cell ( the one with large content) avoids a break of the cell. 最后一个单元格(内容较多的单元格new CantSplit()上的new CantSplit()避免了单元格的破裂。 But none of the options avoids a split of the table (rowwise). 但是,所有选项都不能避免拆分表(按行)。

You need to add a KeepNext to each row to keep them together. 您需要向行添加一个KeepNext才能将它们保持在一起。 The XML output in document.xml should be something akin to: document.xml中的XML输出应类似于以下内容:

在此处输入图片说明

This code successfully creates a table with 2 rows that will stay together across pages: 此代码成功创建了一个包含两行的表,这些表将在页面之间保持在一起:

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"));
i did it like this when I had to apply this to all tables:

private static void AlterTableType(List<Table> t)
    {
        foreach (Table table in t)
        {
            foreach (TableRow row in table.Descendants<TableRow>())
            {
                TableRowProperties trP = new TableRowProperties();
                CantSplit split = new CantSplit();
                trP.Append(split);
                row.AppendChild(trP);
            }
        }
    }

getting all tables 获取所有表格

var t = package.MainDocumentPart.Document.Body.Descendants<Table>().ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM