Aspose Word模板使用總結

Aspose Word模板使用總結

命名空間:

  1. using Aspose.Words;
  2. using Aspose.Words.Saving;
  3. using System.IO;
  4. using System.Data;

添加dll

鏈接:http://pan.baidu.com/s/1pJG899T 密碼:bv3k


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

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

 

    注意:這裏並不是輸入"《”和“》”就可以了,而是必須在菜單的"插入→文檔部件→域”找到MergeField並輸入相應的域名

 

2.使用數組提供數據源

  1. string tempPath = Server.MapPath("~/Docs/Temp/Template.doc");
  2. string outputPath = 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. var docStream = new MemoryStream();
  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. var docStream = new MemoryStream();
  21. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  22. return base.File(docStream.ToArray(), "application/msword","Template.doc");

3.創建循環數據的模版,這裏的循環數據類似頁面的for結構,不拘泥於形式table

   «TableStart:UserList»

   姓名:«UserName»

   «TableEnd:UserList»

   

4.使用DataTable提供數據源

  1. //創建名稱爲UserList的DataTable
  2. DataTable table=new DataTable("UserList");
  3. table.Columns.Add("UserName");
  4. table.Columns.Add("Gender");
  5. table.Columns.Add("BirthDay");
  6. table.Columns.Add("Address");
  7. //----------------------------------------------------------------------------------------------------
  8. //載入模板
  9. var doc = new Document(tempPath);
  10. //提供數據源
  11. var datatable= GetDataTable();
  12. //合併模版,相當於頁面的渲染
  13. doc.MailMerge.ExecuteWithRegions(datatable);
  14. var docStream = new MemoryStream();
  15. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  16. return base.File(docStream.ToArray(), "application/msword","Template.doc"); 

 5.綁定帶有子循環數據模版

 

6.使用DataSet提供數據源

  1. //用戶表結構
  2. DataTable table = new DataTable("UserList");
  3. table.Columns.Add(new DataColumn("Id", typeof(int)));
  4. table.Columns.Add("UserName");
  5. table.Columns.Add("Gender");
  6. table.Columns.Add("BirthDay");
  7. table.Columns.Add("Address");
  8. //分數表結構
  9. DataTable table = new DataTable("ScoreList");
  10. table.Columns.Add(new DataColumn("UserId", typeof(int)));
  11. table.Columns.Add("Name");
  12. table.Columns.Add("Score");
  13. //----------------------------------------------------------------------------------------------------
  14. //載入模板
  15. var doc = new Document(tempPath);
  16. //提供數據源
  17. DataSet dataSet = new DataSet();
  18. var userTable= GetUserDataTable();
  19. var userScoreTable= GetUserScoreDataTable();
  20. dataSet.Tables.Add(userTable);
  21. dataSet.Tables.Add(userScoreTable);
  22. dataSet.Relations.Add(new DataRelation("ScoreListForUser",userTable.Columns["Id"],?userScoreTable.Columns["UserId"]));
  23. //合併模版,相當於頁面的渲染
  24. doc.MailMerge.ExecuteWithRegions(dataSet);
  25. var docStream = new MemoryStream();
  26. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  27. return base.File(docStream.ToArray(), "application/msword","Template.doc");

7.模版上使用書籤,插入標記位置

 

選中文檔中的文字,在菜單的"插入→書籤”指定書籤的名稱,排序依據選定爲位置,添加一個新書籤。選中的文字爲書籤的Text屬性,這裏是爲了方便查看。也可以直接插入一個書籤並指定位置,只是不明顯。

8.在書籤位置插入另一個文檔的內容

  1. //載入模板
  2. var doc = new Document(tempPath);
  3. var doc1 = new Document(tempPath1);//新文檔
  4. //找到名稱爲PositionFlag的書籤
  5. var bookmark= doc.Range.Bookmarks["PositionFlag"];
  6. //清空書籤的文本
  7. bookmark.Text = "";
  8. //使用DocumentBuilder對象插入一些文檔對象,如插入書籤,插入文本框,插入複選框,插入一個段落,插入空白頁,追加或另一個word文件的內容等。
  9. var builder = new DocumentBuilder(doc);
  10. //定位到指定位置進行插入操作
  11. builder.MoveToBookmark("PositionFlag");
  12. //在PositionFlag書籤對應的位置,插入另一個文檔的內容。
  13. //InsertDocument方法可以在http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document找到
  14. InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);

