Page tree

Versions Compared

Key

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

...

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

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," "SubTotal," "Tax," and "Total." 

 The template should resemble this:

...

  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

Info
titleFollowing the Sample Code

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

Code Block

using SoftArtisans.OfficeWriter.WordWriter;

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

Code Block

WordTemplate WT = new WordTemplate();

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

Code Block

WT.Open(Page.MapPath("//templates//Part1_Invoice_Template.docx"));

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.

Code Block

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

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

Info
titleFollowing the Sample

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.

Code Block

 DataTable dtOrderInfo = GetCSVData("//data//OrderInfo.csv");

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.

Code Block

WT.SetDataSource(dtOrderInfo);

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

Code Block

 WT.SetDataSource(detailsArray, detailColNames, "OrderDetails");

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

Code Block

WT.Process();

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.

Code Block

WT.Save(Page.Response, "Part1_Output.docx", false);

Final Code

Code Block


using SoftArtisans.OfficeWriter.WordWriter;
...

//Instantiate a new WordTemplate object
WordTemplate WT = new WordTemplate();

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

//Create the array of details values
object[] detailsArray = { "Jane", "Doe", DateTime.Now.ToString("MM/dd/yy"), "104.97", "4.46", "109.43" };
//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 data sources to import a single row of data for each source
WT.SetDataSource(dtOrderInfo);
WT.SetDataSource(detailsArray, detailColNames, "OrderDetails");

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

WT.Save(Page.Response, "Part1_Output.docx", false);

Downloads

Next Steps