【问题标题】:DocX clone table and insert at indexDocX 克隆表并在索引处插入
【发布时间】:2014-08-23 10:51:02
【问题描述】:

我正在使用 C# 制作一个简单的 Windows 应用程序,使用 Novacode 来操作 Word 文档。

我的 Word 文档中有一个要克隆的源表。我可以使用以下代码找到源表:

Table sourceTable = document.Tables[3]; 

我可以通过行和列看到这实际上是我要克隆的表。

我的 Word 文档中有一个字符串,紧随其后我想插入我的克隆源表。事实上,我可能需要插入不止一次。

我不知道如何找到我的字符串,它的索引,然后在该索引处插入一个或多个克隆表。

谢谢。

【问题讨论】:

    标签: c# docx novacode-docx


    【解决方案1】:

    这是我的做法,我使用插入并替换为表格的标签:

    // Add a Table to this document.
    var table = document.AddTable(2, 3);
    
    // Specify some properties for this Table.
    table.Alignment = Alignment.center;
    
    // Add content to this Table.
    table.Rows[0].Cells[0].Paragraphs.First().Append("A");
    table.Rows[0].Cells[1].Paragraphs.First().Append("B");
    table.Rows[0].Cells[2].Paragraphs.First().Append("C");
    table.Rows[1].Cells[0].Paragraphs.First().Append("D");
    table.Rows[1].Cells[1].Paragraphs.First().Append("E");
    table.Rows[1].Cells[2].Paragraphs.First().Append("F");
    
    // Insert table at index where tag #TABLE# is in document.
    document.InsertTable(table));
    foreach (var paragraph in document.Paragraphs)
    {
        paragraph.FindAll("#TABLE#").ForEach(index => paragraph.InsertTableAfterSelf((table)));
    }
    
    //Remove tag
    document.ReplaceText("#TABLE#", ""); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 2018-08-31
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 2011-11-14
      • 1970-01-01
      相关资源
      最近更新 更多