Articles in this section
Category / Section

How to fetch excel file and import data into the .NET MAUI DataGrid (SfDataGrid) ?

3 mins read

In this article, we demonstrate the process of fetching an Excel file using a file picker and importing its data into the .NET MAUI DataGrid.

C#
The code below illustrates how to fetch an Excel file with the file picker and import data into the DataGrid.

    void Import_Clicked(System.Object sender, System.EventArgs e)
    {
        LoadDataGridAsync();
    }

    private async Task LoadDataGridAsync()
    {
        //Creates a new instance for ExcelEngine
        ExcelEngine excelEngine = new ExcelEngine();

        //Initialize IApplication
        Syncfusion.XlsIO.IApplication application = excelEngine.Excel;


        var customFileType = new FilePickerFileType(
            new Dictionary<DevicePlatform, IEnumerable<string>>
            {
                // iOS: using Uniform Type Identifiers (UTIs)
                { DevicePlatform.iOS, new[] { "public.data" } }, 

                // Android: using MIME types
                { DevicePlatform.Android, new[] { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } }, 

                // Windows: using file extensions
                { DevicePlatform.WinUI, new[] { ".xlsx" } }, 
            });

        PickOptions pickOptions = new PickOptions()
        {
            PickerTitle = "Please select DataGrid file",
            FileTypes = customFileType,
        };

        var result = await FilePicker.Default.PickAsync(pickOptions);
        if (result != null)
        {
            //Load the file into stream
            Stream inputStream = await result.OpenReadAsync();

            //Loads or open an existing workbook through Open method of IWorkbooks
            IWorkbook workbook = excelEngine.Excel.Workbooks.Open(inputStream);

            IWorksheet worksheet = workbook.Worksheets[0];

            DataTable customersTable = worksheet.ExportDataTable(1, 1, 10, 6, ExcelExportDataTableOptions.ColumnNames);

            this.dataGrid.ItemsSource = customersTable;

            workbook.Close();
        }

        excelEngine.Dispose();
    }

Output

FilePicker.png

Download the complete sample from GitHub.

Conclusion

I hope you enjoyed learning how to fetch an Excel file and import data into .NET MAUI DataGrid.

You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.

Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied