C#創(chuàng)建郵件合并模板并合并文本、圖片

對(duì)于Word中的郵件合并功能,用戶可以將郵件合并后的結(jié)果文檔保存并打印,也可以通過(guò)郵件的形式發(fā)送,在很多場(chǎng)合需要使用到此功能。那對(duì)于編程人員,我們也可以在C#語(yǔ)言環(huán)境中通過(guò)代碼的形式來(lái)實(shí)現(xiàn)。根據(jù)需要先創(chuàng)建郵件合并模板后,可合并文本和圖片,在下面的方法中,需要使用到組件Spire.Doc for .NET 。創(chuàng)建模板前,需先安裝該組件,注意添加引用該組件dll文件到控制臺(tái)應(yīng)用程序中,同時(shí)添加命名空間。

創(chuàng)新互聯(lián)長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為斗門企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),斗門網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

一、創(chuàng)建郵件合并模板

第一步:添加命名空間

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;

第二步:主要代碼段

//創(chuàng)建一個(gè)Document類對(duì)象,并添加Section
Document document = new Document();
Section section = document.AddSection();

//添加段落到Section,并向段落添加文字,設(shè)置文字顏色、字體粗細(xì)
Paragraph paragraph = section.AddParagraph();
TextRange tr = paragraph.AppendText("人 物 基 本 信 息");
tr.CharacterFormat.TextColor = Color.YellowGreen;
tr.CharacterFormat.Bold = true;

//添加文本,并添加合并域“Image:Portrait”
paragraph.AppendText("\n人 物 肖 像 : ");
paragraph.AppendField("Image:Portrait", FieldType.FieldMergeField);

//添加文本,并添加合并域“Name”
paragraph.AppendText("\n姓 名 : ");
paragraph.AppendField("Name", FieldType.FieldMergeField);

//添加文本,并添加合并域“Nation”
paragraph.AppendText("\n民 族 :");
paragraph.AppendField("Nation", FieldType.FieldMergeField);

//添加文本,并添加合并域“Nationality”
paragraph.AppendText("\n國(guó) 籍 : ");
paragraph.AppendField("Nationality", FieldType.FieldMergeField);

//添加文本,并添加合并域“Graduated From”
paragraph.AppendText("\n院 校 : ");
paragraph.AppendField("Graduated From", FieldType.FieldMergeField);

//保存并打開文檔
document.SaveToFile("模板.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("模板.docx");

完成以上步驟后,調(diào)試運(yùn)行程序,生成文件(可在項(xiàng)目文件下bin>Debug中查看)
如下圖:
C# 創(chuàng)建郵件合并模板并合并文本、圖片

二:合并文本、圖片

在完成模板創(chuàng)建之后,可添加文本和圖片,如下:
第一步:添加命名空間

using System;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Reporting;

第二步:主要代碼段

static void Main(string[] args)
        {
            //實(shí)例化一個(gè)Document類,并加載文檔模板
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\模板.docx");
            var textFieldNames = new string[] { "Name", "Nation", "Nationality", "Graduated From"};
            var textFieldValues = new string[] { "喬 治?華 盛 頓 (George Washington)", "美 利 堅(jiān) 民 族", "美 國(guó)", "威 廉 與 瑪 麗 學(xué) 院 (William and Mary)"};
            var imageFieldNames = new string[] { "Portrait" };
            var imageFieldValues = new string[] { @"C:\Users\Administrator\Desktop\images\華盛頓.jpg" };

            //合并文本到模板
            doc.MailMerge.Execute(textFieldNames, textFieldValues);

            //創(chuàng)建合并圖片自定義事件
            doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);

            //合并圖片到模板
            doc.MailMerge.Execute(imageFieldNames, imageFieldValues);

            //保存并打開文檔
            doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
        //添加自定義事件載入圖片
        static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field)
        {
            string filePath = field.FieldValue as string;
            if (!string.IsNullOrEmpty(filePath))
            {
                field.Image = Image.FromFile(filePath);
            }
        }

運(yùn)行程序,生成文件,如下圖:

C# 創(chuàng)建郵件合并模板并合并文本、圖片

以上全部?jī)?nèi)容為本文創(chuàng)建郵件合并模板并合并文本和圖片的方法講述,方法中使用到的組件Spire.Doc for .NET在處理Word文檔方面具有很好的輔助作用,感興趣的話可以動(dòng)手試試。如果本文對(duì)你有所幫助,歡迎轉(zhuǎn)載(轉(zhuǎn)載請(qǐng)注明出處)。

當(dāng)前文章:C#創(chuàng)建郵件合并模板并合并文本、圖片
文章來(lái)源:http://www.muchs.cn/article10/ijcjgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、域名注冊(cè)小程序開發(fā)、企業(yè)建站、微信小程序、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)