Aspose Word模板使用總結

來源:互聯網
上載者:User

標籤: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模板使用總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.