In this article, we will look at how you can create a file that contains a header, a tabular report with regions and a total sum using the merge template and an XML document as a data source.
The document with the report will be generated automatically and can be saved in DOCX, RTF, PDF, XTML formats.
To create our report you need:
- Create a merge template.
- Load data from
- Insert data into the appropriate merge fields in the template and save the document.
Consider the creation of a simple tabular report on the example of “Pastry Shop”.
Step 1. Create a report template in Microsoft Word. In the title of the document, we can specify the name of the Company, place the logo, contact information, date and time and other information. Below we insert a table with the required number of rows and columns.
In our example it looks like this.
For changeable data we create merge fields, and also specify “RangeStart” and “RangeEnd” regions for them.
Step 2. Then we create XML-document populated by data. As you can see, tag names have the same names as the merge fields in the template.
This is our XML document:
So, when we have a ready-made report template and an XML document filled with data inside the tags, the names of which correspond to the fields for merging in the template, we can proceed to the process of creating our report automatically and save it in the formats we need.
Step 3. Run the merge process and save the report.
During the merge process, the corresponding template fields for each “Order” are populated with data from the XML document as many times as there are records inside each nested RangeStart and RangeEnd of the merge region.
Thus, for each next "Order" from the XML document, a new report will be created in accordance with the template. The "Order" can be placed one after the other and saved separately. Or each order can be placed on a new page of one document
In our example, we create three reports, each from a new page and save the entire document in PDF format.
Running this sample code.
As a result, we get a document consisting of three pages and saved in PDF format.
In this article, we looked at creating a simple tabular report on the example of a “Pastry Shop” using a report template in DOCX format and an XML document as a data source.
You can create your own report template in Microsoft Word or programmatically using Document .Net.
A template can be of any complexity and contain as many tables and merge fields as you need. Using the merge settings, you can insert not only text into the field, but a picture also.
As well as set custom values for the formatting value (in the following articles).
It follows from the above, if your private business or company constantly requires the formation of a large number of similar documents or reports, the Document .net component is a high-quality and high-speed solution.