)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to export two grids in a single worksheet of a workbook in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: May 11, 2018 |
Last Revised Date: December 23, 2019

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

Export multiple grids

Export GridGroupingControl to Excel sheet

 

Samples:

C#: Export multiple grid_CS

VB: Export multiple grid_VB

Reference link: https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/exporting#excel-export

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon