...
- 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_Note 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.
...
Info | ||
---|---|---|
| ||
In the downloadable WordWriter_Basic_Tutorials.zip under SalesInvoice, there is a completed template file located in SalesInvoice/templates/Part1_Invoice_Template.docx. |
...
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"));
|
...
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[] orderHeader = { "Jane", "Doe", DateTime.Now.ToString("MM/dd/yy")};
string[] orderHeaderColNames = { "FirstName", "LastName", "Date"};
|
5. Create two arrays as the OrderDetail data set.
Code Block |
---|
object[] detailsArray = {"Sport-100 Helmet, Black", 3, 34.99, 104.97, 104.97, 4.46, 109.43 };
string[] detailColNames = {"Item", "Qty", "Price", "LineTotal", "Subtotal", "Tax", "Total" };
|
...
SetDataSource()
binds a single row of data to the template.
Code Block |
---|
WT.SetDataSource(orderHeader, orderHeaderColNames, "OrderHeader");
|
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);
|
The final output should resemble this:
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 header values
object[] orderHeader = { "Jane", "Doe", DateTime.Now.ToString("MM/dd/yy")};
//Create the array of column names
string[] orderHeaderColNames = { "FirstName", "LastName", "Date"};
//Create the array of details values
object[] detailsArray = {"Sport-100 Helmet, Black", 3, 34.99, 104.97, 104.97, 4.46, 109.43 };
//Create the array of column names
string[] detailColNames = {"Item", "Qty", "Price", "LineTotal", "Subtotal", "Tax", "Total" };
//Set the data sources to import a single row of data for each source
WT.SetDataSource(orderHeader, orderHeaderColNames, "OrderHeader");
WT.SetDataSource(detailsArray, detailColNames, "OrderDetails");
//Process to import the data to the template
WT.Process();
WT.Save(Page.Response, "Part1_Output.docx", false);
|
...
You can download the code for the Basic WordWriter Tutorials as a Visual Studio solution, which includes the Simple Expense Summary.