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.
The below code illustrates how to fetch a excel file with 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();
}
Take a moment to explore this documentation, where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
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.
You can refer to our .NET MAUI DataGrid’s feature tour page to learn about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data. For current customers, you can check out our .NET MAUI components on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
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, Direct-Trac or feedback portal, or the feedback portal. We are always happy to assist you!