Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

Adding a WordWriter Reference in Visual Studio

Info
titleFollowing the Sample Code

In the sample code, the reference to SoftArtisans.OfficeWriter.WordWriter.dll has already been added to the SalesInvoice project.

Create a .NET project and add a reference to the WordWriter library.

  1. Open Visual Studio and create a .NET project.
    • The sample code uses a web application.
  2. Add a reference to SoftArtisans.OfficeWriter.WordWriter.dll
    • SoftArtisans.OfficeWriter.WordWriter.dll is located under Program Files > SoftArtisans > OfficeWriter > dotnet > bin

The starting template is from Part 1 - Getting Started: Image Added

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. Image Added

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

Code Block

WT.SetRepeatBlock(dtOrderInfo,"Repeat");

Final Code

Note

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

Code Block
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);

...