[Expand]General Information
[Collapse]WinForms Controls
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications, and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Expand]Get Started With Data Grid and Views
   [Expand]Data Binding
    Unbound Columns
   [Expand]Data Editing and Validation
   [Expand]Filter and Search
   [Expand]Focus and Selection Handling
    Format Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
   [Expand]Batch Modifications
    Hit Information
    Popup Menus
   [Expand]Save and Restore Layout
   [Expand]Visual Elements
   [Expand]Design-Time Features
    [Expand]Conditional Formatting
    [Expand]Data Binding
    [Expand]Data Editing
    [Expand]Data Presentation
    [Collapse]Export and Printing
      Examples: Export and Printing
      How to: Customize Print Settings When Printing GridControl
      How to: Customize the GridControl's Data-Aware Export Output
      How to: Export the View's Data in PDF Format
      How to: Print a Grid and Show its Print Preview
    [Expand]General Use
    [Expand]Layout View
    [Expand]Navigation and Selection
    [Expand]Skins and Look and Feel
    [Expand]Sorting and Grouping
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Customize the GridControl's Data-Aware Export Output

This example demonstrates how to customize the GridControl's data-aware export output.

In this example, the grid's data is exported to XLSX format using the GridControl.ExportToXlsx method with a parameter (an XlsxExportOptionsEx object) that regulates export settings and behavior. This parameter provides events that allow you to add a header and footer to the export output, to customize cells, add rows, etc. Note that you can perform similar customizations when exporting to other formats.

Export Methods Options Class
BaseView.ExportToCsv CsvExportOptionsEx
BaseView.ExportToXls XlsExportOptionsEx
BaseView.ExportToXlsx XlsxExportOptionsEx

To add a header displaying custom information to the generated MS Excel document, the XlsxExportOptionsEx.CustomizeSheetHeader event is handled. In the event handler, the AddRow, InsertImage and MergeCells methods of the event's ExportContext parameter are used to add rows with the company name and contact information, insert the company logo and merge specific cells. Cell formatting is specified using objects of the XlFormattingObject class.

The XlsxExportOptionsEx.CustomizeCell event is used to replace values in the Discontinued column with special symbols. The ColumnName event parameter allows recognizing the desired column. The Value parameter is utilized to substitute certain cell values. The Handled parameter is set to true to apply the changes made.

The XlsxExportOptionsEx.AfterAddRow event handler merges cells in rows that correspond to the grid's group rows using the ExportContext.MergeCells method. For group rows, the DataSourceRowIndex event parameter returns negative values. The current row in the export output is specified by the DocumentRow parameter.

To add a footer to the export document, the XlsxExportOptionsEx.CustomizeSheetFooter event is handled. In this event handler, two new rows are added to the output document by calling the AddRow method, and their formatting is specified using objects of the XlFormattingObject class.

The XlsxExportOptionsEx.CustomizeSheetSettings event is handled to specify export settings. The ExportContext.SetFixedHeader event method is used to anchor the output document's header to the top, and to set the fixed header height. The ExportContext.SetFixedHeader method is called to add the AutoFilter button to the document's cells corresponding to the grid column headers.

The following image demonstrates the resulting export output.

Is this topic helpful?​​​​​​​