你如何在c#中邮件合并word文档 [英] How do you mail merge a word document in c#

查看:26
本文介绍了你如何在c#中邮件合并word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 c# 应用程序中,我想根据应用程序中的数据生成报告(word 文档),我认为最好的方法是使用我的应用程序中的数据源执行类似邮件合并的操作.

In my c# application I would like to generate a report (word document) from data in my application, I figured that the best way to do this would be to perform something like a mail merge using the data source from my application.

  1. 我尝试按照此操作邮件合并到 word但是这使用了 GemBox,您需要为此付费
  2. 我曾尝试使用 Microsoft.Office.Interop.Word 但是我失败了简短的当我不知道如何引用保存的模板文档时:

  1. I tried following this Mail Merge into word however this uses GemBox which you need to pay for
  2. I have tried using Microsoft.Office.Interop.Word however I fell short when I didn't know how to reference the saved template document:

Dictionary<string, string> MailMerge = new Dictionary<string, string>()
    {
        { "ID", "123" },
        { "Name", "Test" },
        { "Address1", "Test" },
        { "Address2", "Test" },
        { "Address3", "Test" },
        { "Address4", "Test" },
        { "PostCode", "Test" },
        { "Year End", "Test" },
        { "SicCode", "123" },
    };

    Document doc = new Document();
    doc.MailMerge.Execute(MailMerge);

总结

我正在寻找关于进一步研究什么的一些指导,因为我相信必须有一种标准"的方法来做到这一点.

Summary

I'm looking for some guidance as to what to research further as I believe there must be a 'standard' way of doing this.

推荐答案

这很简单,使用 Microsoft.Office.Interop.Word.此处 是有关如何执行此操作的简单分步教程.

This is quite simple by using Microsoft.Office.Interop.Word. Here is a simple step by step tutorial on how to do this.

用字符串替换合并域的代码是这样的:

The code to replace a mergefield with a string is like this:

public static void TextToWord(string pWordDoc, string pMergeField, string pValue)
{
    Object oMissing = System.Reflection.Missing.Value;
    Object oTrue = true;
    Object oFalse = false;
    Word.Application oWord = new Word.Application();
    Word.Document oWordDoc = new Word.Document();
    oWord.Visible = true;
    Object oTemplatePath = pWordDoc;
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
    foreach (Word.Field myMergeField in oWordDoc.Fields)
    {
        Word.Range rngFieldCode = myMergeField.Code;
        String fieldText = rngFieldCode.Text;
        if (fieldText.StartsWith(" MERGEFIELD"))
        {
            Int32 endMerge = fieldText.IndexOf("\");
            Int32 fieldNameLength = fieldText.Length - endMerge;
            String fieldName = fieldText.Substring(11, endMerge - 11);
            fieldName = fieldName.Trim();
            if (fieldName == pMergeField)
            {
                myMergeField.Select();
                oWord.Selection.TypeText(pValue);
            }
        }
    }
}

最初发布在此处这里

如果您希望使用字典一次替换多个字段,请使用以下代码:

In case you wish to use a dictionary to replace many fields at once use the code below:

public static void TextToWord(string pWordDoc, Dictionary<string, string> pDictionaryMerge)
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;
        Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
        oWord.Visible = true;
        Object oTemplatePath = pWordDoc;
        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

        foreach (Microsoft.Office.Interop.Word.Field myMergeField in oWordDoc.Fields)
        {
            Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;
            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\");
                Int32 fieldNameLength = fieldText.Length - endMerge;
                String fieldName = fieldText.Substring(11, endMerge - 11);
                fieldName = fieldName.Trim();
                foreach (var item in pDictionaryMerge)
                {
                    if (fieldName == item.Key)
                    {
                        myMergeField.Select();
                        oWord.Selection.TypeText(item.Value);
                    }
                }
            }
        }
    }

这篇关于你如何在c#中邮件合并word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