DocX克隆表并在索引处插入 [英] DocX clone table and insert at index
问题描述
我正在使用C#制作一个简单的Windows应用程序,并使用Novacode来操纵Word文档.
I am using C# to make a simple Windows app using Novacode to manipulate a Word document.
我的Word文档中有一个要克隆的源表.我可以使用以下代码找到源表:
I have a source table in my Word document that I want to clone. I am able to find the source table okay using this code:
Table sourceTable = document.Tables[3];
我可以通过行和列看到实际上这是我要克隆的表.
I can see by the rows and columns that this is in fact the table that I want to clone.
我的Word文档中有一个字符串,该字符串刚好要插入克隆的源表.实际上,我可能需要插入多次.
I have a string in my Word doc that right after it I want to insert my cloned source table. In fact, I may need to insert it more than once.
我不知道如何找到我的字符串及其索引,然后在该索引处插入一个或多个克隆表.
I don't know how to find my string, the index of it, and then insert the one or more cloned tables at that index.
谢谢.
推荐答案
这是我的工作方式,我使用插入并替换为表格的标记:
Here is how I do it, I use a tag that I insert and replace with table:
// 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#", "");
这篇关于DocX克隆表并在索引处插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!