This part focuses on adding repeating data to an order summary. There are slight modifications to the template and code from Part 1.
Changing the Template
The starting template is from Part 1 - Getting Started:
Repeat blocks are used to import multiple rows. A repeat block is a fragment in the template document - defined by a Word bookmark - that contains merge fields and that will be repeated for each row in a data source. To import multiple rows from a single data source, create a repeat block in the template and, in the WordWriter code, call SetRepeatBlock to bind the repeat block to a data source.
In this sample, the repeat block is added to the order info merge fields. This bookmark is called "Repeat."
Add a bookmark around the data you wish to import.
Writing the Code
1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind.
2. In the method that will actually run the report, instantiate the
3. Open the template file with the
4. Create an
object array for the header and total values and a
string array for the column names.
WordTemplate can be bound to numerous types of .NET data structures: single variables, arrays (1-D, jagged, multi-dimensional),
IDataReader etc. The source of the data can come from anywhere.
Some of the aforementioned structures have built in column names, such as the
DataTable. When working with arrays, which don't have built in column names, you have to define the column names in a separate
If you are following in your own project and would like to parse the CSV files as well, you will need to:
- Add a reference to
GeneringParsingat the top of your code.
- Add the
GetCSVDatamethod that can be found in the sample code.
5. Get the datatable for the repeat block.
to pass the data and the bookmark name.
SetDataSource() to bind the order details arrays. Note that the data source name is the last string
WordTemplate.Process to import the data into the file.
WordTemplate.Save to save the output file.
WordTemplate has several output options: save to disk, save to a stream, stream the output file in a page's
Response inline or as an attachment.
The final output should resemble this:
You can download the code for the Basic WordWriter Tutorials as a Visual Studio solution, which includes the Simple Expense Summary.