Setting up the Template

About templates and data markers

An ExcelTemplate template is an Excel file that contains ExcelWriter data markers. A datamarker is a cell value beginning with %%= that specifies a database colum, variable, or array to insert into the spreadsheet column. Data markers are added to a worksheet in Excel and then bound to data sources in code. ExcelWriter populates the data markers with values from the data sources when the code is executed.

ExcelWriter templates do not necessarily need to be in the Excel template file format (XLT, XLTX, XLTM), although these formats are supported by ExcelWriter.

Datamarker syntax

The basic syntax for a datamarker is %%=DataSourceName.ColumnName, where DataSourceName is the name of the data source and ColumnName is the name of the column of data in the data source. You need to follow these rules when naming data markers:

Adding data markers to the the template

The final template will look something like this:

In the sample code, the completed template file is located in SimpleExpenseSummary/templates/

1. Start with a blank xlsx file. Save the file as template.xlsx.

2. Add some text placeholders for data markers and table headers. In the header of the worksheet, we will display the fiscal year, the company division and group. Below will be 2 tables: one to show the top 5 expenses and another to show all the expenses.

In the screen shot, Fiscal Year, Division, and Group will replaced by data markers. Top Expenses, All Expenses, Description, and Expenses are going to become table headers. The data markers for that data will go in the rows below.

3. Replace Fiscal Year, Division, and Group with data markers. These values are going to be a single row of a data set called "Header". The column names will be "FiscalYear", "Division" and "Group".

4. Next add data markers for Top Expenses and All Expenses:

We're done adding the data markers, so next we'll add some styles and formatting to the data markers.

Formatting cells with data markers

Data markers take the formatting and style properties of the cell that they are in. This means if a data marker is bold, then the value that replaces the data marker will be bold as well.

5. In the screen shot we have made the %%=Header.FiscalYear cell font size 18, %%=Header.Division is bold, and %%=Header.Group is italic.

When importing multiple rows of data, ExcelWriter will insert a new row in the worksheet for each row of data, starting from the row with the data markers. Each of the new rows will take on the styles and formatting of the cells that contain the data markers.

6. Since 'Expenses' will be currency values, add a currency number formatting to the cells containing the Expenses data markers. This number formatting will be repeated for row of data that is inserted.

7. Add some borders to the cells in the Top Expenses and All Expenses tables. Then format the table headers as desired. Below is a screen shot of the final image:

We're done creating the template. Now it's time to write the code.

Writing the Code

SOMETHING ABOUT BINDROWDATA and BINDCOLUMN DATA

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.

Final Code

Add code here

Add code here

Downloads