How to: Connect the ASP.NET Core MVC Dashboard to an Excel Database

The example below shows how to create an Excel data source in code to make it available for end users. The specified cell range on the defined worksheet supplies the dashboard with data.

  1. In your application, create the Data folder and add the Financial.xls file to it from the following folder:

    C:\Users\Public\Documents\DevExpress Demos 18.2\Components\Data

  2. In the Startup.cs file, create a public method that returns a configured in-memory dashboard data source storage (DataSourceInMemoryStorage) and define the excel data source.

    using DevExpress.DashboardCommon;
    using DevExpress.DataAccess.Excel;
    
    public DataSourceInMemoryStorage CreateDataSourceStorage() {
        DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
    
        DashboardExcelDataSource excelDataSource = new DashboardExcelDataSource("Excel Data Source");
              excelDataSource.FileName = Path.Combine(HostingEnvironment.ContentRootPath, @"Data\Financial.xlsx");
              excelDataSource.SourceOptions = new ExcelSourceOptions(new ExcelWorksheetSettings("Current_Stocks"));
              dataSourceStorage.RegisterDataSource("excelDataSource", excelDataSource.SaveToXml());      
    
        return dataSourceStorage;
    }
    
    Note

    Use the IHostingEnvironment.ContentRootPath property to set the absolute path to the directory that contains the database.

  3. Use the DashboardConfigurator.SetDataSourceStorage method to set the data source storage and pass the created CreateDataSourceStorage method to use the returned value as a parameter.

    using DevExpress.AspNetCore;
    using DevExpress.DashboardAspNetCore;
    using DevExpress.DashboardWeb;
    
    public void ConfigureServices(IServiceCollection services) {            
        services
            .AddMvc()
            .AddDefaultDashboardController(configurator => {
                // ...
                configurator.SetDataSourceStorage(CreateDataSourceStorage());
            });
    }
    
  4. As a result, the Excel Data Source is displayed as a Web Dashboard's predefined data source.

    web-dashboard-ex-core-excel-data-source

    End users can now bind the dashboard items to data in the Web Dashboard's UI. To learn more, see Binding Dashboard Items to Data in the Web Dashboard's UI.