Category / Section
How to export the grid with row headers and column headers in WinForms GridGroupingControl?
1 min read
The row and column headers of the WinForms GridGroupingControl can be exported to Excel by using Converteroption property. GroupingGridToExcel method helps to trigger the converting of data from grid to Excel.
C#
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Files(*.Xls)|*.Xls|Files(*.Xlsx)|*Xlsx";
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = ".Xls";
saveFileDialog.FileName = "Book1";
if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.CheckPathExists)
{
excelConverter.GroupingGridToExcel(this.gridGroupingControl1, saveFileDialog.FileName, ConverterOptions.ColumnHeaders);
if (MessageBox.Show("Do you wish to open the xls file now?", "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = saveFileDialog.FileName;
proc.Start();
}
}
}
VB
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "Files(*.Xls)|*.Xls|Files(*.Xlsx)|*Xlsx"
saveFileDialog.AddExtension = True
saveFileDialog.DefaultExt = ".Xls"
saveFileDialog.FileName = "Book1"
If saveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK AndAlso saveFileDialog.CheckPathExists Then
excelConverter.GroupingGridToExcel(Me.gridGroupingControl1, saveFileDialog.FileName, ConverterOptions.ColumnHeaders)
If MessageBox.Show("Do you wish to open the xls file now?", "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.FileName = saveFileDialog.FileName
proc.Start()
End If
End If
End Sub
The screenshot below illustrates the conversion of a grid to Excel
Samples:
C#: Export to Excel
VB: Export to Excel
Reference Link: Exporting