Table of Contents |
---|
|
Setting Up the Templates
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.
Adding an ExcelWriter Reference in Visual Studio
Create a .NET project and add a reference to the ExcelWriter library.
- Open Visual Studio and create a .NET project.
- The sample code uses a web application.
- 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
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
3. Create a method to bind and process the two templates. This method takes the filename and returns the processed template object
4. In the BindTemplateData method, instantiate a new ExcelTemplate object to return.
5. Open the template files with the ExcelTemplate.Open
method.
6. 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.
Data Binding
1. In the BindTemplateData method, get the data for Assets, Losses, and Other.
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.
2. Create the datasets for the header row in template 1. This will simply be ignored by template 2.
3. Use ExcelTemplate.BindData
to bind the data for the Top and Details Sales data sets.
4. Use the ExcelTemplate.BindRowData
method to bind the header data to the data markers in the template file (i.e. %%=Header.FiscalYear
).
5. Call ExcelTemplate.Process() to import all data into the 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:
2. Open the populated template file with ExcelApplication. The file will open as a Workbook object.
3. Access the first Worksheet.
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.
5. Instantiate the ExcelApplication object for the Worksheet to be copied from:
6. Open the populated template file with ExcelApplication.
7. Open both the table sheet and the data sheet.
8. Call CopySheet with wbTo, and pass the worksheets. Make sure to copy the data sheet first
9. Finally, call ExcelApplication.Save to save the final file. This example streams the file using the page response.
Final Code
Downloads
TBA