# ExportToXlsx

The **ExportToXlsx** extension method allows exporting the RadGridView to the XLSX format. As the mechanism uses **RadSpreadProcessing** internally, there is no need for the user to make the integration manually. The method was introduced in **Q1 2015**.

## Assembly References

The **ExportToXlsx** method uses additional libraries so you need to add references to the following assemblies:

* Telerik.Windows.Documents.Core.dll
* Telerik.Windows.Documents.Spreadsheet.dll
* Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.dll
* Telerik.Windows.Controls.GridView\.Export.dll

> **Telerik.Windows.Controls.GridView\.Export.dll** is a new binary introduced in **Q1 SP of 2015**. It delimits the exporting to **Xlsx** functionality from **Telerik.Windows.Controls.GridView\.dll**, so in order to use **ExportToXlsx** method, the new dll should also be added.

## Method Overloads

1. **ExportToXlsx(Stream stream)** - Expects the specified stream to which you are exporting data to.

2. **ExportToXlsx(Stream stream, GridViewDocumentExportOptions options)** - Expects the specified stream to which you are exporting data to and parameter of type GridViewDocumentExportOptions. The latter is used to set the following export options:

* **Culture**: Sets a specific Culture.
* **Items**: The collection of items to be exported.
* **ShowColumnFooters**: Specifies whether column footers should be included on export.
* **ShowGroupFooters**: Specifies whether group footers should be included on export.
* **ShowColumnHeaders**: Specifies whether column headers should be included on export.
* **ExportDefaultStyles**: Specifies whether the GridViewDataControl will be exported with its default styles.
* **ExcludedColumns**: Allows you to choose which columns are excluded from export.
* **ShowGroupRows**: Specifies whether the group rows should be included on export (this property was introduced in **R3 2019**).

The following example shows how to use the method on a button click:

**Example 1: Use of ExportToXlsx Method**

```C#
private void btnExport_Click(object sender, RoutedEventArgs e)
{
    string extension = "xlsx";

    Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog()
    {
        DefaultExt = extension,
        Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
        FilterIndex = 1
    };

    if (dialog.ShowDialog() == true)
    {
        using (Stream stream = dialog.OpenFile())
        {
            gridViewExport.ExportToXlsx(stream,
                new GridViewDocumentExportOptions()
                {
                    ShowColumnFooters = true,
                    ShowColumnHeaders = true,
                    ShowGroupFooters = true
                });
        }
    }
}
```

The `ExportToXlsx` overloads do not accept a worksheet name. To set a custom name for an XLSX worksheet, use `GridViewSpreadStreamExport` and set its `SheetName` property before calling `RunExport`. For details, see [SpreadsheetStreamingExport](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/excel/export-with-spreadsheetstreamingexport).

## Export Default Styles

> To export the Default Styles of RadGridView in grouped state, at least one row must be expanded, so that the exporting engine can get the styles.

> important Exporting the Default Styles will take into account the styling applied to the **first element** of each type(cell, column header, etc.). This is due to performance optimizations. Exporting a separate style for the needed element is discussed in details here:[Style Exported XLSX & PDF Documents](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/how-to/style-exported-documents).

RadGridView can be exported with its default styles by setting the **ExportDefaultStyles** property to **True**

By default the ExportDefaultStyles property is set to false. You can see the result (Figure 1).

> caption Exporting with ExportDefaultStyles set to “false” (default)

![Telerik UI for WPF RadGridView XLSX export without default styles applied](https://www.telerik.com/products/wpf/documentation/assets/8e10b70d274293d4111bcaba106566fb/exportdefaultstyles.png)

You can set the **ExportDefaultStyles** value to **“true”** and see the result (Figure 2).

**Example 2: Configuring ExportDefaultStyles Setting**

```C#
gridViewExport.ExportToXlsx(stream,
    new GridViewDocumentExportOptions()
{
    ShowColumnHeaders = true,
		    ShowColumnFooters = true,
		    ShowGroupFooters = true,
		    ExportDefaultStyles = true

        });   
```

> caption Exporting with ExportDefaultStyles set to true

![Telerik UI for WPF RadGridView XLSX export with default styles applied](https://www.telerik.com/products/wpf/documentation/assets/174dc654ccf2908994ec562165a36c98/exportdefaultstyles2.png)

## Disable Column Width Auto Fit

**GridViewDocumentExportOptions** expose the boolean **AutoFitColumnsWidth** property. Its default value is **True**, meaning that the column's width will be automatically fit based on its content. To disable this behavior, its value can be set to **False**.

**Example 3: Setting the AutoFitColumnsWidth Property to False**

```C#
if (dialog.ShowDialog() == true)
{
    using (Stream stream = dialog.OpenFile())
    {
        gridViewExport.ExportToXlsx(stream,
            new GridViewDocumentExportOptions()
{
    ShowColumnHeaders = true,
                ShowColumnFooters = true,
                ShowGroupFooters = true,
                ExportDefaultStyles = true,
                AutoFitColumnsWidth = false
            });
    }
}
```

> caption Exporting with AutoFitColumnsWidth set to false

![Telerik UI for WPF RadGridView XLSX export with automatic column width fitting disabled](https://www.telerik.com/products/wpf/documentation/assets/f4937a45851c4610606a588e24bd04a0/autofitcolumnswidth.png)

## Disable GroupHeaderRow Aggregates

By default, the Aggregate results of the **GroupHeaderRow** will be exported. Note, that this is an operation performed on data level. Hiding the GroupHeaderRow Aggregates in the UI through a Style targeting the **GroupHeaderRow** element does not affect it. In order to disable the exporting of the GroupHeaderRow Aggregates, you can set the **ShowGroupHeaderColumnAggregates** of the **GridViewDocumentExportOptions** to **False**.

**Example 4: Setting ShowGroupHeaderRowAggregates**

```C#
if (dialog.ShowDialog() == true)
{
    using (Stream stream = dialog.OpenFile())
    {
        gridViewExport.ExportToXlsx(stream,
            new GridViewDocumentExportOptions()
            {
                ShowColumnFooters = true,
                ShowColumnHeaders = true,
                ShowGroupFooters = true,
                ShowGroupHeaderRowAggregates = false
            });
    }
}
```

## Events

There are two events related to the exporting of RadGridView with the ExportToXlsx method: *ElementExportingToDocument* and *ElementExportedToDocument*. You can find more information regarding them in the [Export Events](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/events/datacontext) section.

## How to

* **[Get the Column of the Corresponding Cell](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/how-to/column-of-cell)**

* **[Disable the Export of a Particular Column](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/how-to/disable-export-column)**

* **[Style Exported XLSX & PDF Documents](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/how-to/style-exported-documents)**

## See Also

* [RadGridView Overview](https://www.telerik.com/products/wpf/documentation/controls/radgridview/overview2)
* [Export](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/generic/export)
* [Export Async](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/generic/async)
* [Export Events](https://www.telerik.com/products/wpf/documentation/controls/radgridview/export/events/datacontext)
