Export Data
- 7 minutes to read
Note
The DevExpress .NET MAUI DataGrid Export feature requires a license to our Office File API Suite (basic edition). Without a valid license, you will not be able to use this feature within your .NET MAUI application. If you own an active DevExpress Subscription (Universal, DXperience, WinForms, WPF, ASP.NET), the basic version of the DevExpress Office File API Suite is available to you.
The DataGridView control allows you to export its data to the following file formats:
File Format | DataGridExportExtensions Method |
---|---|
ExportToPDFAsync | |
XLS | ExportToXlsAsync |
XLSX | ExportToXlsxAsync |
MHT | ExportToMhtAsync |
CSV | ExportToCsvAsync |
HTML | ExportToHtmlAsync |
RTF | ExportToRtfAsync |
DOCX | ExportToDocxAsync |
TXT | ExportToTextAsync |
Image file (the default is PNG) | ExportToImageAsync |
Follow the steps below to export DataGridView data:
- Install the DevExpress.Maui.DataGrid.Export NuGet package.
Reference the
DevExpress.Maui.DataGrid
andDevExpress.Maui.DataGrid.Export
namespaces.Call any of DataGridExportExtensions.Export methods.
The following code sample exports DataGridView data to the XLSX
format:
using DevExpress.Maui.DataGrid.Export;
using DevExpress.Maui.DataGrid
// ...
public async void OnExportToXslxButtonClicked(object sender, EventArgs e) {
string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "Document_Xslx.xlsx");
await gridView.ExportToXlsxAsync(targetFile);
await Share.Default.RequestAsync(new ShareFileRequest {
Title = "Share XSLX file",
File = new ShareFile(targetFile)
});
}
<ContentPage ...
xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dx="clr-namespace:DevExpress.Maui.Core;assembly=DevExpress.Maui.Core">
<ContentPage.Content>
<Grid>
<dx:DXButton Content="Export to XLSX" Clicked="OnExportToXslxButtonClicked"/>
<dxg:DataGridView x:Name="gridView" ItemsSource="{Binding Employees}" AutoGenerateColumnsMode="None">
<!-- ... -->
</dxg:DataGridView>
</Grid>
</ContentPage.Content>
</ContentPage>
Customize Appearance
Select Elements for Export
Use the following properties to select DataGridView elements to be included in the exported file:
Property | Description |
---|---|
GridColumn.AllowExport | Gets or sets whether to display the column in the exported document. This is a bindable property. |
DataGridView.ExportColumnHeaderMode | Gets or sets the location of DataGridView headers in an exported document. This is a bindable property. |
DataGridView.ExportGroupSummary | Gets or sets whether to print the group summary panel. This is a bindable property. |
DataGridView.ExportTotalSummary | Gets or sets whether to export the summary panel. This is a bindable property. |
Example
The following code sample exports a hidden GridColumn:
<ContentPage ...
xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dx="clr-namespace:DevExpress.Maui.Core;assembly=DevExpress.Maui.Core">
<ContentPage.Content>
<Grid>
<dx:DXButton Content="Export to XLSX" Clicked="OnExportToXslxButtonClicked"/>
<dxg:DataGridView x:Name="gridView" ItemsSource="{Binding Employees}" AutoGenerateColumnsMode="None">
<dxg:DataGridView.Columns>
<dxg:ImageColumn FieldName="Image" IsVisible="false" AllowExport="true/>
<dxg:TextColumn FieldName="FullName"/>
<!-- ... -->
</dxg:DataGridView.Columns>
</dxg:DataGridView>
</Grid>
</ContentPage.Content>
</ContentPage>
using DevExpress.Maui.DataGrid.Export;
using DevExpress.Maui.DataGrid
// ...
public async void OnExportToXslxButtonClicked(object sender, EventArgs e) {
string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "Document_Xslx.xlsx");
await gridView.ExportToXlsxAsync(targetFile);
await Share.Default.RequestAsync(new ShareFileRequest {
Title = "Share XSLX file",
File = new ShareFile(targetFile)
});
}
Appearance Properties
You can use the following properties to customize appearance settings in the exported document:
DataGridView Property | GridColumn Property | Description |
---|---|---|
DataGridView.ExportColumnHeaderAppearance | GridColumn.ExportColumnHeaderAppearance | Gets or sets the appearance object applied to column header items in the exported document. This is a bindable property. |
DataGridView.ExportCellAppearance | GridColumn.ExportCellAppearance | Gets or sets the appearance object applied to cells in the exported document. This is a bindable property. |
DataGridView.ExportGroupRowAppearance | - | Gets or sets the appearance object applied to group row items (including Group Summary) in the exported document. This is a bindable property. |
DataGridView.ExportGroupSummaryAppearance | - | Gets or sets the appearance object applied to the group summary items in the exported document. This is a bindable property. |
DataGridView.ExportTotalSummaryAppearance | GridColumn.ExportTotalSummaryAppearance | Gets or sets the appearance object applied to the total summary items when they are exported. This is a bindable property. |
- | GridColumn.ExportMaxWidth | Gets or sets the column’s maximum width in an exported document. This is a bindable property. |
- | GridColumn.ExportMinWidth | Gets or sets the column’s minimum width in an exported document. This is a bindable property. |
- | GridColumn.ExportWidth | Gets or sets the column’s width in an exported document. This is a bindable property. |
Example
The following code sample specifies appearances of column headers and group rows in the exported document:
<ContentPage ...
xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dx="clr-namespace:DevExpress.Maui.Core;assembly=DevExpress.Maui.Core">
<ContentPage.Content>
<StackLayout>
<dx:DXButton Content="Export to XLSX" Clicked="OnExportToXslxButtonClicked" />
<dxg:DataGridView x:Name="gridView" ItemsSource="{Binding Employees}"
AutoGenerateColumnsMode="None">
<dxg:DataGridView.ExportColumnHeaderAppearance>
<dxg:ExportBrickAppearance Background="LightGreen" BorderColor="Black" BorderThickness="1" FontSize="14"/>
</dxg:DataGridView.ExportColumnHeaderAppearance>
<dxg:DataGridView.ExportGroupRowAppearance>
<dxg:ExportBrickAppearance Background="#FCE9B2" BorderColor="Black" BorderThickness="1" FontSize="14"/>
</dxg:DataGridView.ExportGroupRowAppearance>
<dxg:DataGridView.Columns>
<!-- ... -->
</dxg:DataGridView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Events
You can use the following events to customize the appearance of exported elements:
Property | Description |
---|---|
DataGridView.CustomCellExport | Allows you to customize an exported cell. |
DataGridView.CustomColumnHeaderExport | Allows you to customize an exported column header. |
DataGridView.CustomGroupRowExport | Allows you to customize an exported group row. |
DataGridView.CustomGroupSummaryExport | Allows you to customize an exported group summary. |
DataGridView.CustomTotalSummaryExport | Allows you to customize an exported total summary. |
Example
The following code sample specifies the background color of even rows in the exported document:
<ContentPage ...
xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dx="clr-namespace:DevExpress.Maui.Core;assembly=DevExpress.Maui.Core">
<ContentPage.Content>
<StackLayout>
<dx:DXButton Content="Export to XLSX" Clicked="OnExportToXslxButtonClicked" />
<dxg:DataGridView x:Name="gridView" ItemsSource="{Binding Employees}"
AutoGenerateColumnsMode="None" CustomCellExport="CustomCellExport">
<!-- ... -->
</dxg:DataGridView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
using DevExpress.Maui.DataGrid.Export;
using DevExpress.Maui.DataGrid;
// ...
public void CustomCellExport(object sender, CustomCellExportEventArgs e) {
var rowHandle = gridView.GetRowHandle(e.SourceIndex);
if (rowHandle % 2 == 0) {
e.Appearance.Background = Colors.Red;
}
}
public async void OnExportToXslxButtonClicked(object sender, EventArgs e) {
string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "Document_Xslx.xlsx");
await gridView.ExportToXlsxAsync(targetFile);
await Share.Default.RequestAsync(new ShareFileRequest {
Title = "Share XSLX file",
File = new ShareFile(targetFile)
});
}
Customize Document Settings
You can use the following properties to customize the exported document:
Property | Description |
---|---|
DataGridExportLink.Landscape | Gets or sets a value indicating whether the page orientation is landscape. |
DataGridExportLink.Margins | Gets or sets the margins of a report page (measured in hundredths of an inch). |
DataGridExportLink.MinMargins | Specifies the minimum printer margin values. |
DataGridExportLink.PageHeaderFooter | Gets or sets the object used to fill the document’s area that is occupied by the page header and page footer. |
DataGridExportLink.PaperKind | Gets or sets the type of paper for the document. |
DataGridExportLink.RightToLeftLayout | Specifies the orientation of the document’s content. |
DataGridExportLink.RtfReportFooter | Gets or sets the RTF text, which will be printed as a Report Footer. |
DataGridExportLink.RtfReportHeader | Gets or sets the RTF text, which will be printed as a Report Header. |
Example
The following example specifies the exported document margins, paper type, and orientation:
// ...
public partial class MainPage : ContentPage {
// ...
public async void OnExportToPdfButtonClicked(object sender, EventArgs e) {
var link = this.gridView.GetExportLink();
link.PaperKind = DevExpress.Drawing.Printing.DXPaperKind.A5;
link.Landscape = true;
link.Margins = new DevExpress.Drawing.DXMargins(100, 100, 5, 5);
await Task.Run(link.CreateDocument);
string targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "Document_PDF1.pdf");
using (FileStream outputStream = System.IO.File.OpenWrite(targetFile)) {
await Task.Run(() => link.ExportToPdf(outputStream));
}
await Share.Default.RequestAsync(new ShareFileRequest {
Title = "Share PDF file",
File = new ShareFile(targetFile)
});
}
}
<ContentPage ...
xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dx="clr-namespace:DevExpress.Maui.Core;assembly=DevExpress.Maui.Core">
<ContentPage.Content>
<StackLayout>
<dx:DXButton x:Name="exportButton" Content="Export to PDF" Clicked="OnExportToPdfButtonClicked"/>
<dxg:DataGridView x:Name="gridView" ItemsSource="{Binding Employees}" AutoGenerateColumnsMode="None" >
<dxg:DataGridView.Columns>
<!-- ... -->
</dxg:DataGridView.Columns>
</dxg:DataGridView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Performance Enhancements
When you export the DataGridView, the DataGridExportLink class instance is created. This class is a bridge between the DataGridView and the exported document.
The DataGridView export APIs allow you to generate a DataGridExportLink in the background thread while a user interacts with the UI.
Example
The following code sample generates DataGridExportLink in the background thread while a user interacts with the UI:
// ...
public partial class MainPage : ContentPage {
// ...
public async void OnExportButtonClicked(object sender, EventArgs e) {
DataGridExportLink link = this.grid.GetExportLink();
Task modelCreating = Task.Factory.StartNew(() => link.CreateDocument());
string action = await DisplayActionSheet("Select export format:", "Cancel", null, "Excel", "Word", "PDF");
MemoryStream outputStream = new();
Task exporting = modelCreating.ContinueWith((task) => {
switch (action) {
case "Excel":
link.ExportToXlsx(outputStream);
break;
case "Word":
link.ExportToDocx(outputStream);
break;
case "PDF":
link.ExportToPdf(outputStream);
break;
}
});
string fileName = await DisplayPromptAsync("Enter file name:", "Document");
if (string.IsNullOrEmpty(fileName))
return;
switch (action) {
case "Excel":
if (!fileName.EndsWith(".xlsx"))
fileName += ".xlsx";
break;
case "Word":
if (!fileName.EndsWith(".docx"))
fileName += ".docx";
break;
case "PDF":
if (!fileName.EndsWith(".pdf"))
fileName += ".pdf";
break;
}
string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);
await exporting;
using (FileStream fileStream = new(targetFile, FileMode.Create, FileAccess.Write)) {
outputStream.Seek(0, SeekOrigin.Begin);
outputStream.CopyTo(fileStream);
}
await Share.RequestAsync(new ShareFileRequest {
Title = fileName,
File = new ShareFile(targetFile)
});
}
}
<ContentPage ...
xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dx="clr-namespace:DevExpress.Maui.Core;assembly=DevExpress.Maui.Core">
<ContentPage.Content>
<Grid>
<Button Text="Export" Clicked="OnExportButtonClicked"/>
<dxg:DataGridView ItemsSource="{Binding Employees}" x:Name="grid">
<dxg:DataGridView.Columns>
<!-- ... -->
</dxg:DataGridView.Columns>
</dxg:DataGridView>
</Grid>
</ContentPage.Content>
</ContentPage>