命名空間:(記得引入ASPJPEGLib.dll) usingAspose.Words; 1.創建word模版,使用MergeFeild綁定數據 新建一個Word文檔,命名為Template.doc 打開Template.doc必須在菜單的"插入→文檔部件→域”找到MergeField並輸入相應的域 ...
命名空間:(記得引入ASPJPEGLib.dll)
usingAspose.Words;
1.創建word模版,使用MergeFeild綁定數據
新建一個Word文檔,命名為Template.doc
打開Template.doc必須在菜單的"插入→文檔部件→域”找到MergeField並輸入相應的功能變數名稱
2.新建cs使用數組提供數據源
- stringtempPath = Server.MapPath("~/Docs/Temp/Template.doc");
- stringoutputPath = Server.MapPath("~/Docs/Output/Template.doc");
- //載入模板
- var doc = new Document(tempPath);
- //提供數據源
- String[] fieldNames = new String[] {"UserName", "Gender", "BirthDay", "Address"};
- Object[] fieldValues = new Object[] {"張三", "男", "1988-09-02", "陝西咸陽"};
- //合併模版,相當於頁面的渲染
- doc.MailMerge.Execute(fieldNames, fieldValues);
- //保存合併後的文檔
- doc.Save(outputPath);
- //在WebForm中,保存文檔到流中,使用Response.?BinaryWrite輸出該文件
- vardocStream = newMemoryStream();
- doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
- Response.ContentType = "application/msword";
- Response.AddHeader("content-disposition", "attachment; filename=Template.doc");
- Response.BinaryWrite(docStream.ToArray());
- Response.End();
- //在MVC中採用,保存文檔到流中,使用base.File輸出該文件
- vardocStream = newMemoryStream();
- doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
- returnbase.File(docStream.ToArray(), "application/msword","Template.doc");