Page tree

Versions Compared

Key

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

...

Info
titleFollowing the Sample Code

In the downloadable TODO ADD FILE REFERENCE, there are completed template files located in CompleteFinancialReport/templates.
A copy of the completed template files are also available TODO ADD FILE REFERENCE.

Getting Started

In this tutorial ExcelApplication is being used to 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.

Note

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.

 

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

Using Multiple Instances of ExcelTemplate

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

Code Block
using SoftArtisans.OfficeWriter.ExcelWriter;

2. Globally declare the ExcelTemplate objects. There are two instances - one for the Workbook being copied to and one for the Workbook being copied from

Code Block
ExcelTemplate XLTto;
ExcelTemplate XLTfrom;

3. Create a method to bind and process the two templates. This method takes the filename and returns the processed template object

Code Block

private ExcelTemplate BindTemplateData(string filename)
{
}

4. In the BindTemplateData method that will run the report, instantiate the ExcelTemplate objecta new  ExcelTemplate object to return.

Code Block

ExcelTemplate XLT = new ExcelTemplate();

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

Code Block
XLT.Open(Page.MapPath("//templates//Part2_Financial_Template.xlsx"filename));

46. 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 In the BindTemplateData method, 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 Part2Part1.aspx.cs in a region marked Utility Methods.

...

Code Block
DataTable dtAssets = GetCSVData("//data//Assets.csv");
DataTable dtLosses = GetCSVData("//data//Losses.csv");
DataTable dtOther = GetCSVData("//data//Other.csv");

2. Create the datasets for the header row in template 1. This will simply be ignored by template 2.

Code Block

//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" };

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

Code Block
XLT.BindData(dtAssets, "Assets", bindingProps);
XLT.BindData(dtLosses, "Losses", bindingProps);

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

3. 4. Use the ExcelTemplate.BindRowData method to bind the header data to the data markers in the template file (i.e. %%=Header.FiscalYear).

Code Block

XLT.BindRowData(headerValues, headerNames, "Header", bindingProps);

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

Code Block
XLT.Process();

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

...

Do not save the file. In the next step the XLT object will be processed by ExcelApplication

Post-Processing

1. The first template has to be post-processed with autofit. This is also the workbook that is being copied to. Instantiate the ExcelApplication object:

Code Block

ExcelApplication XLATo = new ExcelApplication();

2. Open the populated template file with ExcelApplication. The file will open as a Workbook object.

Code Block

Workbook wbTo = XLATo.Open(XLTTo);

3. Access the first Worksheet.

Code Block

Worksheet wsTo = wbTo.Worksheets[0];

4. Call Area.AutoFitHeight() and Area.AutoFitWidth() to set the column and row height. AutoFitWidth sets the column width to fit the widest populated cell in the column. AutoFitHeight sets the row height to highest populated cell in the row. In this snippet, the area is Worksheet.PopulatedCells, which returns an area containing all populated cells.

Code Block

wsTo.PopulatedCells.AutoFitWidth();
wsTo.PopulatedCells.AutoFitWidth();

5. Instantiate the ExcelApplication object for the Worksheet to be copied from:

Code Block

ExcelApplication XLAFrom = new ExcelApplication();

6. Open the populated template file with ExcelApplication.

Code Block

Workbook wbFrom = XLAFrom.Open(XLTFrom);

7. Open both the table sheet and the data sheet.

Code Block

Worksheet wsFrom = wbFrom[0];
Worksheet wsFromData = wbFrom[1];
Worksheet wsFrom = wbFrom[0];            Worksheet wsFromData = wbFrom[1]

8. Call CopySheet with wbTo, and pass the worksheets. Make sure to copy the data sheet first

Note
titleA Note About Data Sheets

Always copy the data sheet first. The table sheet references the data sheet. If you copy the data sheet second, the references break and an error is thrown.

Code Block

wbTo.Worksheets.CopySheet(wsFromData, 1, "Data");
wbTo.Worksheets.CopySheet(wsFrom, 1, "Percent by Quarter");

9. Finally, call ExcelApplication.Save to save the final file. This example streams the file using the page response.

Code Block

XLA.Save(wbTo, Page.Response, "temp.xlsx", false);

...

Code Block
using SoftArtisans.OfficeWriter.ExcelWriter;
...
//Declare TBA
Template objects globaly
ExcelTemplate XLTTo;
ExcelTemplate XLTFrom;

protected void RunReport()
{
	//Get the populated files from BindTemplateData
        XLTTo = BindTemplateData("//templates//Part1_Financial_Template.xlsx");
        XLTFrom = BindTemplateData("//templates//Part2_Financial_Template.xlsx");

	//Join the two reports using CopySheet
	JoinReports();
}

protected ExcelTemplate BindTemplateData(string filename)
{
	ExcelTemplate XLT = new ExcelTemplate();


	//Open the template file
	XLT.Open(Page.MapPath(filename));

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

	//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");

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

	/*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();

	//Return the populated file
	return XLT;
}

protected void JoinReports()
{
	//Instantiate ExcelApplication for the workbook being copied to
	ExcelApplication XLATo = new ExcelApplication();

	//Open the populated template
	Workbook wbTo = XLATo.Open(XLTTo);

	//Open the first worksheet
	Worksheet wsTo = wbTo.Worksheets[0];

	//Call AutoFit to set the height and width
	wsTo.PopulatedCells.AutoFitWidth();
	wsTo.PopulatedCells.AutoFitHeight();

	//Instantiate ExcelApplication for the workbook being copied from
	ExcelApplication XLAFrom = new ExcelApplication();

	//Open the populated template
	Workbook wbFrom = XLAFrom.Open(XLTFrom);

	//Open the first worksheet, with the table
	Worksheet wsFrom = wbFrom[0];
	//Open the second worksheet, with the data
	Worksheet wsFromData = wbFrom[1];

	//Call CopySheet, making sure to copy the data sheet first
	wbTo.Worksheets.CopySheet(wsFromData, 1, "Data");
	wbTo.Worksheets.CopySheet(wsFrom, 1, "Percent by Quarter");

	//Save the file
	XLATo.Save(wbTo, Page.Response, "Part3_Output.xlsx", false);
}


Downloads

TBA