当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > c# 实现Word联接Excel的MailMerge功能

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 c# 实现Word联接Excel的MailMerge功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 73 ::
收藏到网摘: n/a

目标:使用word的MailMerge功能,数据源是Excel中的数据。这些资料在网上很少,只能自己慢慢测试了。

关于Word的MailMerge功能:

word提供邮件的模板,可以选择各种数据源,比如数据库,excel等,然后群发(或打印、另存文件)邮件。

 

为了实现这个功能,我的程序要能做的是

1:打开word文件对象

2:设置MailMerge数据源:指定Excel,指定查询语句,指定联接的列s

3:关闭保存

 

关于引用:

using Word = Microsoft.Office.Interop.Word;

using System.Reflection;

using System.Diagnostics;

using System.IO;

关于变量:word的com对象需要传入的参数定义

        Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

        object missing = System.Reflection.Missing.Value;

        object falseValue = false;

        object trueValue = true;

关于处理

  需要注意的是

  1:打开word的方式

  2:query的写法。类似于sql一般,比较好玩。

  3:设置列,。设置之后,在word中可以看见这些列。

  4:关闭word之后,还得再copy一次excel。直接生成之后的excel文件size暴涨,文件还打不开,所以覆盖一遍了之。原因不详。

   private void button1_Click(object sender, EventArgs e)

        {

            object fileName = CopyTemplateDoc();//copy doc in

            Word.Document doc = WordApp.Documents.Open(ref fileName, ref missing, ref falseValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref trueValue, ref missing, ref missing, ref missing);

            object linkTo = CopyExcelData();//copy excel data

            object query = "SELECT * FROM `Sheet1$`";//data from sheet1

            object header = "Name,Category,Address,Content";//filed list

            try

            {

                doc.MailMerge.CreateDataSource(ref linkTo, ref missing, ref missing, ref header, ref falseValue, ref query, ref missing, ref missing, ref trueValue);

                doc.MailMerge.Fields.Add(WordApp.Selection.Range, "Name");//add one filed to test

                MessageBox.Show("success");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                doc.Save();//save word

                CloseApp();//close word app

                CopyExcelData();//copy data again,*******************

            }

        }

关于关闭word对象

         public void CloseApp()
        {
            WordApp.Documents.Close(ref trueValue, ref missing, ref missing);
            WordApp.Quit(ref trueValue, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
            GC.Collect();

            //this.KillExcelProcess();
        }

还有两个工具函数不再赘述,用来copy文件并且返回文件名private string CopyExcelData();和private string CopyTemplateDoc()。