Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Table of Contents

Getting Started

About templates and merge fields

A WordWriter template is a Microsoft Word file that contains merge fields. A merge field displays a data source field (for example, a database column name). A merge field is created in Microsoft Word and bound in code to a data source. The data source may be an array, a DataSet, a DataTable, or a DataReader. When you run the code, WordWriter populates the merge fields with data source values. A template may contain multiple sets of merge fields. Each set of fields binds to a single data source. 

Merge field syntax

  • The general syntax is: DataSource.ColumnName
  • Ordinal syntax (i.e. #1.#2 for source one, column two) can also be used
  • Data source and column names must not include Unicode characters.
  • Data source and column names must begin with a letter (A-Z, a-z).
  • Data source and column names may include the following characters only:
    ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_
    Icon

    If you assigned a different data source separator, you can also use a "." in your data source and column names.

  • Spaces are not allowed anywhere in a merge field.
Icon

Use brackets if your column name does not conform to these rules
The exception of these rules is when a column name is enclosed in brackets (for example, «Products.[Product Category]» . To include spaces or unicode characters in the data source column name, use this format:«DataSource.[Column Name]»

Setting up the Template

Adding merge fields

The final template will resemble this:

Following the Sample Code

Icon

In the downloadable FILEREFERENCE under SalesInvoice, there is a completed template file located in SalesInvoice/templates/Part1_Invoice_Template.docx.

1. Start with a blank .docx file. 

2. Add the merge fields for the header and total data. These values are a single row of data called "OrderDetails" The values are "FirstName," "LastName," "Date," "SubTotal," "Tax," and "Total."

3. Create a table for the order data and add the merge fields. These values are in a data table created from AdventureWorks data. In this sample, the values are "Item," "Qty," "Price," and "LineTotal." 

 The template should resemble this:

4. The template can be styled as desired. Any formatting applied to a merge field will be persisted when WordWriter populates the data.

Adding a WordWriter Reference in Visual Studio

Following the Sample Code

Icon

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

Writing the Code

Following the Sample Code

Icon

There is a sample web application page Part1.aspx and code behind Part1.aspx.cs available in the SalesInvoice/ directory that shows the completed code.

1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind

2. In the method that will actually run the report, instantiate the WordTemplate object.

3. Open the template file with the WordTemplate.Open method.

4. Create an object array for the header 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), DataSet,DataTableIDataReader 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 string array.

5. Get the data for the Order Info data set.

Following the Sample

Icon

In the sample project, we are parsing CSV files with query results, rather than querying a live database. The CSV files are available under the datadirectory. There is a copy of the CSV parser, GenericParsing.dll in the bin directory of the project GetCSVData is defined in Part1.aspx.cs in aregion marked Utility Methods.

This call is to a helper method GetCSVData that parses the CSV files and returns a DataTable with the values.

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 GenericParsing.dll
  • Include GeneringParsing at the top of your code.
  • Add the GetCSVData method that can be found in the sample code.

6. Use the WordTemplate.SetDataSource method to bind the order info to the merge fields in the template file. This data source has no name. One data source can be bound without a name, but it must be bound first.

SetDataSource() binds a single row of data to the template.

7. Use SetDataSource() to bind the order details arrays. Note that the data source name is the last string

8. Call WordTemplate.Process to import the data into the file.

9. Call 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:

Final Code

Downloads

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

  • TODO: Add Link

Next Steps

Continue on to Part 2: Repeat Blocks

  • No labels