在 Open XML 2.0 中打开 microsoft .xml 文件并添加变量数据
我们继承了 Visual Studio 2005 中 .NET 2.0 上的 C# 编写的大量代码。后端数据库是 MS SQL Server 2000。我们已将代码迁移到 Visual Studio 2010 中 .NET 4.0 上的 C# 并可以编译和构建。问题是旧代码使用 Microsoft.Interop.Office.Word 库来允许我们使用邮件合并和宏嵌入模板生成服务器端报告。我们现在了解到 Visual Studio 2010 不支持服务器端 VBA 或 C# MS Word 应用程序工作。所以我们已经下载了 Open XML 2.0。该文档令人沮丧。我们在网络上找到的无数帖子都谈到从头开始创建文档,很少涉及邮件合并或在 .dotm 文件中运行启用的宏等,其中大多数都涉及 Open XML 1.0。
我可以在 MS Word 中将 .dotx 文件转换为 .xml 文件,然后仅使用 C# 和 Open XML API 加载该 .xml 文件并嵌入从数据库中提取的变量数据吗?每次运行报告时都在代码中动态构建现有的 .xml 文件(即 30 页的报告)似乎绝对是疯狂的。我错过了什么吗?
如果有人成功地做到了这一点,那么使用 C# 代码的示例将会非常有帮助。 谢谢, 艾米丽
We have inherited a ton of code written in C# on .NET 2.0 in Visual Studio 2005. The backend database is MS SQL server 2000. We have migrated the code to C# on .NET 4.0 in Visual Studio 2010 and can compile and build. The problem is the old code used the Microsoft.Interop.Office.Word library to allow us to generate server-side reports using mail merge and macro-embedded templates. We now understand that Visual Studio 2010 does not support server side VBA or C# MS Word application work. So we have downloaded Open XML 2.0. The documentation is beyond frustrating. And the myriad posts we find on the web speak to document creation from scratch, little about mail merging or running enabled macros in .dotm files, etc and most of them deal with Open XML 1.0.
Can I convert a .dotx file to a .xml file in MS Word, then just use C# and the Open XML API to load up that .xml file and embed the variable data pulled from the database? It seems absolutely crazy to construct an EXISTING .xml file (that is a 30 page report) dynamically in code each time the report is run. Am I missing something?
If someone has successfully done this, and example with C# code would be really helpful.
Thanks,
Emily
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一种非常非常简单的查找/替换方法。
{{FirstName}}
。我还没有我自己使用 WordML,所以我不知道是否存在会阻止上述工作的陷阱,但根据我对 Open XML 的理解和 SpreadsheetML 的使用,它应该可以工作。
使用 OpenXML 比 Office Interop 更复杂。 Office Interop API 更接近 Office UI,因此熟悉且更易于使用。 Open XML API 是核心 XML 的一个非常轻量级的包装器,几乎与直接处理 XML 完全相同。如果没有完全相同的自动熟悉度,情况会更加复杂。
然而,OpenXML 比 Office 互操作性好得多。 Office 互操作从未对于服务器使用来说是安全的。将此视为摆脱旧的定时炸弹的好时机。
You can try a really really simple find/replace method.
{{FirstName}}
.I haven't worked with WordML myself so I don't know if there are gotchas that would prevent the above from working, but based on my understanding of Open XML and use of SpreadsheetML, it should work.
Working with OpenXML is more complicated than Office Interop. The Office Interop API was much closer to the Office UI and thus familiar and easier to use. The Open XML API is a very light wrapper on the core XML and is almost exactly the same as dealing with the XML directly. It's more complex without quite the same automatic familiarity.
However, OpenXML is much better than office interop. Office interop has never been safe for server use. Think of this as a good time to get rid of your old ticking time bomb.