Table of Contents |
---|
|
Introduction
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 and ExcelApplication objects. We are also using a MemoryStream to pass the application object to the template object.
The next step uses CopySheet to create a JoinReports method.
Joining Reports
1. Instantiate ExcelApplication
2. Open the workbook to copy into.
3. Open the workbook to copy from.
4. Open the worksheets to copy from. These are the first sheet with a table, and the second sheet containing the data.
5. Call CopySheet for each worksheet. The data sheet needs to be copied first so that the references to it remain valid.
6. Instantiate the MemoryStream. ExcelTemplate opens ExcelApplication objects as streams
7. Finally save the joined file to a stream.
Data Binding
1. 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 the ExcelTemplate object
5. Open the template files with the ExcelTemplate.Open
method. This opens the stream created in the JoinReports 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.
7. 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.
8. Create the datasets for the header row in template 1. This will simply be ignored by template 2.
9. Use ExcelTemplate.BindData
to bind the data for the Top and Details Sales data sets.
10. Use the ExcelTemplate.BindRowData
method to bind the header data to the data markers in the template file (i.e. %%=Header.FiscalYear
).
11. Call ExcelTemplate.Process() to import all data into the file.
Do not save the file. In the next step the XLT object will be post processed by ExcelApplication
Post-Processing
1. The file 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. Finally, call ExcelApplication.Save to save the final file. This example streams the file using the page response.
Final Code
Downloads
You can download the code for the Financial Report here.