Grouped Grid - Export Excel - Remove Extra Column

3 posts, 1 answers
  1. Joe Hakooz
    Joe Hakooz avatar
    65 posts
    Member since:
    Dec 2007

    Posted 01 Jun 2009 Link to this post

    Hi there,
    Spent quite a while looking into this and haven't seen an answer yet...

    I have a "simple" grouped grid. When I export it to Excel, there is an extra empty column (A). How can I remove this first column from excel.

    Here is what I've tried...
    I assume the empty column is a combination of GridGroupSplitterColumn and Header Column. Using the following code...

    Protected Sub btnExport_Click(ByVal sender As ObjectByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click  
            RadGrid1.ExportSettings.ExportOnlyData = True 
            RadGrid1.ExportSettings.IgnorePaging = True 
            RadGrid1.ExportSettings.OpenInNewWindow = True 
            RadGrid1.MasterTableView.ExportToExcel()  
    End Sub 
     
    Protected Sub RadGrid1_ExcelExportCellFormatting(ByVal source As ObjectByVal e As Telerik.Web.UI.ExcelExportCellFormattingEventArgs)  
            If TypeOf e.FormattedColumn Is GridGroupSplitterColumn Then 
                e.Cell.Visible = False 
            End If 
    End Sub 

    ...I am able to remove many of the blank cells. But the header row and grouped rows still have an empty cell on the left. So again, my question is, how can I remove all empty cells in that first column (A) when exporting a grouped grid to Excel?

    Thanks for your help...
    Joe
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Jun 2009 Link to this post

    Hello Joe,

    You can try out the following code to remove the empty cells in the Header row and GroupHeader rows while exporting:
    c#:
       Protected Sub btnExport_Click(ByVal sender As ObjectByVal e As EventArgs) 
           TryCast(RadGrid1.MasterTableView.GetItems(GridItemType.Header)(0), GridHeaderItem).Cells(0).Visible = False 
           For Each gpheaderItem As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader) 
                   
               gpheaderItem.Cells(0).Visible = False 
           Next 
            
           RadGrid1.ExportSettings.IgnorePaging = True 
           RadGrid1.ExportSettings.ExportOnlyData = True 
           RadGrid1.ExportSettings.OpenInNewWindow = True 
           RadGrid1.MasterTableView.ExportToExcel() 
       End Sub 
     
     

    Thanks
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Joe Hakooz
    Joe Hakooz avatar
    65 posts
    Member since:
    Dec 2007

    Posted 02 Jun 2009 Link to this post

    Princy,
    Your code did not do the trick in the btn_Export Sub, but I moved it to the ExcelExportCellFormatting Sub and it worked great.
    Thanks for your help!

    Here is the new code that gets rid of the first "empty" column for a grouped grid export...


    Protected
     Sub btnExport_Click(ByVal sender As ObjectByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExport.Click  
            RadGrid1.ExportSettings.ExportOnlyData = True 
            RadGrid1.ExportSettings.IgnorePaging = True 
            RadGrid1.ExportSettings.OpenInNewWindow = True 
            RadGrid1.MasterTableView.ExportToExcel()  
    End Sub 
     
    Protected Sub RadGrid1_ExcelExportCellFormatting(ByVal source As ObjectByVal e As Telerik.Web.UI.ExcelExportCellFormattingEventArgs)  
            TryCast(RadGrid1.MasterTableView.GetItems(GridItemType.Header)(0), GridHeaderItem).Cells(0).Visible = False 
            For Each gpheaderItem As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)  
                gpheaderItem.Cells(0).Visible = False 
            Next 
            If TypeOf e.FormattedColumn Is GridGroupSplitterColumn Then 
                e.Cell.Visible = False 
            End If 
            ' This will resize the column in Excel  
            If e.FormattedColumn.UniqueName = "myUniqueColumnName" Then 
                e.Cell.Width = Unit.Pixel(200)  
            End If 
        End Sub 
     
Back to Top