$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView WinForms Control
Export grid data to excel, CSV, HTML, and PDF files in C# WinForms Applications.
Home > WinForms UI Controls > User Manual > Export Grid Data to Excel in C#

GridView Data Exporting to Excel in C#.NET

The Kettic GridView .NET component provides the support of exporting data from grid to Excel file. This feature needn’t the MS Office installation, however, allows the exported Excel file being read in MS Excel spreadsheet 2003 and higher versions. To export the data to the Excel, we need to use the KetticData.dll assembly and include the C# namespaces to access to the types contained in KetticData, like Kettic.WinForms.Data and Kettic.WinForms.UI.Export

How to Export Data from Grid to Excel in C#

To export the data from grid to Excel, we need to use the ExcelML format, which hold the exported data. So we need to initialize the ExportToExcelML C# class first. And the constructor will take the parameter, KetticGridView. The simple C# code below shows that how to initialize the ExportToExcelML object.

ExportToExcelML exporter = new ExportToExcelML(this.ketticGridView1);
After initializing the ExportToExcelML C# class, we can use the following options to determine how to export data: ExportAlways, DoNotExport, and ExportAsHidden. The default setting is the ExportAsHidden, which allows the users to hide the columns and rows. These options can be achieved through HiddenColumnOption and HiddenRowOption properties. If the users do not want to export the hidden columns or rows, they can set the two properties to DoNotExport. The C# code below is to set the hidden column option.

exporter.HiddenColumnOption = Kettic.WinForm.UI.Export.HiddenOption.DoNotExport;
The ExcelML method is able to export the themes to the Excel file. This feature is enabled through changing the value of EnableAternativeRow property to true. The Kettic GridView is able to export all conditional formatting to the Excel file. To enable the functionality of exporting visual settings, set the value of ExportVisualSettings property to ture.

exporter.ExportVisualSettings = true;
The Kettic GridView control provides the support of splitting data on separate sheets when the number of rows is larger than Excel maximum. This is accomplished by setting the SheetMaxRows property. The following C# code demonstrates how to achieve this.

exporter.SheetMaxRows = ExcelMaxRows._1048576;
There is a SheetName property available to the users changing the name of the MS Excel Sheet. When the data exceeds a single sheet, the export method adds index to the names of the new sheet. The following is the C# code to set the SheetName property

exporter.SheetName = "NewSheet";
The Kettic GridView .NET control offers the SummariesExportOption property to specify how to export summary items. There are four options available for selecting: ExportAll, ExportOnlyTop, ExportOnlyBottom, and DoNotExport. The default setting is ExportAll. The C# code below shows how to set the SummariesExportOption property.

exporter.SummariesExportOption = SummariesOption.DoNotExport;
After setting the above properties, we can export the data from grid to Excel now. To export data from grid to Excel, we need to use the ApplyExport method included in the ExportToExcelML object. Its valid parameter includes fileName which define the name of the exported file.

string fileName = "C:\\ExportedGridData1.xls";
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls