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.
-
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; } // ... }
-
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; } // ... }
-
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; }
- Data Grid
- Button
- Featured Scenarios
- Use Office File API in .NET MAUI Applications (macOS, iOS, Android)
- Workbook
- Worksheets
- DevExpress .NET MAUI Demo Center
- Stocks App
- Data Grid
- Data Form
- Data Editors
- Charts
- Scheduler
- Tab Page
- Tab View
- Drawer Page
- Drawer View
- Collection View
- Popup
(you will be redirected to DevExpress.com to submit your response)