Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DocX-在指定索引处插入表_C#_Visual Studio 2012_Docx_Novacode Docx - Fatal编程技术网

C# 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

我开发了一个WinForms应用程序,它利用DocX库编辑内存中的.DocX文件

我有一个问题,我已经搜索谷歌大力,这是我想添加一个表到我的文档使用特定的索引

为此,我在文档中已经有一个空白表,然后查找索引,并使用预先存在的表中的索引将该表替换为我应用程序中的表。我最终会出现以下错误:

位置:Part:/word/document.xml,第22行,第0列

我不知道这是否是DocX的小故障,或者我处理这个问题的方法是错误的

下面的代码不是我的应用程序,而是我的应用程序使用的一些基本代码

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的答案

    我在这里回答了一个类似的问题:希望它能有所帮助。