Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Next »

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

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.

Following the Sample

Icon

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

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.

  • No labels