Table of Contents |
---|
Introduction
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
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:
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.
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.
Number Formats
This section will cover how to add number formatting and the expected behavior for ExcelTemplate as it imports data.
1. Select the 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.
4. 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.
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.
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.
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 template should look like this:
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 bold.
1. On the "Home" tab in Excel, click on "Conditional Formatting"
2. Select "New Rule..."
3. In this tutorial the condition type is "Format only cells that contain..." The rule is "Cell value less than 0"
4. Click on "Format..." Set the text to be dark red. Set the typeface to be bold.
5. Click OK to save the rule.
Adding an ExcelWriter Reference in Visual Studio
Create a .NET project and add a reference to the ExcelWriter library.
- Open Visual Studio and create a .NET project.
- The sample code uses a web application.
- 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
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
You can download the code for the Financial Report here.