9.創建word模版,使用MergeFeild插入圖片

 

 

10.插入圖片示例

  1. string tempPath = Server.MapPath("~/Docs/Temp/Template.doc");
  2. string logoPath = Server.MapPath("~/Content/logo.jpg");
  3. var doc = new Document(tempPath); //載入模板
  4. //提供數據源
  5. String[] fieldNames = new String[] { "logo", "Gender", "BirthDay", "Address","Logo" };
  6. Object[] fieldValues = new Object[] { "張三", "男", "1988-09-02", "陝西咸陽",logoPath };
  7. //增加處理圖片大小程序
  8. //doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertDocument();
  9. //合併模版,相當於頁面的渲染
  10. doc.MailMerge.Execute(fieldNames, fieldValues);
  11.  
  12. //在MVC中採用,保存文檔到流中,使用base.File輸出該文件
  13. var docStream = new MemoryStream();
  14. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  15. return base.File(docStream.ToArray(), "application/msword", "Template.doc");

  效果如下:

  

 增加圖片大小處理的程序

  1. //Aspose.Word提供了一個類似Handler的功能,IFieldMergingCallback允許我們動態的處理MergeField
  2. void IFieldMergingCallback.FieldMerging(FieldMergingArgs e){} //處理文本
  3. void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args){} //處理圖片
  4. //這裏我們處理圖片寫了一個自定義的類實現
  5. class HandleMergeFieldInsertDocument : IFieldMergingCallback
  6. {
  7.     //文本處理在這裏,如果寫在這一塊,則不起作用
  8.     void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
  9.     {
  10.  
  11.     }
  12.     //圖片處理在這裏
  13.     void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
  14.     {
  15.         if (args.DocumentFieldName.Equals("Logo"))
  16.         {
  17.             // 使用DocumentBuilder處理圖片的大小
  18.             DocumentBuilder builder = new DocumentBuilder(args.Document);
  19.             builder.MoveToMergeField(args.FieldName);
  20.  
  21.             Shape shape = builder.InsertImage(args.FieldValue.ToString());
  22.  
  23.             // 設置x,y座標和高寬.
  24.             shape.Left = 0;
  25.             shape.Top = 0;
  26.             shape.Width = 60;
  27.             shape.Height = 80;
  28.         }
  29.     }
  30. }

效果如下:

 

11.向模版插入Html

 

這裏的家鄉簡介使用html格式

12.插入html示例

  1.  string tempPath = Server.MapPath("~/Docs/Temp/Template.doc");
  2. string descHtml = "";//這裏是html文本,由於太長略去
  3. var doc = new Document(tempPath); //載入模板
  4. //提供數據源
  5. String[] fieldNames = new String[] { "UserName", "Gender", "BirthDay", "Address","Desc"};
  6. Object[] fieldValues = new Object[] { "張三", "男", "1988-09-02", "陝西咸陽", descHtml};
  7. //增加處理html程序
  8. doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
  9. //合併模版,相當於頁面的渲染
  10. doc.MailMerge.Execute(fieldNames, fieldValues);
  11. //在MVC中採用,保存文檔到流中,使用base.File輸出該文件
  12. var docStream = new MemoryStream();
  13. doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
  14. return base.File(docStream.ToArray(), "application/msword", "Template.doc");
  15. //如果不增加html的處理程序,默認以文本的輸出,這裏我們寫一個自定義的處理類
  16. class HandleMergeFieldInsertHtml : IFieldMergingCallback
  17. {
  18.     //文本處理在這裏
  19.     void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
  20.     {
  21.         if (e.DocumentFieldName.Equals("Desc"))
  22.         {
  23.             // 使用DocumentBuilder處理圖片的大小
  24.             DocumentBuilder builder = new DocumentBuilder(e.Document);
  25.             builder.MoveToMergeField(e.FieldName);
  26.             builder.InsertHtml(e.FieldValue.ToString());
  27.         }
  28.     }
  29.     //圖片處理在這裏
  30.     void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
  31.     {
  32.  
  33.     }
  34. }
  35. //IFieldMergingCallback在循環結構中同樣適用
  36. //小結:利用書籤加上標誌位,利用自定義的IFieldMergingCallback靈活處理各種需求,後邊會繼續嘗試根據條件加載不同的模版
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章