Table of Contents

Intro

This is Part 2 of a 2-part tutorial series for the Sales Invoice scenario. It is recommended that you complete Part 1 - Getting Started before starting this section.

There is a downloadable ADD FILE REF with completed templates and code. The completed example of the template is available under templates/Part2_Invoice_Template.xlsx. The code for this part of the tutorial can be found in Part2.aspx.cs.

This part focuses on adding repeating data to an order summary. There are slight modifications to the template and code from Part 1.

Adding Repeat Blocks

The starting template is from Part 1 - Getting Started:

Repeat Block is a functionality 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."

1. Add a bookmark around the data you wish to import.

2. In the code behind, call WordTemplate.SetRepeatBlock. Note that the dataset is bound with the bookmark name as an argument.

WT.SetRepeatBlock(dtOrderInfo,"Repeat");

Final Code

For information on writing this code, see Part 1 - Getting Started .

using SoftArtisans.OfficeWriter.WordWriter;
...
//Instantiate a new WordTemplate object
WordTemplate WT = new WordTemplate();

//Open the template file
WT.Open(Page.MapPath("//templates//Part2_Invoice_Template.docx"));

//Create the array of details values
object[] detailsArray 
    = { "Jane", "Doe", DateTime.Now.ToString("MM/dd/yy"), "13139.51", "558.43", "13697.94" };
//Create the array of column names
string[] detailColNames = { "FirstName", "LastName", "Date", "Subtotal", "Tax", "Total" };

//Get the order info datatable using GenericParser
DataTable dtOrderInfo = GetCSVData("//data//OrderInfo.csv");

//Set the repeat block to bind the data for multiple order items
WT.SetRepeatBlock(dtOrderInfo,"Repeat");
//Set the details data source to import a single row of data
WT.SetDataSource(detailsArray, detailColNames, "OrderDetails");

//Process to import the data to the template
WT.Process();

WT.Save(Response, "Part2_Output.docx", false);

Downloads

You can download the code for the Basic WordWriter Tutorials as a Visual Studio solution, which includes the Simple Expense Summary.