C# DocX-在指定索引处插入表
我开发了一个WinForms应用程序,它利用DocX库编辑内存中的.DocX文件 我有一个问题,我已经搜索谷歌大力,这是我想添加一个表到我的文档使用特定的索引 为此,我在文档中已经有一个空白表,然后查找索引,并使用预先存在的表中的索引将该表替换为我应用程序中的表。我最终会出现以下错误: 位置:Part:/word/document.xml,第22行,第0列 我不知道这是否是DocX的小故障,或者我处理这个问题的方法是错误的 下面的代码不是我的应用程序,而是我的应用程序使用的一些基本代码C# DocX-在指定索引处插入表,c#,visual-studio-2012,docx,novacode-docx,C#,Visual Studio 2012,Docx,Novacode Docx,我开发了一个WinForms应用程序,它利用DocX库编辑内存中的.DocX文件 我有一个问题,我已经搜索谷歌大力,这是我想添加一个表到我的文档使用特定的索引 为此,我在文档中已经有一个空白表,然后查找索引,并使用预先存在的表中的索引将该表替换为我应用程序中的表。我最终会出现以下错误: 位置:Part:/word/document.xml,第22行,第0列 我不知道这是否是DocX的小故障,或者我处理这个问题的方法是错误的 下面的代码不是我的应用程序,而是我的应用程序使用的一些基本代码 usin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novacode;
namespace DocXTesting
{
class Program
{
static void Main(string[] args)
{
using (DocX document = DocX.Load(@"Test.docx"))
{
Table t = document.Tables[0];
int tIndex = t.Index;
try
{
t.Remove();
Table newTable = document.InsertTable(tIndex, 4, 4);
document.Save();
}
catch (Exception x)
{
Console.WriteLine(x.Message);
}
}
Console.ReadKey();
}
}
}根据可从codeplex()下载的“预付款-发票示例”示例项目:
Table placeholderTable = document.Tables[0];
Table realTable = placeholderTable.InsertTableAfterSelf(4, 4);
placeholderTable.Remove();
如果需要将文本替换为表格,请参见Vilhelm的答案我在这里回答了一个类似的问题:希望它能有所帮助。