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. Image for the cookie policy date
close icon

Need to export selected rows from grid to excel

I am using syncfusion 4.2 & export gridcontrol to excel using

GridExcelConverterControl.GridToExcel(this.mGrid.Model, filename as string, Syncfusion.GridExcelConverter.ConverterOptions.RowHeaders);

But this method expects either grid control or GridModel. Is there any way I can only export only selected rows to excel.

Thanks,
Sachin

6 Replies

AD Administrator Syncfusion Team February 20, 2007 11:41 PM UTC

Hi Sachin,

There is no built-in support for this. If you want to export the selected cells to excel, you need to derive the GridExcelConverterControl class and implement the SelectedRowExport method. Please refer to the attached sample for more details.

public void SelectedCellsExport(GridModel model,string FileName)
{
ExcelEngine engine = CreateEngine();
IWorkbook book = engine.Excel.Workbooks.Create( 1 );
IWorksheet sheet = book.Worksheets[ 0 ];
GridRangeInfoList list = model.SelectedRanges;
foreach( GridRangeInfo range in list)
{
switch( range.RangeType)
{
case GridRangeInfoType.Cells:
ExportCells(range,model,sheet);
break;
case GridRangeInfoType.Cols:
ExportCells(GridRangeInfo.Cells(1,range.Left,model.RowCount,range.Right),model,sheet);
break;
case GridRangeInfoType.Rows:
ExportCells(GridRangeInfo.Cells(range.Top,1,range.Bottom,model.ColCount),model,sheet);
break;
}
}
book.SaveAs(FileName);
book.Close();
}
public void ExportCells(GridRangeInfo range, GridModel model, IWorksheet sheet)
{
for(int i = range.Top, iRowRange = 1 ;i<= range.Bottom;i++,iRowRange++)
{
for(int j = range.Left,iColRange = 1 ;j<= range.Right;j++,iColRange++)
{
this.CopyStyle( model[i,j],sheet[iRowRange,iColRange]);
sheet[iRowRange,iColRange].Text = model[i,j].FormattedText;
}
}
}

Sample :GDBGExport.zip

Best regards,
Haneef


AD Administrator Syncfusion Team February 21, 2007 05:38 PM UTC

Hi Hannef,

I am able to export selected rows. Thanks for your help. But I could not export row header now . As it was possible earlier with

gecc.GridToExcel(this.mGrid.Model, filename as string, Syncfusion.GridExcelConverter.ConverterOptions.RowHeaders);

Can I specify ConverterOptions also with the fix given by you.

Also there is a problem when i exceeds rows larger than 65536 which is the largest rows supported by excel.

Thanks,
Sachin

>Hi Sachin,

There is no built-in support for this. If you want to export the selected cells to excel, you need to derive the GridExcelConverterControl class and implement the SelectedRowExport method. Please refer to the attached sample for more details.

public void SelectedCellsExport(GridModel model,string FileName)
{
ExcelEngine engine = CreateEngine();
IWorkbook book = engine.Excel.Workbooks.Create( 1 );
IWorksheet sheet = book.Worksheets[ 0 ];
GridRangeInfoList list = model.SelectedRanges;
foreach( GridRangeInfo range in list)
{
switch( range.RangeType)
{
case GridRangeInfoType.Cells:
ExportCells(range,model,sheet);
break;
case GridRangeInfoType.Cols:
ExportCells(GridRangeInfo.Cells(1,range.Left,model.RowCount,range.Right),model,sheet);
break;
case GridRangeInfoType.Rows:
ExportCells(GridRangeInfo.Cells(range.Top,1,range.Bottom,model.ColCount),model,sheet);
break;
}
}
book.SaveAs(FileName);
book.Close();
}
public void ExportCells(GridRangeInfo range, GridModel model, IWorksheet sheet)
{
for(int i = range.Top, iRowRange = 1 ;i<= range.Bottom;i++,iRowRange++)
{
for(int j = range.Left,iColRange = 1 ;j<= range.Right;j++,iColRange++)
{
this.CopyStyle( model[i,j],sheet[iRowRange,iColRange]);
sheet[iRowRange,iColRange].Text = model[i,j].FormattedText;
}
}
}

Sample :GDBGExport.zip

Best regards,
Haneef


AD Administrator Syncfusion Team February 21, 2007 08:44 PM UTC

Hi Sachin,

Here is a minimal sample that shows you "How to export the selected cell ranges with column header in the grid to excel?". Please try the attached sample and let me know if this helps.

sample : GDBGExportRowHeader.zip

Best regards,
Haneef


SB Sachin Bamel February 21, 2007 11:22 PM UTC

Hi Haneef,

Thanks for your fix. I will appreciate if you can help me to export selected rows from grouping grid also.

Thanks,
Sachin



AD Administrator Syncfusion Team February 22, 2007 08:25 PM UTC

Hi Sachin,

Here is a minimal sample that shows you "How to export the selected rows in a GroupingGrid to Excel?". Please refer the sample and let me know if this helps.
GGCSelecterRow_ExcelExport.zip

Best regards,
Haneef


AD Administrator Syncfusion Team February 23, 2007 07:43 PM UTC

Hi Haneef,

This solution works when grid is expanded. Is it possible to do multiple selection when the grid is collapsed & export selected rows.

Thanks,
Sachin

Loader.
Live Chat Icon For mobile
Up arrow icon