Articles in this section
Category / Section

How to export a selected range of WinForms GridControl to Excel?

1 min read

Excel export

You can export selected range of cells to excel by using ExportRange method. In this example, the selected range is passed as the parameter to the ExportRange method.

C#

private void button1_Click(object sender, System.EventArgs e)
{
     Syncfusion.GridExcelConverter.GridExcelConverterControl gecc = new Syncfusion.GridExcelConverter.GridExcelConverterControl();
     SaveFileDialog saveFileDialog = new SaveFileDialog();
     saveFileDialog.Filter = "Files(*.xls)|*.xls";
     saveFileDialog.DefaultExt = ".xls";
     GridRangeInfoList list = this.gridControl1.Model.SelectedRanges;
     if (saveFileDialog.ShowDialog() == DialogResult.OK)
     {
        foreach (GridRangeInfo range in list)
        {
          gecc.ExportRange(range, this.gridControl1.Model, saveFileDialog.FileName,
                         Syncfusion.GridExcelConverter.ConverterOptions.RowHeaders);
        }
        if (MessageBox.Show("Do you wish to open the xls file now?", "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
           Process proc = new Process();
           proc.StartInfo.FileName = saveFileDialog.FileName;
           proc.Start();
        }
     }
}

 

VB

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  Dim gecc As New Syncfusion.GridExcelConverter.GridExcelConverterControl()
  Dim saveFileDialog As New SaveFileDialog()
  saveFileDialog.Filter = "Files(*.xls)|*.xls"
  saveFileDialog.DefaultExt = ".xls"
  Dim list As GridRangeInfoList = Me.gridControl1.Model.SelectedRanges
  If saveFileDialog.ShowDialog() = DialogResult.OK Then
  For Each range As GridRangeInfo In list
    gecc.ExportRange(range, Me.gridControl1.Model, saveFileDialog.FileName, Syncfusion.GridExcelConverter.ConverterOptions.RowHeaders)
  Next range
  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 Process()
     proc.StartInfo.FileName = saveFileDialog.FileName
     proc.Start()
  End If
  End If
End Sub

Show the Export to excel option in grid

Grid data moves to the excel sheet

Samples:

C#: ExportingSelectedCells-C#.

VB: ExportingSelectedCells-VB.

Reference link: https://help.syncfusion.com/windowsforms/grid-control/exporting#excel-export

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied