Table of Contents |
---|
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.
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. |
The template should resemble this upon completion:
This template uses two different data marker modifiers - fieldname and optional.
The fieldname modifier shows the fieldname of the column being bound. It will not bind any additional data. It is used like this:
The optional modifier allows that data marker to be ignored on data binding. If you have a column that may be empty, the optional modifier allows you to bind the data set regardless. It is used like this:
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.
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. Include the SoftArtisans.OfficeWriter.ExcelWriter namespace in the code behind
using SoftArtisans.OfficeWriter.ExcelWriter; |
2. In the method that will run the report, instantiate the ExcelTemplate object.
ExcelTemplate XLT = new ExcelTemplate(); |
3. Open the template file with the ExcelTemplate.Open
method.
XLT.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx")); |
4. Create a DataBindingProperties
object. Although we won't be changing any of the binding properties, a DataBindingProperties
is a required parameter in all ExcelTemplate
data binding methods.
DataBindingProperties dataProps = XLT.CreateDataBindingProperties(); |
1.Get the data for the Assets, Losses, and Other datasets
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:
GenericParsing.dll
GeneringParsing
at the top of your code.GetCSVData
method that can be found in the sample code.DataTable dtAssets = GetCSVData("//data//Assets.csv"); DataTable dtLosses = GetCSVData("//data//Losses.csv"); DataTable dtOther = GetCSVData("//data//Other.csv"); |
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.
//Create the array of header values. This example only binds a single item string[] headerValues = { "2011" }; //Create the array of header names. string[] headerNames = { "FiscalYear" }; |
3. Use ExcelTemplate.BindData
to bind the data for the Top and Details Sales data sets.
XLT.BindData(dtAssets, "Assets", bindingProps); XLT.BindData(dtLosses, "Losses", bindingProps); XLT.BindData(dtOther, "Other", bindingProps); |
4. Use the ExcelTemplate.BindRowData
method to bind the header data to the data markers in the template file (i.e. %%=Header.FiscalYear
).
XLT.BindRowData(headerValues, headerNames, "Header", bindingProps); |
5. Call ExcelTemplate.Process() to import all data into the file.
XLT.Process(); |
We are not saving the file, since we will be using ExcelApplication to access the file and post-process.
Without the post processing, the populated file will persist the column width and heights. It should look something like this:
1. In the post-processing method, instantiate the ExcelApplication object:
ExcelApplication XLA = new ExcelApplication(); |
2. Open the populated file via the ExcelTemplate object. The file will open as a Workbook object
Workbook wb = XLA.Open(XLT); |
3. Access the first Worksheet.
Worksheet ws = wb.Worksheets[0]; |
4. Call Area.AutoFitHeight() and Area.AutoFitWidth() to set the column and row height correctly. In this snippet, the area is Worksheet.PopulatedCells, which returns an area containing all populated cells.
ws.PopulatedCells.AutoFitWidth(); ws.PopulatedCells.AutoFitWidth(); |
5. Finally call ExcelApplication.Save to save the final file. This example streams the file using the page response.
XLA.Save(wb, Page.Response, "temp.xlsx", false); |
The final output should look something like this
ExcelTemplate XLT = new ExcelTemplate(); XLT.Open(Page.MapPath("//templates//Part1_Financial_Template.xlsx")); DataBindingProperties bindingProps = XLT.CreateDataBindingProperties(); DataTable dtAssets = GetCSVData("//data//Assets.csv"); DataTable dtLosses = GetCSVData("//data//Losses.csv"); DataTable dtOther = GetCSVData("//data//Other.csv"); string[] headerValues = { "2011" }; string[] headerNames = { "FiscalYear" }; XLT.BindData(dtAssets, "Assets", bindingProps); XLT.BindData(dtLosses, "Losses", bindingProps); XLT.BindData(dtOther, "Other", bindingProps); XLT.BindRowData(headerValues, headerNames, "Header", bindingProps); XLT.Process(); //Post-Processing ExcelApplication XLA = new ExcelApplication(); Workbook wb = XLA.Open(XLT); Worksheet ws = wb.Worksheets[0]; ws.PopulatedCells.AutoFitWidth(); ws.PopulatedCells.AutoFitHeight(); XLA.Save(wb, Page.Response, "temp.xlsx", false); |
TBA
Continue to Part 2: Sub-Report with Number Formats