Aspose Word Template Usage Summary
1. Create a Word template,
using Mergefeild
binding DataCreate a new Word document named Template.docNote: This is not the "" "and" ", but must be in the menu" Insert → document parts → domain "to find mergefield and enter the appropriate domain name
2. Using arrays to provide data sourcesstring temppath = Server.MapPath ("~/docs/temp/template.doc");string outputPath = Server.MapPath ("~/docs/output/template.doc");//Load Templatevar doc = new Document (temppath);//Provide data sourcestring[] FieldNames = new string[] {"UserName", "Gender", "BirthDay", "Address"}; object[] fieldvalues = new object[] {"Zhang San", "Male", "1988-09-02", "Shaanxi Xianyang"};//merge template, equivalent to the rendering of the pageDoc. Mailmerge.execute (FieldNames, fieldvalues);// Save the merged document Doc. Save (OutputPath);//In WebForm, Save the document to the stream, using response. BinaryWrite Output The file 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 (); //adopted in MVC, save the document to the stream, using base. File Output var docstream = new MemoryStream ();Doc. Save (Docstream, Saveoptions.createsaveoptions (Saveformat.doc));return base. File (Docstream.toarray (), "Application/msword", "Template.doc");3.
Create a template for the cyclic data, where the loop data is similar to the page's for structure, not rigidly tied to the form table
«tablestart:userlist»
Name:«username»
«tableend:userlist»
4. Using a DataTable to provide a data source
Create a DataTable named UserList
DataTable table=new DataTable ("UserList");
Table. Columns.Add ("UserName");
Table. Columns.Add ("Gender");
Table. Columns.Add ("BirthDay");
Table. Columns.Add ("Address");
//----------------------------------------------------------------------------------------------------
Loading templates
var doc = new Document (temppath);//Provide data sourcevar datatable= getdatatable ();//merge template, equivalent to the rendering of the page 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. Binding with sub-cyclic data templates
6. Using a dataset to provide a data source//user table structuredatatable 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");//SCORE Table Structuredatatable table = new DataTable ("Scorelist");table. Columns.Add (New DataColumn ("UserId", typeof (int)));table. Columns.Add ("Name");table. Columns.Add ("score");//----------------------------------------------------------------------------------------------------//Load Templatevar doc = new Document (temppath);//Provide data sourceDataSet 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 "]));//merge template, equivalent to the rendering of the page 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. Use bookmarks on template, insert marker positionSelect the text in the document, and in the menu, insert → bookmark, specify the name of the bookmark, sort by selected as location, and add a new bookmark. The selected text is the bookmark's Text property and is here for easy viewing. You can also insert a bookmark directly and specify a location, just not obvious.
8. Insert the contents of another document at the bookmark location//Load Templatevar doc = new Document (temppath);var doc1 = new Document (tempPath1); //New document //Find a bookmark named Positionflagvar bookmark= doc. range.bookmarks["Positionflag"];//Empty the bookmark's textbookmark. Text = "";//Use the Documentbuilder object to insert some document object, such as inserting a bookmark, inserting a text box, inserting a check box, inserting a paragraph, inserting a blank page, appending or another Word file's contents, etc. var builder = new Documentbuilder (DOC);//Navigate to the specified location for insert operationBuilder. MoveToBookmark ("Positionflag");//In the location of the Positionflag bookmark, insert the contents of another document. the//insertdocument method can be used in http://www.aspose.com/docs/display/wordsnet/How+to++Insert+a+Document+into+another+ Document foundinsertdocument (bookmark. Bookmarkstart.parentnode, Doc1);
Aspose Word Template Usage Summary