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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Export certain rows from GridDataControl to excel

Thread ID:

Created:

Updated:

Platform:

Replies:

125325 Aug 8,2016 08:27 PM UTC Aug 10,2016 01:47 PM UTC WPF 3
loading
Tags: GridDataControl
Keller Grimmitt
Asked On August 8, 2016 08:27 PM UTC

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

Jai Ganesh S [Syncfusion]
Replied On 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 


Keller Grimmitt
Replied On August 9, 2016 06:00 PM 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 


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'.  



Jai Ganesh S [Syncfusion]
Replied On 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"); 
} 
 
 
In the above sample, we have exported the records that having the EmployeeGender=Male. 
 
Regards, 
Jai Ganesh S 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

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

;