Aspose. word easy to use, aspose. word easy to use
Concepts
To generate a word report using aspose:
Load Template
Provides four overload Methods
?
12345 |
public Document(); public Document(Stream stream); public Document( string fileName); public Document(Stream stream, LoadOptions loadOptions); public Document( string fileName, LoadOptions loadOptions); |
Template Creation
Aspose uses a domain (MergeField) in the word template. A domain is equivalent to a placeholder. Domain, you can choose from menu Insert> document parts.
Data Filling
There are two cases: basic information and list.
Basic information: basic attributes. The list corresponds to the cyclic data structure, such as DataTable.
Use DataTable to fill the list.
Template creation:
The table starts with the keyword TableStart and ends with TableEnd. Add the DataTable table name after the keyword. E.g. TableStart: tableName
In the middle of the header, It is the name of a specific field.
Serial number |
Name |
Gender |
Age |
<TableStart: Name> <Index> |
<Name> |
<Sex> |
<Age> <TableEnd: Name> |
Document doc = new Document(Server.MapPath("~\\templet") + "\\" + name);doc.MailMerge.ExecuteWithRegions(DataTable)
Specific fields
There are two ways to achieve this:
Method 1,
?
123 |
DocumentBuilder builder = new DocumentBuilder(doc); builder.MoveToMergeField(MergeFiled Name); builder.Write(value; |
In this way, one data is filled at a time. However, a field can be used multiple times and can be filled at a time.
Advantages: flexible customization. If you want to replace multiple domain fields at a time, you can do the same with a slight change.
DocumentBuilder builder = new DocumentBuilder(doc);while(builder.MoveToMergeField(MergeFiled Name)){ builder.Write(value;}
Method 2,
doc.MailMerge.Execute(fieldNames, fieldValues);
In the specific development process, Entity is usually selected as the data source. You can obtain data through reflection.
How to obtain domain fields
?
1 |
doc.MailMerge.GetFieldNames() |
Method returns the book string []