...
Info | ||
---|---|---|
| ||
There is a downloadable ADD FILE REF with WordWriter_Basic_Tutorials.zip with completed templates and code. The completed example of the template is available under templates/Part2_Invoice_Template.xlsx. The code for this part of the tutorial can be found in Part2.aspx.cs. |
...
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//Part2_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"), 13139.51, 558.43, 13697.94 };
string[] orderHeaderColNames = { "FirstName", "LastName", "Date", "Subtotal", "Tax", "Total" };
|
Info | ||
---|---|---|
| ||
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 datadata _directory. There is a copy of the CSV parser, |
...
- 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.
5. Get the datatable for the repeat block.
Code Block |
---|
DataTable dtOrderInfo = GetCSVData("//data//OrderInfo.csv");
|
6. useUse
WordTemplate.SetRepeatBlock
to pass the data and the bookmark name.
Code Block |
---|
WT.SetRepeatBlock(dtOrderInfo,"Repeat");
|
7. Use SetDataSource()
to bind the order details arrays. Note that the data source name is the last string
Code Block |
---|
WT.SetDataSource(orderHeader, orderHeaderColNames, "OrderHeader");
|
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, "Part2_Output.docx", false);
|
The final output should resemble this:
Final Code
Note |
---|
Code Block |
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 header 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 header data source to import a single row of data
WT.SetDataSource(orderHeader, orderHeaderColNames, "OrderHeader");
//Process to import the data to the template
WT.Process();
WT.Save(Response, "Part2_Output.docx", false);
|
...
You can download the code for the Basic WordWriter Tutorials as a Visual Studio solution, which includes the Simple Expense Summary.
- TODO: Add LinkWordWriter_Basic_Tutorials.zip