Skip to content

Commit 775d561

Browse files
committed
Code updated.
1 parent 2cb7a87 commit 775d561

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1969
-2
lines changed

README.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,75 @@
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+
![FilePicker.png](https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI3NzczIiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.lNSPuqqSHipgdcTfTf8_nieKs-jomRpzqVzN5sxg7gc)
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!

SfDataGridSample/App.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDataGridSample"
5+
x:Class="SfDataGridSample.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>

SfDataGridSample/App.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
12+
}

SfDataGridSample/AppShell.xaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="SfDataGridSample.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:SfDataGridSample"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="SfDataGridSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>

SfDataGridSample/AppShell.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SfDataGridSample
8+
{
9+
public partial class SaveService
10+
{
11+
//Method to save document as a file and view the saved document.
12+
public partial void SaveAndView(string filename, string contentType, MemoryStream stream);
13+
}
14+
}

SfDataGridSample/MainPage.xaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
5+
xmlns:local="clr-namespace:SfDataGridSample"
6+
x:Class="SfDataGridSample.MainPage">
7+
8+
<ContentPage.BindingContext>
9+
<local:EmployeeViewModel x:Name="viewModel"/>
10+
</ContentPage.BindingContext>
11+
12+
13+
<StackLayout>
14+
<HorizontalStackLayout Padding="20,0,20,0">
15+
<Button Text="Export" Padding="5"
16+
Clicked="Export_Clicked" />
17+
<Button Text="Import" Padding="5"
18+
Clicked="Import_Clicked" />
19+
<Button Text="ClearGrid" Padding="5"
20+
Clicked="Button_Clicked" />
21+
</HorizontalStackLayout>
22+
23+
24+
<syncfusion:SfDataGrid x:Name="dataGrid" Margin="20"
25+
VerticalOptions="FillAndExpand"
26+
HorizontalOptions="FillAndExpand"
27+
GridLinesVisibility="Both"
28+
ColumnWidthMode="Auto"
29+
HeaderGridLinesVisibility="Both"
30+
AutoGenerateColumnsMode="None"
31+
ItemsSource="{Binding Employees}">
32+
33+
<syncfusion:SfDataGrid.Columns>
34+
<syncfusion:DataGridTextColumn MappingName="Name" />
35+
<syncfusion:DataGridTextColumn MappingName="Title" />
36+
<syncfusion:DataGridDateColumn MappingName="HireDate" />
37+
<syncfusion:DataGridTextColumn MappingName="MaritalStatus" />
38+
</syncfusion:SfDataGrid.Columns>
39+
</syncfusion:SfDataGrid>
40+
41+
</StackLayout>
42+
43+
</ContentPage>

SfDataGridSample/MainPage.xaml.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Syncfusion.Maui.DataGrid.Exporting;
2+
using Syncfusion.XlsIO;
3+
using System.Data;
4+
5+
namespace SfDataGridSample
6+
{
7+
public partial class MainPage : ContentPage
8+
{
9+
10+
public MainPage()
11+
{
12+
InitializeComponent();
13+
}
14+
void Export_Clicked(System.Object sender, System.EventArgs e)
15+
{
16+
DataGridExcelExportingController excelExport = new DataGridExcelExportingController();
17+
DataGridExcelExportingOption option = new DataGridExcelExportingOption();
18+
var excelEngine = excelExport.ExportToExcel(dataGrid, option);
19+
var workbook = excelEngine.Excel.Workbooks[0];
20+
MemoryStream stream = new MemoryStream();
21+
workbook.SaveAs(stream);
22+
workbook.Close();
23+
excelEngine.Dispose();
24+
string OutputFilename = "ExportFeature.xlsx";
25+
SaveService saveService = new();
26+
saveService.SaveAndView(OutputFilename, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", stream);
27+
}
28+
29+
void Import_Clicked(System.Object sender, System.EventArgs e)
30+
{
31+
LoadDataGridAsync();
32+
}
33+
34+
private async Task LoadDataGridAsync()
35+
{
36+
//Creates a new instance for ExcelEngine
37+
ExcelEngine excelEngine = new ExcelEngine();
38+
39+
//Initialize IApplication
40+
Syncfusion.XlsIO.IApplication application = excelEngine.Excel;
41+
42+
43+
var customFileType = new FilePickerFileType(
44+
new Dictionary<DevicePlatform, IEnumerable<string>>
45+
{
46+
// iOS: using Uniform Type Identifiers (UTIs)
47+
{ DevicePlatform.iOS, new[] { "public.data" } },
48+
49+
// Android: using MIME types
50+
{ DevicePlatform.Android, new[] { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } },
51+
52+
// Windows: using file extensions
53+
{ DevicePlatform.WinUI, new[] { ".xlsx" } },
54+
});
55+
56+
PickOptions pickOptions = new PickOptions()
57+
{
58+
PickerTitle = "Please select DataGrid file",
59+
FileTypes = customFileType,
60+
};
61+
62+
var result = await FilePicker.Default.PickAsync(pickOptions);
63+
if (result != null)
64+
{
65+
//Load the file into stream
66+
Stream inputStream = await result.OpenReadAsync();
67+
68+
//Loads or open an existing workbook through Open method of IWorkbooks
69+
IWorkbook workbook = excelEngine.Excel.Workbooks.Open(inputStream);
70+
71+
IWorksheet worksheet = workbook.Worksheets[0];
72+
73+
DataTable customersTable = worksheet.ExportDataTable(1, 1, 10, 6, ExcelExportDataTableOptions.ColumnNames);
74+
75+
this.dataGrid.ItemsSource = customersTable;
76+
77+
workbook.Close();
78+
}
79+
80+
excelEngine.Dispose();
81+
}
82+
83+
void Button_Clicked(System.Object sender, System.EventArgs e)
84+
{
85+
dataGrid.ItemsSource = null;
86+
}
87+
}
88+
}

SfDataGridSample/MauiProgram.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core;
3+
using Syncfusion.Maui.Core.Hosting;
4+
namespace SfDataGridSample
5+
{
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)