0

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

我有一个问题,我已经在 Google 上大力搜索,这是我想使用特定索引向我的文档中添加一个表格。

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

位置:部分:/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();

    }
}

}

4

1 回答 1

1

根据可以从 codeplex ( http://docx.codeplex.com/releases/view/31554 ) 下载的“Advance - Invoice Example”示例项目:

  1. 查找占位符表
  2. 在占位符表格之后插入新表格
  3. 删除占位符表

简化代码:

Table placeholderTable = document.Tables[0];

Table realTable = placeholderTable.InsertTableAfterSelf(4, 4);

placeholderTable.Remove();

如果需要用表格替换文本,请参阅 Vilhelm 的回答DocX clone table and insert at index

于 2016-07-14T04:36:57.437 回答