Reporting: Expense Report with Business Objects
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.…

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.
Reporting
The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.
Related Posts
Creating Your First ASP.NET Reporting Application
This tutorial shows how to use the MailMerge component in an ASP.NET Web application to merge a template with data to create an Adobe PDF document.
New Online Sample: Build your First Report
We published a new online demo that shows how to create a report including preparing data, creating a template to merging them together.
ReportingDocumentationReportingCloud
Create your First Document with ReportingCloud
As part of our new ReportingCloud documentation, we published a guided tutorial that shows how to create a document without programming.
MailMerge: Starting Each Merge Block on a New Page
A merge block is repeated based on the number of matching data rows in the hierarchical data object. The complete merge block is cloned and inserted under the original location in the template.…
Using MailMerge with JSON Data
In the last article, we explained how to create an ASP.NET Web API to merge templates with JSON data in the payload body of an HTTP request. The focus of this article was on the Web API and…