Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,75 @@
# How-can-we-fetch-excel-file-with-file-picker-in-.NET-MAUI-DataGrid
How can we fetch excel file with file picker in .NET MAUI DataGrid
# How can we fetch a excel file with file picker and import data into the .NET MAUI DataGrid?
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).

## C#
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();
}
```

![FilePicker.png](https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI3NzczIiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.lNSPuqqSHipgdcTfTf8_nieKs-jomRpzqVzN5sxg7gc)

[View sample in GitHub](https://github.com/SyncfusionExamples/How-can-we-fetch-excel-file-with-file-picker-in-.NET-MAUI-DataGrid)

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).

##### Conclusion

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](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.
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.

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!
14 changes: 14 additions & 0 deletions SfDataGridSample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SfDataGridSample"
x:Class="SfDataGridSample.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
12 changes: 12 additions & 0 deletions SfDataGridSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SfDataGridSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
}
15 changes: 15 additions & 0 deletions SfDataGridSample/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="SfDataGridSample.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SfDataGridSample"
Shell.FlyoutBehavior="Disabled"
Title="SfDataGridSample">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions SfDataGridSample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SfDataGridSample
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
14 changes: 14 additions & 0 deletions SfDataGridSample/Helper/SaveService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SfDataGridSample
{
public partial class SaveService
{
//Method to save document as a file and view the saved document.
public partial void SaveAndView(string filename, string contentType, MemoryStream stream);
}
}
43 changes: 43 additions & 0 deletions SfDataGridSample/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
xmlns:local="clr-namespace:SfDataGridSample"
x:Class="SfDataGridSample.MainPage">

<ContentPage.BindingContext>
<local:EmployeeViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>


<StackLayout>
<HorizontalStackLayout Padding="20,0,20,0">
<Button Text="Export" Padding="5"
Clicked="Export_Clicked" />
<Button Text="Import" Padding="5"
Clicked="Import_Clicked" />
<Button Text="ClearGrid" Padding="5"
Clicked="Button_Clicked" />
</HorizontalStackLayout>


<syncfusion:SfDataGrid x:Name="dataGrid" Margin="20"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
GridLinesVisibility="Both"
ColumnWidthMode="Auto"
HeaderGridLinesVisibility="Both"
AutoGenerateColumnsMode="None"
ItemsSource="{Binding Employees}">

<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="Name" />
<syncfusion:DataGridTextColumn MappingName="Title" />
<syncfusion:DataGridDateColumn MappingName="HireDate" />
<syncfusion:DataGridTextColumn MappingName="MaritalStatus" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

</StackLayout>

</ContentPage>
88 changes: 88 additions & 0 deletions SfDataGridSample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using Syncfusion.Maui.DataGrid.Exporting;
using Syncfusion.XlsIO;
using System.Data;

namespace SfDataGridSample
{
public partial class MainPage : ContentPage
{

public MainPage()
{
InitializeComponent();
}
void Export_Clicked(System.Object sender, System.EventArgs e)
{
DataGridExcelExportingController excelExport = new DataGridExcelExportingController();
DataGridExcelExportingOption option = new DataGridExcelExportingOption();
var excelEngine = excelExport.ExportToExcel(dataGrid, option);
var workbook = excelEngine.Excel.Workbooks[0];
MemoryStream stream = new MemoryStream();
workbook.SaveAs(stream);
workbook.Close();
excelEngine.Dispose();
string OutputFilename = "ExportFeature.xlsx";
SaveService saveService = new();
saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream);
}

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();
}

void Button_Clicked(System.Object sender, System.EventArgs e)
{
dataGrid.ItemsSource = null;
}
}
}
27 changes: 27 additions & 0 deletions SfDataGridSample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Logging;
using Syncfusion.Maui.Core;
using Syncfusion.Maui.Core.Hosting;
namespace SfDataGridSample
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureSyncfusionCore()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

#if DEBUG
builder.Logging.AddDebug();
#endif

return builder.Build();
}
}
}
Loading