...
Info | ||
---|---|---|
| ||
In the downloadable WordWriter_Basic_Tutorials.zip, there is a completed template file located in BusinessLabels/templates/Business_Label_Template.docx. |
...
1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind
Code Block |
---|
using SoftArtisans.OfficeWriter.WordWriter;
|
2. Create a new WordTemplate
object and open the template file.
Code Block |
---|
WordTemplate WT = new WordTemplate();
WT.Open(Page.MapPath("//templates//Business_Label_template.docx"));
|
...
This call is to a helper method GetCSVData
that parses the CSV files and returns a DataTable
with the values.
Code Block |
---|
DataTable dtBusinessLabels = GetCSVData("//data//BusinessLabelTutorialData.csv");
|
...
Recall that SetMailMerge
treats the entire document as a repeat block. This means that the merge fields in the document will need to match the column names of the data source. If there are any stray merge fields in the document, WordWriter will throw an error.
Code Block |
---|
WT.SetMailMerge(dtBusinessLabels);
|
5. Process and save the template file.
Code Block |
---|
WT.Process();
WT.Save(Page.Response, "Business_Label_Output.docx", false);
|
The completed code should look like this:
Code Block |
---|
//Instantiate a new WordTemplate object
WordTemplate WT = new WordTemplate();
//Open the template file
WT.Open(Page.MapPath("//templates//Business_Label_template.docx"));
//Get the order info datatable using GenericParser
DataTable dtBusinessLabels = GetCSVData("//data//BusinessLabelTutorialData.csv");
//Set the data sources to import a single row of data for each source
WT.SetMailMerge(dtBusinessLabels);
//Process to import the data to the template
WT.Process();
WT.Save(Page.Response, "Business_Labels_Output.docx", false);
|
...
By default, NEXT fields will be ignored by WordWriter. To enable NEXT fields, set the WordTemplate.EnableNEXTFields
property. This property must be set before WordTemplate.Open
is called.
Code Block |
---|
WT.EnableNEXTFields = true;
|
Final Code
Code Block |
---|
//Instantiate a new WordTemplate object
WordTemplate WT = new WordTemplate();
//NEXT fields will be ignored by default
//This must be set before Open() is called
WT.EnableNEXTFields = true;
//Open the template file
WT.Open(Page.MapPath("//templates//Business_Label_template.docx"));
//Get the order info datatable using GenericParser
DataTable dtBusinessLabels = GetCSVData("//data//BusinessLabelTutorialData.csv");
//Set the data sources to import a single row of data for each source
WT.SetMailMerge(dtBusinessLabels);
//Process to import the data to the template
WT.Process();
WT.Save(Page.Response, "Business_Labels_Output.docx", false);
|
...