How to export the grid with customized background to excel in WinForms GridGroupingControl?
Exporting
The customizing color elements such as borders, back color, etc., and data are exported from the WinForms GridGroupingControl into an Excel sheet for which you can use the GroupingGridToExcel method in the GroupingGridExcelConverterControl Class. The SaveFileDialog Class prompts you to select the location for saving a file.
C#
private void bt_export_Click(object sender, System.EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Files(*.XLS)|*.XLS";
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = ".XLS";
if(saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.CheckPathExists)
{
GroupingGridExcelConverterControl converter = new GroupingGridExcelConverterControl();
//ExportStyle
converter.ExportBorders = true;
//set the backcolor of the header
converter.HeaderBackColor = Color.Aqua;
//set the backcolor of the caption
converter.CaptionBackColor = Color.MistyRose;
converter.ExportPreviewRows = true;
//export the plus/minus icons
converter.ExportGroupPlusMinus = true;
converter.ExportRecordPlusMinus = true;
ConverterOptions options = ConverterOptions.Default;
converter.GroupingGridToExcel(this.gridGroupingControl1, saveFileDialog.FileName, options);
converter.Dispose();
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 bt_export_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "Files(*.XLS)|*.XLS"
saveFileDialog.AddExtension = True
saveFileDialog.DefaultExt = ".XLS"
If saveFileDialog.ShowDialog() = DialogResult.OK AndAlso saveFileDialog.CheckPathExists Then
Dim converter As New GroupingGridExcelConverterControl()
'ExportStyle
converter.ExportBorders = True
'set the backcolor of the header
converter.HeaderBackColor = Color.Aqua
'set the backcolor of the caption
converter.CaptionBackColor = Color.MistyRose
converter.ExportPreviewRows = True
'export the plus/minus icons
converter.ExportGroupPlusMinus = True
converter.ExportRecordPlusMinus = True
Dim options As ConverterOptions = ConverterOptions.Default
converter.GroupingGridToExcel(Me.gridGroupingControl1, saveFileDialog.FileName, options)
converter.Dispose()
If MessageBox.Show("Do you wish to open the xls file now?", "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.FileName = saveFileDialog.FileName
proc.Start()
End If
End If
End SubSamples:
C#: Export
VB: Export