Table of Contents |
---|
Setting Up the Template
Getting Started
In this tutorial ExcelTemplate is being used to populate data and ExcelApplication is being used to format the data. This part of the tutorial will make use of data marker modifiers.
Using Modifiers
Here is the starting template:
The next few steps will show the process of adding data marker modifiers.
ExcelTemplate supports the use of ordinal numbering based on data source column. Instead of binding to "%%=Assets.Q1" the data marker can bind to the second column in the Assets data set with the syntax "%%=Assets.#2." Note that the hash (#) indicates ordinal syntax. An example is below:
This template uses two different data marker modifiers - fieldname and optional. Modifiers are added in parentheses at the end of a data marker. They alter the binding behavior of the data marker.
The fieldname modifier shows the fieldname of the column being bound. The syntax is as follows:
The optional modifier allows the data marker to be ignored if there is no data corresponding with that column. This is just an ignore - the column itself will still exist, but the data marker will be skipped. Here is a usage example:
In the above, the "Division" column will be ignored if no division is present in the data.
After the modifiers are added, the template should resemble this:
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. Create the datasets for the header row. Recall the optional modifier for the "Division" tag. This tutorial will not bind any data for that tag to demonstrate the function.
3. Use ExcelTemplate.BindData
to bind the data for the Assets, Losses, and Other 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.
6. Call ExcelTemplate.Save() to save the final output
The final output should look something like this:
Final Code
Downloads
You can download the code for the Financial Report here.