在 Open XML 2.0 中打开 microsoft .xml 文件并添加变量数据

发布于 2024-10-12 02:10:24 字数 573 浏览 7 评论 0原文

我们继承了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2024-10-19 02:10:24

您可以尝试一种非常非常简单的查找/替换方法。

  1. 将模板创建为 .docx 文件并将其保存在服务器上 使用
  2. 易于查找和解析的占位符更改所有邮件合并字段,这些占位符通常不会出现在文档中,也不需要 XML 编码。例如:{{FirstName}}
  3. 为要生成的每个文档创建一个新的空白 .docx (zip) 文件
  4. 从模板 .docx 文件中读取每个文件
  5. 当您读取源文件时,根据需要搜索并替换您的标记
  6. 将文件发送到客户端

我还没有我自己使用 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.

  1. Create your template as a .docx file and save it on the server
  2. Change all your mail-merge fields with easy to find and parse placeholders that are not going to appear int he document normally and will not require XML encoding. For example: {{FirstName}}.
  3. Create a new blank .docx (zip) file for each document you want to generate
  4. Read each file from inside the template .docx file
  5. As you read the source file, search for and replace your tokens as appropriate
  6. Send file to client

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文