简体   繁体   English

OpenXML表副本提供了OpenXmlUnknownElement

[英]OpenXML table copy gives OpenXmlUnknownElement

I'm using OpenXML SDK with Word to produce labels. 我将OpenXML SDK与Word结合使用以产生标签。 The base Word doc has one page with a table, two rows and two cells where each cell represents one label. 基本的Word文档具有一页带表格的页面,两行和两个单元格,其中每个单元格代表一个标签。 My plan was to add pages as needed based on the size of a SQL result set. 我的计划是根据SQL结果集的大小根据需要添加页面。 The append of the page works but the count of tables in the Body doesn't change. 该页面的附加部分可以使用,但是正文中的表数不变。 Is there a way to refresh the Element array? 有没有一种方法可以刷新Element数组? I suppose I can get the size of the result set and append the required number of pages then close and reopen the doc but I'm guessing I'm missing something simple. 我想我可以得到结果集的大小并添加所需的页面数,然后关闭并重新打开文档,但是我想我缺少了一些简单的东西。

Reported the wrong problem sorry, the doc looked correct in Word but not in the productivity tool. 报告了错误的问题很抱歉,该文档在Word中看起来正确,但在生产力工具中看起来不正确。 I updated the code to show my latest attempt. 我更新了代码以显示我的最新尝试。 iTest now reports the correct number of tables but in the productivity tool the added tables are flagged as OpenXmlUnknownElement . 现在,iTest报告了正确的表数,但是在生产力工具中,添加的表被标记为OpenXmlUnknownElement

for (int i = 0; i < pages; ++i)
{
    Table table = new Table(doc.MainDocumentPart.Document.Body.Elements<Table>().First().CloneNode(true));
    doc.MainDocumentPart.Document.Body.Append(table);
}
int iTest = doc.MainDocumentPart.Document.Body.Elements<Table>().Count();

the xml shows there is an extra <w:tbl> generated: xml显示生成了额外的<w:tbl>

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:tbl>
    <w:tblPr>
      <w:tblStyle w:val="TableGrid" />

So the real question is, how to copy and paste a table with OpenXML SDK? 因此,真正的问题是,如何使用OpenXML SDK复制和粘贴表?

new and CloneNode were redundant, also InsertAfter wasn't necessary but kept the tables together. new和CloneNode是多余的,也不需要InsertAfter,但是将表保持在一起。 Since I'm not updating the labels until later inserting after First() works for my purpose. 由于我要等到以后在First()之后插入才可以实现我的目的,所以我才更新标签。

for (int i = 0; i < pages; ++i)
{
    doc.MainDocumentPart.Document.Body.InsertAfter(doc.MainDocumentPart.Document.Body.Elements<Table>().First().CloneNode(true), MDP.Document.Body.Elements<Table>().First());
}

Try Descendants<Table> instead of Elements<Table> . 尝试使用Descendants<Table>而不是Elements<Table>

Also post the XML of your Body, so we can better help you. 同时发布您的Body的XML,以便我们更好地为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM