In this tutorial, you will learn how to create this pixel-perfect Expense Report using Text Control Reporting and business objects as a data source:
An expense report contains general data such as the purpose of the expense, the pay period and a statement number. Additionally, it should list the employee details and finally, the expenses itself. The class diagram of the business object is illustrated below:
- Expenses: This is an IEnumerable wrapper object for the separate Expense reports. This object is passed to Text Control Reporting.
- Expense: The expense report itself.
- Exployee: Information about the employee referenced in Expense.
- LineItem: Details of each expense referenced in Expense.
The business object Expenses has some useful features. It contains XML Serialization attributes which allows us to use an XML data source that is deserialized into the business object automatically.
[XmlType("expenses", IncludeInSchema = true)] public class Expenses : List<Expense> { [XmlElement("expense")] public List<Expense> expenses { get; set; } } ...
The attributes define which XML element is mapped to which member of the object. An XmlSerializer is used to deserialize the XML in order to create a new instance of the business object.
XmlSerializer serializer = new XmlSerializer(typeof(Expenses)); Expenses expenses = (Expenses)serializer.Deserialize( new StreamReader("expense_report_data.xml"));
Additionally, we don't need to store all values in our XML file (or database in real life applications). The business object contains the logic and the code to calculate specific field values internally. All inverted members in the above illustration are not imported from the XML, but calculated on the fly in the business object itself.
The following code shows how the Total value is calculated based on existing values:
public double Total { get { double dTotal = 0; foreach (LineItem item in this.LineItems) { dTotal += item.line_total; } return dTotal - this.Advances; } }
In order to start the merge process itself, only 1 line of code is required:
mailMerge1.MergeObjects(expenses);
Text Control's Reporting engine MailMerge is mapping the merge fields to the business object members and related objects automatically.
The sample project, written in C#, is very easy to use:
-
Start Visual Studio and load the sample project.
-
Compile and start the project.
-
The template is already merged and you can use the arrow buttons to navigate through the created documents.
You can download the sample project and test it on your own. At least, a TX Text Control .NET for Windows Forms trial version is required.