Intro
Excerpt |
---|
A generic purchase order document with a header section and an expanding Word table for detail rows. |
This demo uses the setDataSource and setRepeatBlock methods together. The SetDataSource method is used to assign all of the single-value items to the order header and total sections. A repeat block is defined in the template document around one table row so that the table will automatically expand for every row of data in the order detail query.
Code
Code Block |
---|
public void GenerateDocument()
{
// Create an instance of WordTemplate
WordTemplate wt = new WordTemplate();
// Open the template document
string templatePath = @"..\..\WordTemplateFiles\PurchaseOrderTemplate.docx";
wt.Open(templatePath);
// Query the database for header and detail information.
// Set these DataTables as WordTemplate data sources.
DataTable dtHeader = GetOrderHeaderData(@"..\..\WordData\PurchaseOrderDemoHeaderData.csv");
DataTable dtDetail = GetOrderDetailData(@"..\..\WordData\PurchaseOrderDemoDetailData.csv");
wt.SetDataSource(dtHeader);
wt.SetRepeatBlock(dtDetail, "OrderDetailQuery");
wt.Process();
// Save the document to the disc
wt.Save(@"..\..\WordOutputFiles\PurchaseOrder_output.docx");
}
/// <summary>Retrieve a DataTable of order header data</summary>
/// <param name="orderId">OrderID for which to retrieve data</param>
/// <returns>DataTable of the requested order's data</returns>
private DataTable GetOrderHeaderData(String csvFileName)
{
DataTable dt;
using (GenericParserAdapter parser = new GenericParserAdapter(csvFileName))
{
parser.ColumnDelimiter = ',';
parser.FirstRowHasHeader = true;
dt = parser.GetDataTable();
}
return dt;
}
/// <summary>Retrieve a ReusltSet of order detail line items</summary>
/// <param name="orderId">OrderID for which to retrieve data</param>
/// <returns>DataTable of the requested order's line items</returns>
private DataTable GetOrderDetailData(String csvFileName)
{
DataTable dt;
using (GenericParserAdapter parser = new GenericParserAdapter(csvFileName))
{
parser.ColumnDelimiter = ',';
parser.FirstRowHasHeader = true;
dt = parser.GetDataTable();
}
return dt;
}
|
...