首先引用Aspose.Words.dll
之后进行Word制作,插入》文档部件》域
选择MergeField》在域名里输入域名,之后电机确定
效果如下图:
添加图片方式也一样,如域名(Image:Name)
之后创建vs工程引用Aspose.Words.dll后
public void DOC_hzd()
{
Dictionary<string, string> data = new Dictionary<string, string>();
string SBockDoc = Application.StartupPath + @"\回执单.docx";//创建的Word文件路径
//string ewm = Application.StartupPath + @"\ewm.png";
//data["ewm"] = File.Exists(ewmpath) ? ewmpath : "";
data["IdCardNo"] = "姓名";
CreateDocument(SBockDoc, data, Application.StartupPath + @"\1.docx");
}
public static void CreateDocument(string docfile, Dictionary<string, string> data, string filename = "")
{
if (docfile == "" || !File.Exists(docfile)) return;
string[] keys = data.Keys.ToArray();
Object[] values = data.Values.ToArray();
Document d = new Document(docfile);
d.MailMerge.Execute(keys, values);
if (filename != "") d.Save(filename);
d = null;
}