An ExcelWriter template is a spreadsheet created in Microsoft Excel that contains data markers. A data marker is a cell value or sheet tab name beginning with %%= or %%=$ that specifies a database column, a variable, or an array to insert in the spreadsheet column or sheet tab containing the marker. Data markers are created in Microsoft Excel and bound to data sources in code. When you run the code, ExcelWriter populates the data markers with values from a data source.
Refer to the ExcelTemplate Code Samples to see live demonstrations of various ExcelTemplate applications. Click the "View template" link on each sample page to see the template and view the Data Markers.
Using data markers in tab sheet names was introduced in ExcelWriter 8.5.0. For more information, please refer to Using Data Markers in Sheet Tabs. A data marker binds in script to a data source which may be a variable, an array, or an ADO.NET DataSet, DataTable, or DataReader. A data marker may include modifiers. Data source and field numbers are 1-based. If ExcelWriter encounters %%=#0[.field] or %%=[DataSource.]#0, an error will occur. |
In ExcelWriter versions before 3.1, data source and field numbers were 0-based. If you upgraded from an earlier version, you may need to modify your code.
Implicitly or explicitly, a data marker always specifies both a data source and a column - even if the data source is a variable or a one-dimensional array. For example, in the data marker %%=Products.ProductID, "Products" is the data source, and "ProductID" is the column. The data marker %%=ProductID means the "ProductId" column from the first data source defined in the ExcelWriter code. Both the data source and the column can be specified by either name or number:
Data Marker NamesFollow these rules when naming data markers:
To include spaces or Unicode characters in the data source column name, use this format: %%=DataSource.[Column Name] Data Marker Formats
|
In ExcelWriter 4, a $ was required for all data markers that bind to 1-dimensional arrays. In ExcelWriter 5 and above, if a 1-dimensional array binds to a row of data markers by the ExcelTemplate.SetRowDataSource or ExcelTemplate.BindRowData methods, the $ should be omitted. However, for backward compatibility, if you include the $, the data marker will be populated without error.
Using Short Data MarkersExcelWriter supports several full-length and short data marker forms. When you use any of the long data marker formats, you explicitly include both the data source name or number and the column name or number. Short formats do not specify both data source and column explicitly: one or the other is omitted. How ExcelWriter understands a short data marker depends on whether or not the marker contains a $. ExcelWriter understands %%=$Employee as the first column of the data source "Employee." If you omit the $, ExcelWriter will read "Employee" as a column from the first data source defined in the ExcelWriter code. So:
Examples
Long Data MarkersWhen you use any of the long data marker formats, explicitly include both the data source name or number and the column name or number. Using the long formats may prevent confusion when writing the ExcelWriter code, since you must be able to distinguish between data source and column to set the data source in code. Examples
Grouping and Nesting Markers(Enterprise Edition only) In addition to data markers, ExcelTemplate uses Grouping and Nesting markers to format flat data as it is brought into a spreadsheet. The %%group, %%endgroup, %%header, and %%footer markers are used to signify the beginning and ending of the group formatting and the header and footer sections; their rows will not be included in the worksheet. The %%value(data marker) marker allows you to include a data marker value in the header or footer of a group, while the (hide) modifier, described below, allows you to remove a field from the data marker rows if its values are repeated in the header and footer rows. |
Grouping and Nesting Markers are only evaluated in Office Open XML (.xlsx) files. Grouping and Nesting Markers placed in BIFF8 (.xls) templates will not be evaluated.
Grouping and Nesting Marker Descriptions(Enterprise Edition only)
ExampleBelow is an example of a grouping block in a template spreadsheet.
For the data to be grouped correctly, they must be in a flat format and sorted hierarchically by grouping categories. For the grouping block above, for example, you could use the following data table:
The ExcelTemplate.BindData method should bring the data in as displayed below:
Data Marker ModifiersAn ExcelWriter data marker can include modifiers, included in parentheses at the end of the data marker, that change the behavior of ExcelTemplate. For example: ExcelWriter supports the following data marker modifiers:
|
||
Preserve |
The Preserve modifier forces Excel to always insert a string value as a string, instead of attempting to convert the value to a number. |
---|---|
Convert |
The Convert modifier forces Excel to always try converting a string to a number. If the conversion fails, the value will be inserted as a string. |
Continue |
The Continue modifier is used when the user wishes to bind data from a single data source into multiple worksheets. If the data source for the ExcelTemplate uses a forward-only cursor, such as with a DataReader, the second worksheet with the same data marker will automatically start from the row after the last row in the first worksheet. If the data source is scrollable, however, the second worksheet with the same data marker will start from the first row of the data source. The Continue modifier tells the ExcelTemplate object to start from the row after the last row in the previous worksheet when a scrollable data source is being used. |
Hide |
The Hide modifier removes the field from the spreadsheet after data is bound to the spreadsheet. This is usually used to allow you to remove field values if they are already expressed in a Group header or footer using the %%value() marker. Since the data are removed on the server side, they will not be evaluated in any formulas on the spreadsheet. |
The (hide) data marker modifier will only be evaluated in Office Open XML (.xlsx) files. (hide) data marker modifiers in BIFF8 (.xls) files will not be evaluated and may throw an exception. |