Page tree

Versions Compared

Key

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

Table of Contents

 

Table of Contents
maxLevel2

Setting Up the Templates

Info
titleFollowing the Sample Code

In the downloadable TODO ADD FILE REFERENCE, there is a completed template file located in ComleteFinancialReport/templates/Part2_Financial_Template.xlsx.
A copy of the completed template file is also available TODO ADD FILE REFERENCE.

Getting Started

...

 

Introduction

Note

This is Part 3 of the three-part tutorial series Financial Report scenario. It is recommended that you complete Part 1 - Using Modifiers and Ordinal Syntax and Part 2 - Using Styles and Formatting before starting this section.

This tutorial also assumes a basic understanding of how to open and manipulate Excel files with ExcelApplication.

Getting Started

This section covers using ExcelApplication to join two Worksheets into one Workbook. These two worksheets were created in Part 1 and Part 2 of this tutorial. 

This example assumes an understanding of ExcelTemplate. If you are not familiar with how to set up an Excel template with data markers, please go through the Simple Expense Summary first
Note
Info
titleFollowing the Sample Code

In the downloadable ExcelWriter_Basic_Tutorials.zip, there are completed template files located in CompleteFinancialReport/templates.

Adding an ExcelWriter Reference in Visual Studio

...

  1. Open Visual Studio and create a .NET project.
    • The sample code uses a web application.
  2. Add a reference to SoftArtisans.OfficeWriter.ExcelWriter.dll
    • SoftArtisans.OfficeWriter.ExcelWriter.dll is located under Program Files > SoftArtisans > OfficeWriter > dotnet > bin

Writing the Code

Merging template together with CopySheet

1. Include the SoftArtisans.OfficeWriter.ExcelWriter namespace in the code behind

Code Block

using SoftArtisans.OfficeWriter.ExcelWriter;

2. In the Globally declare the ExcelTemplate, ExcelApplication, and Workbook objects.

Code Block
ExcelApplication XLA;
ExcelTemplate XLT;
Workbook MERGEDWB;

3. Define a method that will run the report, instantiate the ExcelTemplate open the two template files and merge them together with CopySheet. In this case, the method is called MergeTemplates().

Code Block
protected void MergeTemplates()
{

}

4. In MergeTemplates(), instantiate the ExcelApplication object.

Code Block
ExcelTemplateXLA XLT = new ExcelTemplateExcelApplication();

35. Open the template file with the ExcelTemplate.Open method.

...

workbook that the worksheets will be copied into. For this example, the second template will be copied into the first template workbook.

Code Block
MERGEDWB = XLA.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx"));

6. Open the second workbook that will be merged into the first.

Code Block
Workbook wbOther = XLA.Open(Page.MapPath("//templates//Part2_Financial_Template.xlsx"));

4. Create a DataBindingProperties object. None of the binding properties will be changed for this tutorial, but DataBindingProperties is a required parameter in ExcelTemplate data binding methods.

Code Block

DataBindingProperties dataProps = XLT.CreateDataBindingProperties();

Data Binding

1.Get the data for the Assets, Losses, and Other datasets

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 data directory. There is a copy of the CSV parser, GenericParsing.dll in the bin directory of the project GetCSVData is defined in Part2.aspx.cs in a region marked Utility Methods.

These calls are to a helper method GetCSVData that parses the CSV files and returns a DataTable with the values.

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.

...

7. Copy the worksheet from the second template into the first workbook. Worksheet.Name can be used to pass along the original name of the worksheet ("Percent by Quarter"). The worksheet should be copied to the end of the current worksheet collection (index 1).

Code Block
MERGEDWB.Worksheets.CopySheet(wbOther[0], 1, wbOther[0].Name);

Binding Data to the Merged Template

1. Create a method to handle binding and processing the data import for the combined template. For this example, the method is called BindDataToMergedTemplate().

Code Block
protected void BindDataToMergedTemplate()
{

}

2. Instantiate ExcelTemplate and open the template file with ExcelTemplate.Open(ExcelApplication, Workbook) to open directly from the pre-existing ExcelApplication instance.

Code Block
XLT = new ExcelTemplate();
XLT.Open(XLA, MERGEDWB);

