.net根據模板對Word用Aspose Word操作


命名空間:(記得引入ASPJPEGLib.dll)

usingAspose.Words;

1.創建word模版,使用MergeFeild綁定數據

新建一個Word文檔,命名為Template.doc

 打開Template.doc必須在菜單的"插入→文檔部件→域”找到MergeField並輸入相應的域名

 

2.新建cs使用數組提供數據源

  1. stringtempPath = Server.MapPath("~/Docs/Temp/Template.doc");
  2. stringoutputPath = Server.MapPath("~/Docs/Output/Template.doc");
  3. //載入模板
  4. var doc = new Document(tempPath);
  5. //提供數據源
  6. String[] fieldNames = new String[] {"UserName", "Gender", "BirthDay", "Address"};
  7. Object[] fieldValues = new Object[] {"張三", "男", "1988-09-02", "陝西咸陽"};
  8. //合並模版,相當於頁面的渲染
  9. doc.MailMerge.Execute(fieldNames, fieldValues);
  10. //保存合並后的文檔
  11. doc.Save(outputPath);
  12. //在WebForm中,保存文檔到流中,使用Response.?BinaryWrite輸出該文件
  13. vardocStream = newMemoryStream();
  14. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  15. Response.ContentType = "application/msword";
  16. Response.AddHeader("content-disposition", "attachment; filename=Template.doc");
  17. Response.BinaryWrite(docStream.ToArray());
  18. Response.End();
  19. //在MVC中采用,保存文檔到流中,使用base.File輸出該文件
  20. vardocStream = newMemoryStream();
  21. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  22. returnbase.File(docStream.ToArray(), "application/msword","Template.doc");


免責聲明!

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



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