Skip to content

Latest commit

 

History

History

ImportExcelData

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

DevExpress Data Grid for .NET MAUI - Import Excel Data

If you own an active DevExpress Universal or Office File API Subscription, you can use the DevExpress Office File API to import Excel data to our .NET MAUI Data Grid control. In this example, you can press the "Upload File" button to select the appropriate Excel file and display its data within the Data Grid. You can also select the "Use Sample File" option and press the button to display a predefined Excel file.

DevExpress Data Grid for .NET MAUI - Import Excel Data

Included Controls and Their Properties

Implementation Details

  1. Call the newCustomersWorkbook.LoadDocumentAsync(filePath) method to open an Excel file:

    private async Task LoadDataFromExcel(string filePath) {
        using Workbook newCustomersWorkbook = new Workbook();
        bool excelOpenResult = await newCustomersWorkbook.LoadDocumentAsync(filePath);
    
        if (!excelOpenResult) {
            errorHandler?.Invoke("Couldn't open the file");
            return;
        }
    
        // ...
    }
  2. Use the Workbook.Worksheets property to get the first sheet. To select cells with data, call the Worksheet.GetDataRange() method:

    private async Task LoadDataFromExcel(string filePath) {
    
        // ...
    
        // get the first sheet in the uploaded Excel file
        Worksheet firstWorkSheet = newCustomersWorkbook.Worksheets[0];
        // get the range of cells (TopRowIndex and LeftColumnIndex)
        CellRange valuesRange = firstWorkSheet.GetDataRange();
        int topRowIndex = valuesRange.TopRowIndex;
        int leftColumnIndex = valuesRange.LeftColumnIndex;
        // validate the received sheet and range of cells
        if (!IsValidDataStructure(firstWorkSheet, topRowIndex, leftColumnIndex)) {
            errorHandler?.Invoke("Data structure in the selected file is invalid");
            return;
        }
    
        // ...
    
    }
  3. Create the newCustomers data object and populate it with data items imported from the Excel file:

    private async Task LoadDataFromExcel(string filePath) {
    
        // ...
    
        List<Customer> newCustomers = new List<Customer>();
        for (int rowIndex = topRowIndex + 1; rowIndex < valuesRange.RowCount + topRowIndex; rowIndex++) {
            Customer newCustomer = new Customer() {
                FirstName = firstWorkSheet.Rows[rowIndex][leftColumnIndex].Value.TextValue,
                LastName = firstWorkSheet.Rows[rowIndex][leftColumnIndex + 1].Value.TextValue,
                Company = firstWorkSheet.Rows[rowIndex][leftColumnIndex + 2].Value.TextValue
            };
            newCustomers.Add(newCustomer);
        }
    
        Customers = newCustomers;
    }

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)