Forums     Knowledge Base     OfficeWriter Online     
 
 
This documentation is for
OfficeWriter v3.8.1
.NET Platform

View Docs for Another
Version or Platform

ExcelTemplate in Depth > Using an Array as a Data Source

Using an Array as a Data Source

The following sample generates an Excel spreadsheet from the template using arrays as the data source. In this example, the template contains several data markers that bind to array data sources in the ASP.NET code.

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
For a single column data marker that will be bound to a 1-dimensional array data source by the method SetColumnDataSource, 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 SetRowDataSource, use the data marker format %%=DataMarkerName.Field (without a $). For more information, see Creating Data Markers.

Note on Backward Compatibility
In ExcelWriter 4, a $ was required for all data markers that bind to 1-dimensional arrays. In ExcelWriter 5 and 6, if a 1-dimensional array binds to a row of data markers by the method SetRowDataSource, the $ should be omitted. However, for backward compatibility, if you include the $, the data marker will be populated without error.

In ArrayDataSource.aspx.cs, the SetColumnDataSource is called to set %%=$SimpleArray's data source to a one-dimensional array:

string[] onedim = {"SoftArtisans", "OfficeWriter", "ExcelTemplate"};
xlt.SetColumnDataSource(onedim, "SimpleArray");

SetColumnDataSource sets a data source for a template column to a 1-dimensional array of objects. The method's first parameter - onedim in the example - is the data source array. The second parameter - "SimpleArray" - is the name of the template data marker that the data source binds to.

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

string[][] twodim = { 
  new string[]{"Nancy","Michael","Adrian"}, 
  new string[]{"Davolio","Suyama","King"},
  new string[]{"Sales Manager","HR Representative","IS Support"} 
  };
string[] names = {"FirstName", "LastName", "Position"};
xlt.SetDataSource(twodim, names, "TwoDimArray");

SetDataSource'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 that the data source binds to. 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.

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

string[] addressvalues = {"3 Brook St.", "Watertown", "MA"};
string[] addressnames = {"Street", "City", "State"};
xlt.SetRowDataSource(addressvalues, addressnames, "Address");

The first parameter of SetRowDataSource - 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 last parameter - "Address" - is the name of the data marker to bind to.

Data MarkerCode
%%=$SimpleArray
string[] onedim = {"SoftArtisans", 
	"OfficeWriter", 
	"ExcelTemplate"};
xlt.SetColumnDataSource(onedim, "SimpleArray");
%%=TwoDimArray.#1
%%=TwoDimArray.#2
%%=TwoDimArray.#3
string[][] twodim = { 
	new string[]{"Nancy","Michael","Adrian"}, 
	new string[]{"Davolio","Suyama","King"},
	new string[]{"Sales Manager",
		"HR Representative",
		"IS Support"} };
string[] names = {"FirstName", "LastName", "Position"};
xlt.SetDataSource(twodim, names, "TwoDimArray");
%%=Address.Street
%%=Address.City
%%=Address.State
string[] addressvalues = {"3 Brook St.", 
	"Watertown", 
	"MA"};
string[] addressnames = {"Street", "City", "State"};
xlt.SetRowDataSource(addressvalues, 
	addressnames, 
	"Address");



Copyright 2007 © SoftArtisans, Inc. All Rights Reserved.