標籤:style blog http color io os 使用 ar for
Aspose Word模板使用總結
1.建立word模版,
使用MergeFeild
綁定資料 建立一個Word文檔,命名為Template.doc 注意:這裡並不是輸入"《”和“》”就可以了,而是必須在菜單的"插入→快速組件→域”找到MergeField並輸入相應的網域名稱
2.使用數組提供資料來源 string tempPath = Server.MapPath("~/Docs/Temp/Template.doc"); string outputPath = 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輸出該檔案 var docStream = new MemoryStream(); 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輸出該檔案 var docStream = new MemoryStream(); doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); return base.File(docStream.ToArray(), "application/msword","Template.doc");3.
建立迴圈資料的模版,這裡的迴圈資料類似頁面的for結構,不拘泥於形式table
«TableStart:UserList»
姓名:«UserName»
«TableEnd:UserList»
4.使用DataTable提供資料來源
//建立名稱為UserList的DataTable
DataTable table=new DataTable("UserList");
table.Columns.Add("UserName");
table.Columns.Add("Gender");
table.Columns.Add("BirthDay");
table.Columns.Add("Address");
//----------------------------------------------------------------------------------------------------
//載入模板
var doc = new Document(tempPath); //提供資料來源 var datatable= GetDataTable(); //合并模版,相當於頁面的渲染 doc.MailMerge.ExecuteWithRegions(datatable); var docStream = new MemoryStream(); doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); return base.File(docStream.ToArray(), "application/msword","Template.doc");
5.綁定帶有子迴圈資料模版
6.使用DataSet提供資料來源//使用者表結構 DataTable table = new DataTable("UserList"); table.Columns.Add(new DataColumn("Id", typeof(int))); table.Columns.Add("UserName"); table.Columns.Add("Gender"); table.Columns.Add("BirthDay"); table.Columns.Add("Address");//分數表結構 DataTable table = new DataTable("ScoreList"); table.Columns.Add(new DataColumn("UserId", typeof(int))); table.Columns.Add("Name"); table.Columns.Add("Score");//----------------------------------------------------------------------------------------------------//載入模板 var doc = new Document(tempPath); //提供資料來源 DataSet dataSet = new DataSet(); var userTable= GetUserDataTable(); var userScoreTable= GetUserScoreDataTable(); dataSet.Tables.Add(userTable); dataSet.Tables.Add(userScoreTable); dataSet.Relations.Add(new DataRelation("ScoreListForUser",userTable.Columns["Id"], userScoreTable.Columns["UserId"])); //合并模版,相當於頁面的渲染 doc.MailMerge.ExecuteWithRegions(dataSet); var docStream = new MemoryStream(); doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); return base.File(docStream.ToArray(), "application/msword","Template.doc");
7.模版上使用書籤,插入標記位置選中文檔中的文字,在菜單的"插入→書籤”指定書籤的名稱,排序依據選定為位置,添加一個新書籤。選中的文字為書籤的Text屬性,這裡是為了方便查看。也可以直接插入一個書籤並指定位置,只是不明顯。
8.在書籤位置插入另一個文檔的內容//載入模板 var doc = new Document(tempPath); var doc1 = new Document(tempPath1);//新文檔//找到名稱為PositionFlag的書籤 var bookmark= doc.Range.Bookmarks["PositionFlag"];//清空書籤的文本 bookmark.Text = "";//使用DocumentBuilder對象插入一些文檔對象,如插入書籤,插入文字框,插入複選框,插入一個段落,插入空白頁,追加或另一個word檔案的內容等。 var builder = new DocumentBuilder(doc);//定位到指定位置進行插入操作 builder.MoveToBookmark("PositionFlag");//在PositionFlag書籤對應的位置,插入另一個文檔的內容。//InsertDocument方法可以在http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+Document找到 InsertDocument(bookmark.BookmarkStart.ParentNode, doc1);
Aspose Word模板使用總結