C#讀取word模版並對指定域寫入數據保存為新word


引用:

using System;
using System.Collections.Generic;
using
System.Aspose.Words;
using System.Windows.Forms;

獲取模版路徑:

string = "路徑";   //如:string = "e:\\word\\word1.doc"
Document dm = new Document();

將數據放在list<string>中:

List<string> list1 = new List<string>;  //保存的是域
List<string> list2 = new List<string>;  //保存的是要插入的數據
list1.Add("Name");
list2.Add("messege"); //在域名為Name的位置給值messege

將list轉換為string數組,使用Document.MailMerge.Excute(string1,string2)方法;string1是域,string2是數據

string[] s1 = list1.ToArry();
string[] s2 = list2.ToArry();
dm.MailMerge.Excute(s1,s2);    //給域賦值

保存到某個文件夾下新建newdoc.doc文檔:

string savePath = @"e:\word\newdoc.doc";
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = "newdoc.doc";
sfd.InitialDirectory = @"e:\word";
sfd.Filter = "Word 文檔(*.doc)|*.doc";
sfd.show();
dd.Save(sfd.FileName);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM