使用 Open XML SDK 进行邮件合并

发布于 2024-07-16 07:15:41 字数 449 浏览 5 评论 0原文

我有一个包含 3 列(a、b、c)的 DataTable 和一个设置了相应 MailMerge 字段的 docx 文件。 我想做的是对包含数据的文档执行邮件合并。

假设您可以写入硬盘(如果您需要创建 csv 等文件来进行合并等),您没有有 word、excel 等,Open XML SDK是< /em> 已安装,但同样我们可以安装其他任何东西。

就答案而言,将输入数据转换为所需的数据并不是真正的问题,问题是如何在 Open XML SDK(或其他免费 API)中执行邮件合并。

附带说明一下,输出应该是一个包含 n 页的文件(其中 n 是数据中的行数),即不是 n 个文档(尽管我不介意文档合并是否在最后完成)。

(我应该补充一点,我与 MailMerge 概念无关,例如能够仅进行替换就可以了 - 尽管显然这需要最后将文件合并在一起......)

I have a DataTable with 3 columns (a,b,c) and a docx file with the corresponding MailMerge fields set up. What I'd like to do is perform a Mail Merge on the document with the data.

Presume that you can write to the hard disk (if you need to create a csv etc for doing the merge etc), you don't have word, excel etc, Open XML SDK is installed, but equally we can install anything else.

In terms of the answer, converting the input data to whatever is needed isn't really a problem, the problem is how to perform a mail merge in the Open XML SDK (or other FREE API).

As a side note, the output should be one file with n pages (where n is the number of rows in the data), i.e. not n documents (though I don't mind if the merge of documents is done at the end).

(I should add, I'm not tied to the MailMerge concept, being able to just do a replace for example would work - though obviously that then requires merging the files together at the end...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

善良天后 2024-07-23 07:15:41

我以一种非常可怕的方式工作 - 基本上 - 目前,算法如下:

  1. 解压缩 docx 文件
  2. 读取 document.xml (为字符串)
  3. 字符串。替换字段
  4. 重新压缩到临时 docx
  5. 合并所有临时文档创建

合并文档的实际代码来自 Eric White 的博客: http://blogs.msdn.com/ericwhite/archive/2009/02/05/move-insert-delete-文字处理文档中的段落使用-the-open-xml-sdk.aspx

I've got this working in a pretty horrible way - basically - at the moment, the algorithm follows this:

  1. Unzip docx file
  2. Read in document.xml (to a string)
  3. string.Replace the fields
  4. Rezip to temporary docx
  5. Merge all the temporary documents created

The actual code for merging the documents comes from Eric White's Blog : http://blogs.msdn.com/ericwhite/archive/2009/02/05/move-insert-delete-paragraphs-in-word-processing-documents-using-the-open-xml-sdk.aspx

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