Binding to Excel Data Sources

  • 3 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, the ExcelDataSource component and in code.

Data Source Configuration Wizard

  1. Invoke the smart tag for the required data-aware DevExpress control and select the 'Data Source Wizard' option.

    Excel Data Source - 1

  2. 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.

  3. Select the required data source and set advanced options using the Data Source Wizard. The following gallery illustrates this process.

  4. Click finish and run the application to see the result.

    Excel Data Source - Res

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.

  1. Locate the ExcelDataSource component on the toolbox under the 'Data&Analytics' group and drop it onto your form.

    Excel Data Source - AddSource

  2. 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.

    Excel Data Source - Edit Source

  3. 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.

    Excel Data Source - Specify Source

  4. The ExcelDataSource component does not retrieve records automatically. Thus, to populate your data control, call the Fill or FillAsync method.

    
    private void Form1_Load(object sender, EventArgs e) {
        myExcelSource.Fill();
        //myExcelSource.FillAsync();
    }
    

Binding in Code

  1. Create a new instance of the ExcelDataSource component.

    
    DevExpress.DataAccess.Excel.ExcelDataSource myExcelSource = new DevExpress.DataAccess.Excel.ExcelDataSource();
    
  2. Use the ExcelDataSource.FileName or ExcelDataSource.Stream properties to supply the component with a data source from a local storage or stream.

    
    myExcelSource.FileName = @"d:\Work\Excel DataSources\Book1.xlsx";
    
  3. 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 ExcelWorksheetSettings, ExcelTableSettings or ExcelDefinedNameSettings objects for each ExcelSourceOptions class instance.

    
    ExcelWorksheetSettings worksheetSettings = new ExcelWorksheetSettings("SalesPerson", "A1:L100");
    myExcelSource.SourceOptions = new ExcelSourceOptions(worksheetSettings);
    //or
    myExcelSource.SourceOptions = new CsvSourceOptions() { CellRange = "A1:L100" };
    
  4. Set additional import options if required.

    
    myExcelSource.SourceOptions.SkipEmptyRows = false;
    myExcelSource.SourceOptions.UseFirstRowAsHeader = true;
    
  5. Assign your ExcelDataSource to the data-aware control and call the component's Fill/FillAsync method to populate it with records.

    
    myExcelSource.Fill();
    gridControl1.DataSource = myExcelSource;
    

    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;