簡體   English   中英

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

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

我想生成多個表,每個表在Word文檔中各有2行。 但是我想將這兩行保持在一起(如果可能)。

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

在所有情況下,第二行(如果太大)將落在第二頁上。 最后一個單元格(內容較多的單元格new CantSplit()上的new CantSplit()避免了單元格的破裂。 但是,所有選項都不能避免拆分表(按行)。

您需要向行添加一個KeepNext才能將它們保持在一起。 document.xml中的XML輸出應類似於以下內容:

在此處輸入圖片說明

此代碼成功創建了一個包含兩行的表,這些表將在頁面之間保持在一起:

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);
            }
        }
    }

獲取所有表格

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM