Back to Devexpress

ExcelDataSource.Schema Property

corelibraries-devexpress-dot-dataaccess-dot-excel-dot-exceldatasource-ad95b788.md

latest5.6 KB
Original Source

ExcelDataSource.Schema Property

Gets the ExcelDataSource schema.

Namespace : DevExpress.DataAccess.Excel

Assembly : DevExpress.DataAccess.v25.2.dll

NuGet Package : DevExpress.DataAccess

Declaration

csharp
[Browsable(false)]
public FieldInfoList Schema { get; }
vb
<Browsable(False)>
Public ReadOnly Property Schema As FieldInfoList

Property Value

TypeDescription
FieldInfoList

A FieldInfoList object that is the ExcelDataSource schema.

|

Remarks

The ExcelDataSource allows you to specify its schema using the Schema property that provides access to a collection of FieldInfo objects.

The FieldInfo class exposes the FieldInfo.Name and FieldInfo.Type properties allowing you to specify the field name and type, respectively.

The FieldInfo.Selected property specifies whether or not the field will be displayed within the field list.

Note

After customizing the Excel data source schema at runtime, call the ExcelDataSource.RebuildResultSchema method to apply the changes.

Example

This example shows how to customize the schema of the ExcelDataSource using the ExcelDataSource.Schema property. To do this, perform the following steps.

csharp
// Creates an Excel data source and selects the specific cell range from the SalesPerson worksheet.
ExcelDataSource excelDataSource = new ExcelDataSource();
excelDataSource.Name = "Excel Data Source";
excelDataSource.FileName = HostingEnvironment.MapPath(@"~/App_Data/ExcelDataSource.xlsx");
ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings("SalesPerson", "A1:L2000");
excelDataSource.SourceOptions = new ExcelSourceOptions(worksheetSettings);

// Specifies the fields that will be available for the created data source.
IExcelSchemaProvider schemaProvider = excelDataSource.GetService(typeof(IExcelSchemaProvider)) 
    as IExcelSchemaProvider;
FieldInfo[] availableFields = schemaProvider.GetSchema(excelDataSource.FileName, null, 
    ExcelDocumentFormat.Xlsx, excelDataSource.SourceOptions, System.Threading.CancellationToken.None);
List<string> fieldsToSelect = new List<string>() { "CategoryName", "ProductName", "Country", "Quantity", 
    "Extended Price"};
foreach (FieldInfo field in availableFields) {
    if (fieldsToSelect.Contains(field.Name)) {
        excelDataSource.Schema.Add(field);
    }
    else {
        field.Selected = false;
        excelDataSource.Schema.Add(field);
    }
}
excelDataSource.Fill();
vb
' Creates an Excel data source and selects the specific cell range from the SalesPerson worksheet.
Dim excelDataSource As New ExcelDataSource()
excelDataSource.Name = "Excel Data Source"
excelDataSource.FileName = HostingEnvironment.MapPath("~/App_Data/ExcelDataSource.xlsx")
Dim worksheetSettings As New ExcelWorksheetSettings("SalesPerson", "A1:L2000")
excelDataSource.SourceOptions = New ExcelSourceOptions(worksheetSettings)

' Specifies the fields that will be available for the created data source.
Dim schemaProvider As IExcelSchemaProvider = TryCast(excelDataSource.GetService(GetType(IExcelSchemaProvider)), IExcelSchemaProvider)
Dim availableFields() As FieldInfo = schemaProvider.GetSchema(excelDataSource.FileName, Nothing, ExcelDocumentFormat.Xlsx, excelDataSource.SourceOptions, System.Threading.CancellationToken.None)
Dim fieldsToSelect As New List(Of String) (New String() {"CategoryName", "ProductName", "Country", "Quantity", "Extended Price"})
For Each field As FieldInfo In availableFields
    If fieldsToSelect.Contains(field.Name) Then
        excelDataSource.Schema.Add(field)
    Else
        field.Selected = False
        excelDataSource.Schema.Add(field)
    End If
Next field
excelDataSource.Fill()

See Also

ExcelDataSource Class

ExcelDataSource Members

DevExpress.DataAccess.Excel Namespace