Export certain rows from GridDataControl to excel
I have a GridDataControl that I'd like to export to excel, but only include a certain subset of rows from the grid. How can I do this?
Thanks,
Keller
Keller
SIGN IN To post a reply.
3 Replies
JG
Jai Ganesh S
Syncfusion Team
August 9, 2016 09:33 AM UTC
Hi Keller,
You can achieve your requirement for export the certain set of rows like below,
|
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GridRangeInfo range = GridRangeInfo.Rows(4, 8);
grid.ExportToExcel(range, "sample.xlsx", ExcelVersion.Excel2010);
System.Diagnostics.Process.Start("Sample.xlsx");
} |
In the above sample, we have exported only the 4 to 8 rows.
Regards,
Jai Ganesh S
Hi Keller,You can achieve your requirement for export the certain set of rows like below,
private void Button_Click_1(object sender, RoutedEventArgs e){GridRangeInfo range = GridRangeInfo.Rows(4, 8);grid.ExportToExcel(range, "sample.xlsx", ExcelVersion.Excel2010);System.Diagnostics.Process.Start("Sample.xlsx");}In the above sample, we have exported only the 4 to 8 rows.Regards,Jai Ganesh S
thank you, but what if you want to select a few rows based on the underlying data itself. For example, I have a grid that is bound to a list of People records. I'd like to export to excel the grid, but only include People that are male, so Gender = 'Male'.
JG
Jai Ganesh S
Syncfusion Team
August 10, 2016 01:47 PM UTC
Hi Keller,
You can achieve your requirement to Export the certain rows based on the condition by using the below code,
|
private void Button_Click_1(object sender, RoutedEventArgs e)
{
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Create(1); //We are using single workbook
IWorksheet sheet = workbook.Worksheets[0];
//In this case we are exporting to single ExcelSheet so we marked Worksheets as 0
var itemProperties = grid.Model.View.GetPropertyAccessProvider();
for (int i = 0; i < grid.VisibleColumns.Count; i++)
{
sheet.Range[1, i+1].Text = grid.VisibleColumns[i].MappingName;
}
var collection = grid.Model.View.Records.Where(x => (x.Data as BusinessObjects).EmployeeGender == "Male").ToList();
for (int i = 0; i < collection.Count; i++)
{
//Setting Excel cell height based on Grid Cell height
sheet.SetRowHeightInPixels(i + 2, grid.Model.RowHeights[i]);
var r = collection[i];
for (int j = 0; j < grid.VisibleColumns.Count; j++)
{
int width = Convert.ToInt32(grid.Model.ColumnWidths[j]); //Getting Grid Cell column width
sheet.SetColumnWidthInPixels(j + 1, width); //Setting Width for Excel cell
sheet.Range[i + 3, j + 1].Text =
itemProperties.GetValue(r.Data, grid.VisibleColumns[j].MappingName).ToString();
}
}
workbook.SaveAs("Sample.xls");
excelEngine.Dispose();
System.Diagnostics.Process.Start("Sample.xls");
} |
Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/GDCExportDemo-967039729.zip
In the above sample, we have exported the records that having the EmployeeGender=Male.
Regards,
Jai Ganesh S
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
KG Keller Grimmitt
- Aug 8, 2016 08:27 PM UTC
- Aug 10, 2016 01:47 PM UTC