Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note

This is Part 2 of the three-part tutorial series Financial Report scenario. It is recommended that you complete Part 1 - Using Modifiers and Ordinal Syntax before starting this section.

This tutorial also assumes a basic understanding of how ExcelTemplate imports data how that affects formulas and formatting.

Info
titleFollowing the Sample Code

In the downloadable ExcelWriter_Basic_Tutorials.zip, there is a completed template file located in CompleteFinancialReport/templates/Part2_Financial_Template.xlsx.

In this tutorial ExcelTemplate is being used to populate data in a template set up with styles and formats. This part of the tutorial uses formulas and persists Excel styles.

Setting Up the Template

Data Sheets

This sub-report makes use of a data sheet. This is where the data markers will go. It should look something like this: Image Removed

The alternating rows (highlighting added for demonstration) contain the SUM Excel formula. The formula is visible in the function bar below: Image Removed

The sum formula is a standard Excel function, which gets updated when ExcelWriter populates the file with data. When the data is populated, the A1:A1 reference gets updated to include all the rows of data. The result should be something like this (highlighting added for demonstration):

Image Removed

Referencing the Data Sheet

The next step is adding references to the data sheet. This example references the "SUM" formulas on the data sheet. These sums are added to a percent change equation. This will result in a template resembling the following: Image Removed
Note the formula in the formula bar, "Sheet2" is the data sheet.

Info
titleUsing Data Sheets

In this example, the sum rows alternate on the data sheet. If you stretch a formula, you'll have to update the references to skip every other row.

The formula we are using shows percent change. It is set up like this:  The goal of this part of the tutorial is how to apply number formats and conditional formatting to ExcelTemplate files and what you can expect as ExcelTemplate imports data.

The final template will look like this:

Image Added

The bottom of the worksheet has a section for the data that will be imported. Each section has a total row summing all of the values for the section. This formula will expand as ExcelTemplate imports data.

Image Added

At the top is a grid that calculates the percent in change between each quarter, based on the asset, loss, and other totals. The formulas are set up as "=(Sheet2!B2-Sheet2!A2)/(Sheet2!A2)" for each cell in the grid.

Image Added

Number Formats

The value returned by the percent change equation should be displayed as a percentage. The table cells all have to be formatted.This section will cover how to add number formatting and the expected behavior for ExcelTemplate as it imports data.

1. Select all cellsthe 9 cells in the percentage grid at the top of the worksheet (C4:E6).

2. Right click and select "Format Cells..."

3. Select "Percentage" on the Number tab.

Image Removed

Setting Styles

The next step is setting up the table. Add a header and label the rows and columns to end up with a complete table: Image Removed
Once the table is complete, add styles.

1.Select "Percent Change by Quarter" and set it to "Heading 1." Heading 1 is found in the named styles list, on the "Home" tab of the ribbon.

2. Select the header row (B3:E3) and set it to "Total" in the named styles menu.

3. Select the row labels (B4:B6) and set that to "Total."

You should now have this: 

Image Removed

4. Select the area of B3:E6, and apply a consistent border:
Image Removed

5. Now select B2:E2 and apply a bottom border in dark blue.

The template should look like this:
Image Removed

Conditional Formatting

ExcelTemplate will persist conditional formatting in a template. In this tutorial, conditional formatting is applied to the "Other" table. It sets negative numbers to be red and bold4. Click OK

5. Select cells C12:F12, C17:F17, and C22:D22.

6. In the top tool bar, under the 'Number' tab, click the "$" to apply the currency format.

Image Added

So far the number formats have been applied to cells that only contain formulas, but number formats also work with cells that contain data markers.

1. Select cells C11:F11, C16:F16, C21:F21.
2. At the top bar, click the "$" to apply the currency format.

Image Added

As ExcelTemplate populates the worksheet, the number format will be copied to all the new rows of data that are inserted where the data markers are.

Conditional Formatting

ExcelTemplate will preserve existing conditional formatting. If the conditional format includes a formula, that formula will also be updated as new rows are inserted into the file.

In this case, the conditional formatting will be applied to the 'percent table' and it format negative numbers to be bold and red.

1. On the "Home" tab in Excel, click on "Conditional Formatting"

...

5. Click OK to save the rule. Image Removed

Image Added

Adding an ExcelWriter Reference in Visual Studio

...

1. Include the SoftArtisans.OfficeWriter.ExcelWriter namespace in the code behind

Code Block

using SoftArtisans.OfficeWriter.ExcelWriter;

...

3. Open the template file with the ExcelTemplate.Open method.

Code Block

XLT.Open(Page.MapPath("//templates//Part2_Financial_Template.xlsx"));

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

Code Block

DataBindingProperties dataProps = XLT.CreateDataBindingProperties();

Data Binding

1.Get the data for the Assets, Losses, and Other datasets

Info
titleFollowing the Sample

In the sample projectSimilarly to part 1, 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 Part2.aspx.cs in a region marked Utility Methods.using CSV that contain values for the data import. Please see Part 1 for more information on using the CSV files and the CSV parser.

1.Get the data for the Assets, Losses, and Other datasets
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:

...

Code Block

...

...

Code Block

DataTable dtAssets = GetCSVData("//data//Assets.csv");
DataTable dtLosses = GetCSVData("//data//Losses.csv");
DataTable dtOther = GetCSVData("//data//Other.csv");

2. Use ExcelTemplate.BindData to bind the data for the Assets, Losses, and Other data sets.

Code Block

XLT.BindData(dtAssets, "Assets", bindingProps);
XLT.BindData(dtLosses, "Losses", bindingProps);
XLT.BindData(dtOther, "Other", bindingProps);

3. Call ExcelTemplate.Process() to import all data into the file.

Code Block

XLT.Process();

4. Call ExcelTemplate.Save() to save the final file.

Code Block

XLT.Save(Page.Response, "temp.xlsx", false);

The final output should look something like this: 

Image RemovedImage Added

Final Code

Code Block

using SoftArtisans.OfficeWriter.ExcelWriter;
...


//Instantiate the template object
ExcelTemplate XLT = new ExcelTemplate();
//Open the file
XLT.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx"));


//Create data binding properties
DataBindingProperties bindingProps = XLT.CreateDataBindingProperties();


//Get the data from the CSVs. More info about the generic parser is available
//in the project and in the tutorial above.
DataTable dtAssets = GetCSVData("//data//Assets.csv");
DataTable dtLosses = GetCSVData("//data//Losses.csv");
DataTable dtOther = GetCSVData("//data//Other.csv");

//Bind each datatable
XLT.BindData(dtAssets, "Assets", bindingProps);
XLT.BindData(dtLosses, "Losses", bindingProps);
XLT.BindData(dtOther, "Other", bindingProps);

//Call process to import data to file
XLT.Process();

//Call save
XLT.Save(Page.Response, "temp.xlsx", false);

...

You can download the code for the Financial Report here.

Next Steps

Continue to Part 3: Combine Reports with CopySheet