Binding to Excel Data Sources
- 4 minutes to read
This document illustrates how to bind to data from Microsoft Excel workbooks (XLS, XLSX or XLSM files) or CSV files using the Data Source Configuration Wizard, ExcelDataSource component, and in code.
Note
The Excel
#Data Source Configuration Wizard
Important
The Excel
wizard is not available for .NET projects. Please set up the Excel
in code.
Invoke the smart tag for the required data-aware DevExpress control and select the ‘Data Source Wizard’ option.
On the wizard main page, select the ‘Excel Data Source’ option. Click ‘New Data Source’ to create a new data source or select the required data source from the list, then click ‘Next’ to proceed.
Select the required data source and set advanced options using the Data Source Wizard. The following gallery illustrates this process.
Click finish and run the application to see the result.
#The ExcelDataSource Component
Using the ExcelDataSource component, you can provide data from Excel data sources for any data-aware control that does not support the Data Source Configuration Wizard.
Locate the ExcelDataSource component on the toolbox under the ‘Data&Analytics’ group and drop it onto your form.
When you add the ExcelDataSource component to an application form, the ‘Data Source Wizard’ dialog automatically pops up. Follow these steps to set up the connection. To invoke this wizard manually, use the component’s smart-tag as shown below.
Assign the customized ExcelDataSource component as the data source for the target control. For instance, for the Data Grid control, use the GridControl.DataSource property.
The ExcelDataSource component does not retrieve records automatically. Thus, to populate your data control, call the Fill or FillAsync method.
#Binding in Code
Create a new instance of the ExcelDataSource component.
Use the ExcelDataSource.FileName or ExcelDataSource.Stream properties to supply the component with a data source from a local storage or stream.
Depending on the data source type (Excel workbook or CSV file), create either a ExcelSourceOptions or CsvSourceOptions object. Using this object’s properties, you can specify a cell range to be loaded. Assign the object created to the ExcelDataSource.SourceOptions property.
Important
You must provide the Excel
Worksheet , ExcelSettings Table or ExcelSettings Defined objects for each ExcelName Settings Source class instance.Options Set additional import options if required.
Assign your ExcelDataSource to the data-aware control and call the component’s Fill/FillAsync method to populate it with records.
The complete code is listed below.
using DevExpress.DataAccess.Excel; //. . . DevExpress.DataAccess.Excel.ExcelDataSource myExcelSource = new DevExpress.DataAccess.Excel.ExcelDataSource(); myExcelSource.FileName = @"d:\Work\Excel DataSources\Book1.xlsx"; ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings("SalesPerson", "A1:L13"); myExcelSource.SourceOptions = new ExcelSourceOptions(worksheetSettings); //or myExcelSource.SourceOptions = new CsvSourceOptions() { CellRange = "A1:L100" }; myExcelSource.SourceOptions.SkipEmptyRows = false; myExcelSource.SourceOptions.UseFirstRowAsHeader = true; myExcelSource.Fill(); gridControl1.DataSource = myExcelSource;
#Convert ExcelDataSource to DataTable
The following example demonstrates how to ‘convert’ data from ExcelDataSource to a DataTable
:
public static class ExcelDataSourceExtension {
public static DataTable ToDataTable(this ExcelDataSource excelDataSource) {
IList list = ((IListSource)excelDataSource).GetList();
DevExpress.DataAccess.Native.Excel.DataView dataView = (DevExpress.DataAccess.Native.Excel.DataView)list;
List<PropertyDescriptor> props = dataView.Columns.ToList<PropertyDescriptor>();
DataTable table = new DataTable();
for(int i = 0; i < props.Count; i++) {
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach(DevExpress.DataAccess.Native.Excel.ViewRow item in list) {
for(int i = 0; i < values.Length; i++) {
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}