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

Table of Contents

Introduction

Icon

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

Following the Sample Code

Icon

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:

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

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

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:
Note the formula in the formula bar, "Sheet2" is the data sheet.

Also, the error message is because there currently isn't any data, so Excel is throwing a 'Divide by 0' error. This will not be in the output file because the file will contain data.

Using Data Sheets

Icon

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.

1. Select C4 on Sheet1.

2. Start to type the formula by entering =(

3. Click over to Sheet2, Row 2 and select the second SUM cell. (B2 in this example)

4. Go back to Sheet1 and add a minus sign for =(Sheet2!B2-

5. Click over to Sheet2, Row 2 again and select the first SUM cell.

6. Continue selecting cells into formulas to end up with this:   =(Sheet2!B2-Sheet2!A2)/(Sheet2!A2)

7. Drag this formula Horizontally only because of the alternating rows.

8. Repeat with rows 4 and 6

Number Formats

The value returned by the percent change equation should be displayed as a percentage. The table cells all have to be formatted.

1. Select all cells

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

3. Select "Percentage" on the Number tab.

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

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

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

The final template should look like this:

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

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

2.  In the method that will run the report, instantiate the ExcelTemplate object.

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

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.

Data Binding

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

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 Part2.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. Use ExcelTemplate.BindData to bind the data for the Assets, Losses, and Other data sets.

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

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

The final output should look something like this: 

Final Code

Downloads

TBA

Next Steps

Continue to Part 3: Combine Reports with CopySheet

  • No labels