3. Use the code from Part 1 and Part 2 to bind data to the template. Note that both templates used the same data set, so the calls to bind the data only need to be made once.

Info

If there are duplicate data markers in a workbook, ExcelWriter will import the matching data to all the data markers, across all the worksheets in the workbook.

Code Block
//Create data binding properties for ExcelTemplate
DataBindingProperties bindingProps = XLT.CreateDataBindingProperties();

//Create the array of header values. This example only binds a single item
string[] headerValues = { "2011" };

//Create the array of header names.
string[] headerNames = { "FiscalYear" };

//Get the datatables to be bound into the template.
//This example uses CSV data, but the data can come from
//anywhere
DataTable dtAssets = GetCSVData("//data//Assets.csv");
DataTable dtLosses = GetCSVData("//data//Losses.csv");
DataTable dtOther = GetCSVData("//data//Other.csv");

2. Use ExcelTemplate.BindData to bind the data for the Top and Details Sales data sets.

Code Block


//Bind the header row data
XLT.BindRowData(headerValues, headerNames, "Header", bindingProps);

//Bind the datatables
XLT.BindData(dtAssets, "Assets", bindingProps);
XLT.BindData(dtLosses, "Losses", bindingProps);

XLT.BindData(dtOther, "Other", bindingProps);

3. Call ExcelTemplate.Process() to import all data into the file.

Code Block

//Process to import the data to the file
XLT.Process();

4. Call ExcelTemplate.Save() to save the final file.

Code Block
XLT.Save(Page.Response, "tempPart3_Output.xlsx", false);

Final Code

Code Block

using SoftArtisans.OfficeWriter.ExcelWriter;
using GenericParsing; //See Part 1 for more information about the CSV parser
...
//Declare globals
 ExcelApplication XLA;
 ExcelTemplate XLT;
 Workbook MERGEDWB;

//InstantiateMerges the templatereports objectand ExcelTemplatebinds XLT data to the templates
protected void RunReport()
{
          //Join the two reports using CopySheet
          MergeTemplates();

          //bind the data for the newly modified file
          BindDataToMergedTemplate();

}


protected void JoinReports()
{
	//Instantiate ExcelApplication
	XLA = new ExcelTemplateExcelApplication();

	//Open the file
XLTfirst template
	MERGEDWB = XLA.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx"));

	//Open the second template
	Workbook wbOther = XLA.Open(Page.MapPath("//templates//Part2_Financial_Template.xlsx"));

	//Call CopySheet
        MERGEDWB.Worksheets.CopySheet(wbOther[0], 1, wbOther[0].Name);

}

protected void BindTemplateData()
{
	XLT = new ExcelTemplate();

	//Open the template file
	XLT.Open(XLA, MERGEDWB);

	//Create data binding properties for ExcelTemplate
	DataBindingProperties bindingProps = XLT.CreateDataBindingProperties(); 


	//Get the datadatatables fromto thebe CSVs.bound Moreinto infothe about the generic parser is available
//intemplate.
	//This example uses CSV data, but the projectdata andcan income the tutorial above.
from
	//anywhere
	DataTable dtAssets = GetCSVData("//data//Assets.csv");
	DataTable dtLosses = GetCSVData("//data//Losses.csv");
	DataTable dtOther = GetCSVData("//data//Other.csv");

	//Bind eachthe datatabledatatables
	XLT.BindData(dtAssets, "Assets", bindingProps);
	XLT.BindData(dtLosses, "Losses", bindingProps);
	XLT.BindData(dtOther, "Other", bindingProps);

	//Call process *This section only applies to the first template. It will be ignored by the 
	second template, since there are no markers to bind to.*/

	//Create the array of header values. This example only binds a single item
	string[] headerValues = { "2011" };

	//Create the array of header names.
	string[] headerNames = { "FiscalYear" };

	//Bind the header row data
	XLT.BindRowData(headerValues, headerNames, "Header", bindingProps);

	//end

	//Process to import the data to the file
	XLT.Process();

//Call save       XLT.Save(Page.Response, "tempPart3_Output.xlsx", false);
}

Downloads

TBA

Next Steps

Continue to Part 3: Combine Reports with CopySheet

You can download the code for the Financial Report here.