Skip to end of metadata
Go to start of metadata

Table of Contents

 

 

Introduction

Icon

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. 

Following the Sample Code

Icon

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

Adding an ExcelWriter Reference in Visual Studio

Following the Sample Code

Icon

In the sample code, the reference to SoftArtisans.OfficeWriter.ExcelWriter.dll has already been added to the CompleteFinancialReport project.

Create a .NET project and add a reference to the ExcelWriter library.

  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

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

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

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

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

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

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).

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().

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

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.

Icon

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.

Final Code

Downloads

You can download the code for the Financial Report here.

  • No labels