Table of Contents | ||||
---|---|---|---|---|
|
Setting Up the Template
Info | ||
---|---|---|
| ||
In the downloadable TODO ADD FILE REFERENCE, there is a completed template file located in ComleteFinancialReport/templates/Part2_Financial_Template.xlsx. |
Getting Started
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.
Note |
---|
This example assumes an understanding of ExcelTemplate. If you are not familiar with how to set up an Excel template with data markers, please go through the Simple Expense Summary first. |
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 sum formulas, visible in the function bar below:
The sum formula is the standard Excel function. When the data is populated, the A1:A1 reference gets updated to include all the data. The result should be something like this (highlighting added for demonstration):
Referencing the Data Sheet
Number Formats
Setting Styles
Adding an ExcelWriter Reference in Visual Studio
Info | ||
---|---|---|
| ||
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.
- 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
Code Block |
---|
using SoftArtisans.OfficeWriter.ExcelWriter;
|
2. In the method that will run the report, instantiate the ExcelTemplate object.
Code Block |
---|
ExcelTemplate XLT = new ExcelTemplate();
|
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 | ||
---|---|---|
| ||
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, |
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.
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 Top and Details Sales 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:
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);
|
Downloads
TBA