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 9 Next »

Table of Contents


Setting Up the Templates

Following the Sample Code

Icon

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

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

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

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.

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

A Note About Data Sheets

Icon

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.

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

Final Code

Downloads

TBA

  • No labels