Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Csharp
2
2
ExcelTemplate XLT = new ExcelTemplate();
Vbnet
2
2

3. Open the template file from earlier with the ExcelTemplate.Open() method.

Csharp
3
3
XLT.Open(Page.MapPath("//templates//part1_template.xlsx"));
Vbnet
3
3

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.

Csharp
4
4
DataBindingProperties dataProps = XLT.CreateDataBindingProperties();
Vbnet
4
4

5. Create a object array for the header values and a string array for the column names.

ExcelTemplate can be bound to numerous types of .NET data structures: single variables, arrays (1-D, jagged, multi-dimensional), DataSet, DataTable, IDataReader etc. The source of the data can come from anywhere.

Some of the aforementioned structures have built in column names, such as the DataTable. When working with arrays, which don't have built in column names, you have to define the column names in a separate string array.

Csharp
5
5

//This report is for FiscalYear: FY 2004, Division: Canadian Division, Group: Research and Development
object[] valuesArray = { "FY 2004", "Canadian Division", "Research and Development" };

//The column names are FiscalYear, Division, Group
string[] columnNamesArray = { "FiscalYear", "Division", "Group" };
Vbnet
5
5

6. Use the ExcelTemplate.BindRow() method to bind the header data to the data markers in the template file (%%=Header.FiscalYear, %%=Header.Division, %%=Header.Group).

BindRowData() binds a single row of data to the template, but the data markers in the template do not need to be in a row.

Csharp
6
6

XLT.BindRowData(valuesArray, columnNamesArray, "Header", dataProps);
Vbnet
6
6

Info

If you want to import a row of data as a vertical column in Excel, you need to use ExcelTemplate.BindColumnData and the data marker syntax %%=$DataSourceName.ColumnName, with a $ to denote that the data should be imported as a column instead of a row.

7. Get the data for the Top 5 Expenses and All Expenses data sets.

In this case, we chose to parse CSV files that contained query results from the AdventureWorks2008 database. These calls are to a helper method GetCSVData that parses the CSV files and returns a DataTable with the values.

Csharp
7
7

DataTable dtTop5 = GetCSVData(Page.MapPath("//data//Part1_Top5Expenses.csv"));
DataTable dtAll = GetCSVData(Page.MapPath("//data//Part1_AllExpenses.csv"));
Vbnet
7
7

8. Use ExcelTemplate.BindData() to bind the data for the Top 5 Expenses and All Expenses data sets.

Recall that the data source names (/Top 5 Expenses/, /All Expenses/) need to match the data marker names exactly.

Csharp
8
8

XLT.BindData(dtTop5, "Top 5 Expenses", dataProps);
XLT.BindData(dtAll, "All Expenses", dataProps);
Vbnet
8
8

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

Csharp
9
9

XLT.Process();
Vbnet
9
9

10. Call ExcelTemplate.Save() to save the output file.

ExcelTemplate has several output options: save to disk, save to a stream, stream the output file in a page's Response inline or as an attachment.

Csharp
10
10

XLT.Save(Page.Response, "Part1_Output.xlsx", false);
Vbnet
10
10

11. Run your code.

Here is an example of the output from the sample code:

Image Added

Note: The formatting has been applied to the values that replaced the data markers, including the data sets with multiple rows. Also note that the Top 5 Expenses and All Expenses tables have expanded to accomodate the new rows of data (i.e. All Expenses was pushed down when the Top 5 Expenses data was imported).

Final Code

Csharp
101110
11
Add code here

Downloads

using SoftArtisans.OfficeWriter.ExcelWriter;
...
ExcelTemplate XLT = new ExcelTemplate();

XLT.Open(Page.MapPath("//templates//part1_template.xlsx"));

DataBindingProperties dataProps = XLT.CreateDataBindingProperties();

object[] valuesArray = { "FY 2004", "Canadian Division", "Research and Development" };
string[] columnNamesArray = { "FiscalYear", "Division", "Group" };

XLT.BindRowData(valuesArray, columnNamesArray, "Header", dataProps);

DataTable dtTop5 = GetCSVData(Page.MapPath("//data//Part1_Top5Expenses.csv"));
DataTable dtAll = GetCSVData(Page.MapPath("//data//Part1_AllExpenses.csv"));

XLT.BindData(dtTop5, "Top 5 Expenses", dataProps);
XLT.BindData(dtAll, "All Expenses", dataProps);

XLT.Process();

XLT.Save(Page.Response, "Part1_Output.xlsx", false);
Vbnet
11
11

Downloads

Next Steps