Category / Section
How to export two grids in a single worksheet of a workbook in WinForms GridGroupingControl?
2 mins read
Excel export
By default, the GridGroupingControl does not have direct support to export multiple grids as same as in view (with styles and formats) into a single worksheet in the workbook. To perform this, create a custom GridGroupingExcelConverterControl class. Set proper value to the ExcelStartRowIndex property of the worksheet in the ExportTable overridden method.
C#
//Creating custom class public class GridGroupingExcelConverterControlEXT: GridGroupingExcelConverterControl { private int rowIndex = 1; public GridGroupingExcelConverterControlEXT() { } public int ExcelStartRowIndex { get { return rowIndex; } set{ rowIndex = value; } } protected override void ExportTable(IList tableElements, GridTableDescriptor tableDescriptor, ref int excelRowIndex, int excelStartColumnIndex,bool isNestedTable,IWorksheet sheet,ExcelExportingOptions exportingOptions) { excelRowIndex = ExcelStartRowIndex; base.ExportTable(tableElements, tableDescriptor, ref excelRowIndex, excelStartColumnIndex, isNestedTable, sheet, exportingOptions); } } //Exporting to Excel private void exportBtn_Click(object sender, EventArgs e) { ExcelEngine Engine = new ExcelEngine(); IWorkbook workbook = Engine.Excel.Workbooks.Create(1); IWorksheet sheet = workbook.Worksheets[0]; sheet.Name = "sample"; ExcelExportingOptions options = new ExcelExportingOptions(); GridGroupingExcelConverterControlEXT converter = new GridGroupingExcelConverterControlEXT(); //Exporting 1st grid converter.ExportToExcel(gridGroupingControl1, sheet, options); //Specifying the starting row index of 2nd grid converter.ExcelStartRowIndex = sheet.UsedRange.LastRow + 2; //Exporting 2nd grid converter.ExportToExcel(gridGroupingControl2, sheet, options); workbook.SaveAs("Sample.xls"); Process.Start("Sample.xls"); }
VB
'Creating custom class Public Class GridGroupingExcelConverterControlEXT Inherits GridGroupingExcelConverterControl Private rowIndex As Integer = 1 Public Sub New() End Sub Public Property ExcelStartRowIndex() As Integer Get Return rowIndex End Get Set(ByVal value As Integer) rowIndex = value End Set End Property Protected Overrides Sub ExportTable(ByVal tableElements As IList, ByVal tableDescriptor As GridTableDescriptor, ByRef excelRowIndex As Integer, ByVal excelStartColumnIndex As Integer, ByVal isNestedTable As Boolean, ByVal sheet As IWorksheet, ByVal exportingOptions As ExcelExportingOptions) excelRowIndex = ExcelStartRowIndex MyBase.ExportTable(tableElements, tableDescriptor, excelRowIndex, excelStartColumnIndex, isNestedTable, sheet, exportingOptions) End Sub End Class 'Exporting to Excel Private Sub exportBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles exportBtn.Click Dim Engine As New ExcelEngine() Dim workbook As IWorkbook = Engine.Excel.Workbooks.Create(1) Dim sheet As IWorksheet = workbook.Worksheets(0) sheet.Name = "sample" ' save to workbook in excel Dim options As New ExcelExportingOptions() Dim converter As New GridGroupingExcelConverterControlEXT() ‘Exporting 1st grid converter.ExportToExcel(gridGroupingControl1, sheet, options) ‘Specifying the starting row index of 2nd grid converter.ExcelStartRowIndex = sheet.UsedRange.LastRow + 2 ‘Exporting 2nd grid converter.ExportToExcel(gridGroupingControl2, sheet, options) workbook.SaveAs("Sample.xls") Process.Start("Sample.xls") End Sub
Screenshot
Samples:
Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/exporting#excel-export