根据标记名在 Word 中查找内容控件
我目前正在尝试使用 OpenXml 进行一些新操作,但最近遇到了一些麻烦。正如标题所示,我正在尝试使用 Xml 将数据动态插入到表中。为了识别 Worddoc 中的表格,我在其周围放置了富文本内容控件。
一切正常&看起来很好,直到我尝试在文档中搜索富文本内容控件的标记名。运行代码时,我不断收到“未设置对象实例的对象引用。”。
他就是发生这种情况的地方:
SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == t.Name).Single();
我已经使用以下 MSDN 文档来尝试实现我的目标,但没有成功:
使用 Open Xml API 将重复数据项插入到 Word 2007 表中
如果有人可以帮助我,我会长久地爱你。
I'm currenty trying some new things with OpenXml but I recently came across some trouble. As the title says I'm trying to insert data dynamically into a table using Xml. To indentify the table in my Worddoc I've put a Rich Text Content Control arround it.
Everything worked & looked fine until I tried to search my document for the tagname of the Rich Text Content Control. I keep getting a "Object reference not set to an instance of an object." while running my code.
He's the line where it happens:
SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == t.Name).Single();
I've used following MSDN document to try and achieve my goal, without succes:
Inserting Repeating Data Items into a Word 2007 Table by Using the Open Xml API
If anyone can help me, I would love you long time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经创建了一个实际上效果更好的解决方法。
首先,我从文档中检索所有表。
然后,我检查 SdtBlock(我的表的父级)的标记参数,看看它们是否匹配。
t.Name是我要查找的内容的标签。
我实际上对这个结果非常满意,因为这解决了无法填充多个相同表格(在同一文档中,具有相同标签)的问题。
小旁注:如果文档中还有其他非动态的表格,最好尝试捕获 if。
会杀死那些人。
Okay, I've created a workarround which actually works even better.
First I retrieve all the tables from the document..
Then I check the tag-parameter of SdtBlock which is the parent of my Table, to see if they match.
t.Name is the tag of the content I wanted to find.
I'm actually pretty pleased about this result because this solves the fact that several same tables (in the same document, with the same tag) could not be filled.
Small sidenote: It might be best to try catch the if, if you have other tables in your document which aren't dynamic.
would kill those.