5

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

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

Table sourceTable = document.Tables[3]; 

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

我的 Word 文档中有一个字符串,在它之后我想插入我的克隆源表。事实上,我可能需要多次插入它。

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

谢谢。

4

1 回答 1

11

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

// 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#", ""); 
于 2014-09-18T16:06:06.557 回答