Aspose.words合併文件、郵件合併功能
阿新 • • 發佈:2019-01-24
aspose.words 15.12版本
把兩個檔案合併成一個檔案
string fp1 = Request.PhysicalApplicationPath + "111.doc"; FileStream fs1 = new FileStream(fp1, FileMode.Open); Document doc1 = new Document(fs1); string fp2 = Request.PhysicalApplicationPath + "222.doc"; FileStream fs2 = new FileStream(fp2, FileMode.Open); Document doc2 = new Document(fs2); Document doc3 = new Document(); doc3.RemoveAllChildren(); doc3.AppendDocument(doc1, ImportFormatMode.UseDestinationStyles); fs1.Close(); fs2.Close(); fs1.Dispose(); fs2.Dispose(); doc3.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles); doc3.Save(Request.PhysicalApplicationPath + "333.docx", SaveFormat.Docx); doc3.Save(Request.PhysicalApplicationPath + "333.pdf", SaveFormat.Pdf);
郵件合併功能,可以將所有檔案單獨儲存,也可以合併儲存一個檔案
string fp1 = Request.PhysicalApplicationPath + "1.docx"; FileStream fs1 = new FileStream(fp1, FileMode.Open); Document doc1 = new Document(fs1); DataTable dt = GetDataSource(); // Loop though all records in the data source. Document outDoc = new Document(); outDoc.RemoveAllChildren(); foreach (DataRow row in dt.Rows) { Document dstDoc = (Document)doc1.Clone(true); dstDoc.MailMerge.Execute(row); outDoc.AppendDocument(dstDoc, ImportFormatMode.KeepSourceFormatting); //dstDoc.Save(string.Format(Request.PhysicalApplicationPath + "{0}.doc", row["username"])); } outDoc.Save(string.Format(Request.PhysicalApplicationPath + "out.doc")); fs1.Close(); fs1.Dispose();
生成一個示含例表
private DataTable GetDataSource() { DataTable table = new DataTable(); table.Columns.Add("username"); table.Columns.Add("id"); table.Columns.Add("kkkk"); table.Rows.Add("張三", "03-5555555"); table.Rows.Add("李四", "03-5522555"); table.Rows.Add("王武", "03-5554455"); return table; }
第二個示例改自慧聰控制元件網
在模板Doc中,先要插入 “域”,MergeFiled, 域名對應表的欄位名,