1

我正在尝试使用 DocX nuget 包创建/操作 Word .docx 文件。

在文档中,他们提供了以下示例:

// Place holder for a Table.
Table t;

// Load document a.
using (DocX documentA = DocX.Load(@"C:\Example\a.docx"))
{
    // Get the first Table from this document.
    t = documentA.Tables[0];
}

// Load document b.
using (DocX documentB = DocX.Load(@"C:\Example\b.docx"))
{
    /* 
     * Insert the Table that was extracted from document a, into document b. 
     * This creates a new Table that is now associated with document b.
     */
    Table newTable = documentB.InsertTable(t);

    // Save all changes made to document b.
    documentB.Save();
}// Release this document from memory.

当代码执行时,我在插入表格时收到错误: Table newTable = documentB.InsertTable(t);

错误:System.InvalidOperationException {“序列不包含元素”}

我不知道为什么会发生这种情况。我查看了正在插入的表“t”,它似乎填充了所有属性。我不清楚是什么导致了错误。

任何帮助将不胜感激。

4

1 回答 1

1

我已切换到使用从 docx.codeplex.com 下载的最新版本的 DLL。

我仍然有这个问题。然而,我随后将我从源文档处理的表与我直接使用 DocX.dll 创建的表进行了比较。

我发现由于某种原因,我的源表上的 Design 属性没有设置为任何值,但是我直接创建的表设置为 Novacode.TableDesign.TableGrid。

我现在在我的源表上设置这个属性,一切都按预期工作。

mytable.Design = Novacode.TableDesign.TableGrid;

我怀疑这是我正在修改的源文档中的一个问题。但是,由于我不是原作者,我无法控制这一点。

手动设置此属性允许我克隆源表,我的解决方案现在正在运行。

于 2015-05-04T14:29:39.573 回答