Skip to main content

Bind a Report to an Excel Workbook (Runtime Sample)

  • 4 minutes to read

View Example: Reporting for WinForms - Bind a Report to an Excel Workbook at Runtime

This example demonstrates how to bind a report to a Microsoft Excel workbook and specify the report layout at runtime.

Do the following to bind a report to an Excel file:

  1. Create the ExcelDataSource class’s instance and specify the full path to the source Excel file using the ExcelDataSource.FileName property.

    Select the required worksheet by creating a new ExcelWorksheetSettings object and using its ExcelWorksheetSettings.WorksheetName property.

  2. Create the ExcelSourceOptions class’s instance to specify the required options for extracting data from the workbook.

    Assign the ExcelSourceOptions.ImportSettings property to the previously created ExcelWorksheetSettings object.

    Disable the ExcelSourceOptions.SkipHiddenRows and ExcelSourceOptions.SkipHiddenColumns options to include hidden rows and columns to the resulting data source.

    Set the ExcelDataSource.SourceOptions property to this ExcelSourceOptions object.

  3. The report engine automatically generates a data source schema by default. Do the following to manually define the data source schema if you need to customize the data fields’ display names and types:

  4. Assign the created data source to the report’s XtraReportBase.DataSource property.
  5. Use the XRControl.ExpressionBindings property to provide data to the report.
using DevExpress.DataAccess.Excel;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Configuration;
// ...
// Create an empty report.
XtraReport report = new XtraReport();

// Create a new Excel data source.
ExcelDataSource excelDataSource = new ExcelDataSource();
excelDataSource.FileName = "..//..//Northwind.xlsx";

// Select a required worksheet.
ExcelWorksheetSettings excelWorksheetSettings = new ExcelWorksheetSettings();
excelWorksheetSettings.WorksheetName = "Sheet_Categories";

// Specify import settings.
ExcelSourceOptions excelSourceOptions = new ExcelSourceOptions();
excelSourceOptions.ImportSettings = excelWorksheetSettings;
excelSourceOptions.SkipHiddenRows = false;
excelSourceOptions.SkipHiddenColumns = false;
excelDataSource.SourceOptions = excelSourceOptions;

// Create new fields and specify their settings.
FieldInfo fieldCategoryID = new FieldInfo { Name = "CategoryID", Type = typeof(double), Selected = false };
FieldInfo fieldCategoryName = new FieldInfo { Name = "CategoryName", Type = typeof(string) };
FieldInfo fieldDescription = new FieldInfo { Name = "Description", Type = typeof(string) };
// Add the created fields to the data source schema in the order that matches the column order in the source file.  
excelDataSource.Schema.AddRange(new FieldInfo[] { fieldCategoryID, fieldCategoryName, fieldDescription });

// Assign the data source to the report.
report.DataSource = excelDataSource;

// Add a detail band to the report.
DetailBand detailBand = new DetailBand();
detailBand.Height = 50;
report.Bands.Add(detailBand);

// Create a new label.
XRLabel label = new XRLabel();
// Specify the label's binding depending on the data binding mode.
if (Settings.Default.UserDesignerOptions.DataBindingMode == DataBindingMode.Bindings)
    label.DataBindings.Add("Text", null, "CategoryName");
else label.ExpressionBindings.Add(new ExpressionBinding("BeforePrint", "Text", "[CategoryName]"));
// Add the label to the detail band.
detailBand.Controls.Add(label);


// Show the report's print preview.
report.ShowPreview();  
See Also