|
1 | | -# How-can-we-fetch-excel-file-with-file-picker-in-.NET-MAUI-DataGrid |
2 | | -How can we fetch excel file with file picker in .NET MAUI DataGrid |
| 1 | +# How can we fetch a excel file with file picker and import data into the .NET MAUI DataGrid? |
| 2 | +In this article, we will show you how can we fetch a excel file with file picker and import data into the [.Net Maui DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid). |
| 3 | + |
| 4 | +## C# |
| 5 | +The below code illustrates how to fetch a excel file with file picker and import data into the DataGrid. |
| 6 | +``` |
| 7 | + void Import_Clicked(System.Object sender, System.EventArgs e) |
| 8 | + { |
| 9 | + LoadDataGridAsync(); |
| 10 | + } |
| 11 | +
|
| 12 | + private async Task LoadDataGridAsync() |
| 13 | + { |
| 14 | + //Creates a new instance for ExcelEngine |
| 15 | + ExcelEngine excelEngine = new ExcelEngine(); |
| 16 | +
|
| 17 | + //Initialize IApplication |
| 18 | + Syncfusion.XlsIO.IApplication application = excelEngine.Excel; |
| 19 | +
|
| 20 | +
|
| 21 | + var customFileType = new FilePickerFileType( |
| 22 | + new Dictionary<DevicePlatform, IEnumerable<string>> |
| 23 | + { |
| 24 | + // iOS: using Uniform Type Identifiers (UTIs) |
| 25 | + { DevicePlatform.iOS, new[] { "public.data" } }, |
| 26 | +
|
| 27 | + // Android: using MIME types |
| 28 | + { DevicePlatform.Android, new[] { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } }, |
| 29 | +
|
| 30 | + // Windows: using file extensions |
| 31 | + { DevicePlatform.WinUI, new[] { ".xlsx" } }, |
| 32 | + }); |
| 33 | +
|
| 34 | + PickOptions pickOptions = new PickOptions() |
| 35 | + { |
| 36 | + PickerTitle = "Please select DataGrid file", |
| 37 | + FileTypes = customFileType, |
| 38 | + }; |
| 39 | +
|
| 40 | + var result = await FilePicker.Default.PickAsync(pickOptions); |
| 41 | + if (result != null) |
| 42 | + { |
| 43 | + //Load the file into stream |
| 44 | + Stream inputStream = await result.OpenReadAsync(); |
| 45 | +
|
| 46 | + //Loads or open an existing workbook through Open method of IWorkbooks |
| 47 | + IWorkbook workbook = excelEngine.Excel.Workbooks.Open(inputStream); |
| 48 | +
|
| 49 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 50 | +
|
| 51 | + DataTable customersTable = worksheet.ExportDataTable(1, 1, 10, 6, ExcelExportDataTableOptions.ColumnNames); |
| 52 | +
|
| 53 | + this.dataGrid.ItemsSource = customersTable; |
| 54 | +
|
| 55 | + workbook.Close(); |
| 56 | + } |
| 57 | +
|
| 58 | + excelEngine.Dispose(); |
| 59 | + } |
| 60 | +``` |
| 61 | + |
| 62 | +  |
| 63 | + |
| 64 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-can-we-fetch-excel-file-with-file-picker-in-.NET-MAUI-DataGrid) |
| 65 | + |
| 66 | +Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid). |
| 67 | + |
| 68 | +##### Conclusion |
| 69 | + |
| 70 | +I hope you enjoyed learning about how to fetch a excel file with file picker and import data into .NET MAUI DataGrid (SfDataGrid) Pdf Exporting. |
| 71 | + |
| 72 | +You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data. |
| 73 | +For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components. |
| 74 | + |
| 75 | +If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you! |
0 commit comments