Skip to end of metadata
Go to start of metadata
The following sample generates an Excel spreadsheet from a template using arrays as data sources.

Code sample: Using an Array as the Data Source

[C#] [VB.NET]

The first data marker in the template is *%%=$SimpleArray*. *%%=$* indicates that the data source is either a simple variable or a 1-dimensional array that is a data source for a single column.

Data Marker Format for 1-D Arrays

Icon

For a single column data marker that will be bound to a 1-dimensional array data source by the method BindColumnData, use the data marker format *%%=$DataMarkerName*. For a set of data markers with multiple fields that will be bound to a 1-dimensional array data source by the method BindRowData, use the data marker format *%%=DataMarkerName.Field* (without a *$*). For more information, see Creating Data Markers.

In ArrayDataSource.aspx.cs, the BindColumnData method is called to set the data source for *%%=$SimpleArray* to a one-dimensional array:

ExcelTemplate.BindColumnData sets a data source for a template column to a 1-dimensional array of objects. The method's first parameter, onedim, is the data source array. The second parameter, "SimpleArray", is the name of the template data marker to which the data source binds. The third parameter, xlt.CreateDataBindingProperties(), is a DataBindingProperties object that contains values that change how the data are bound to the template; in this case, the default property values are used.

 

The method BindData binds the data markers *%%=TwoDimArray.#1*, *%%=TwoDimArray.#2*, and *%%=TwoDimArray.#3* to a 2-dimensional string array:

BindData's first parameter is the 2-dimensional array of strings to use as the data source. The second parameter specifies an array of column names.

The third parameter - "TwoDimArray" - is the name of the template data marker to which the data source binds. The fourth parameter - xlt.CreateDataBindingProperties() - is a DataBindingProperties object that contains values that change how the data are bound to the template; in this case, the default property values are used.

There are three "TwoDimArray" data markers; their fields are specified by ordinal (#1, #2, and #3). The columns of values in the data source bind to the data markers by order. That is, the first column of values will populate *%%=TwoDimArray.#1*, the second will populate *%%=TwoDimArray.#2*, and the third *%%=TwoDimArray.#3*.

BindRowData sets a data source for the row of data markers *%%=Address.Street*, *%%=Address.City*, and *%%=Address.State* to a 1-dimensional array:

 

The first parameter of BindRowData, addressvalues, specifies the array of values to bind to the data markers. The second parameter, addressnames, is the array of data marker field names. The values in the data source array will bind to the data marker fields by field name. The third parameter, "Address", is the name of the data marker to which ExcelTemplate binds the data. The fourth parameter, xlt.CreateDataBindingProperties(), is a DataBindingProperties object that contains values that change how the data are bound to the template; in this case, the default property values are used.

Data MarkerCode

%%=$SimpleArray

%%=TwoDimArray.#1

%%=TwoDimArray.#2

%%=TwoDimArray.#3


%%=Address.Street

 

 

  • No labels