Export To Excel from RadGrid

13 posts, 1 answers
  1. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 05 Nov 2009 Link to this post

    (Q2 2009 version)
    Hi, i'm triying to export to excel from my radgrid from buttons on <commandItemTemplate>  tags like this

    <MasterTableView CommandItemDisplay="Top" CellSpacing="0" ShowGroupFooter="true" AllowMultiColumnSorting="true" ShowFooter="true" DataKeyNames="NombreTipoCuenta" >                                                                                     
                                            <CommandItemTemplate> 
                                                <asp:ImageButton runat="server" ID="btnExcelExport" ImageUrl="~/images/page_excel.png" OnClick="btnExcelExport_Click" AlternateText="Excel" ToolTip="Excel" />  
     
                                                                                                                                    
                                            </CommandItemTemplate> 

    but when i click on my button, commanditem hides, and doesn`t work , when i copy this outside the grid it works

    <asp:ImageButton runat="server" ID="btnExcelExport" OnClick="btnExcel_Click" ImageUrl="~/images/page_excel.png"  AlternateText="Excel" />  
                                     <telerik:RadGrid   
                                                AutoGenerateColumns="False"  
                                                ID="tk_Grid"  
                                                AllowPaging="True"  
                                                PageSize="20"  
                                                runat="server"  
                                                GridLines="None" 
                                                ShowFooter="false"  
                                                ShowGroupPanel="false" 
                                                style="margin-top: 0px"  
                                                BorderStyle="None"                                              
                                                CssClass="Grid" onitemdatabound="tk_Grid_ItemDataBound" Skin="Default" 
                                                >                                                                      

    this is the code behind for both mehods
     protected void btnExcelExport_Click(object sender, ImageClickEventArgs e) 
        { 
     
            tk_Grid.ExportSettings.ExportOnlyData = true
            tk_Grid.ExportSettings.IgnorePaging = true
            tk_Grid.ExportSettings.OpenInNewWindow = false
            tk_Grid.MasterTableView.ExportToExcel(); 
        } 

    thanks, Gino.




  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Nov 2009 Link to this post

    Hello Gino,

    Try setting the CommandName of the ImageButton when it is placed in the CommandItemTemplate and see if it helps:
    aspx:
    <CommandItemTemplate>  
          <asp:ImageButton runat="server" ID="btnExcelExport" ImageUrl="~/images/page_excel.png" CommandName="ExportToExcel" AlternateText="Excel" ToolTip="Excel" />                          
    </CommandItemTemplate>  

    c#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.ExportToExcelCommandName) 
            { 
                RadGrid1.ExportSettings.ExportOnlyData = true
                RadGrid1.ExportSettings.IgnorePaging = true
                RadGrid1.ExportSettings.OpenInNewWindow = false
            } 
        } 

    Hope this helps.
    Princy.
  3. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 06 Nov 2009 Link to this post

    Hello Princy, thanks for your answer but it doesn`t work too, occurs the same thing, buttons on CommandItemTemplate hides and don`t show the export dialog.

    Regards Gino.
  4. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 06 Nov 2009 Link to this post

    Hello Gino,

    I am sending you a simple working project which handles the desired functionality. Please give ita try and let me know if it works for you.

    I hope this helps.

    Kind regards,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 10 Nov 2009 Link to this post

    Hello Pavlina,

    I try your sample app, and it works, thanks, however, after the first exceution of the export method my links items in the RadGrid doesn`t work and i'm force to reload to restore the functionality. Code below shows how i populate my grid and turn each items into links. I hope you can help me, Thanks.

    protected void tk_Grid_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
    if (e.Item is GridDataItem) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item; 
     
     
                HyperLink h0 = new HyperLink();             
                currUrl = "frmAnexoBalance.aspx?Periodo=APERTURA&MesID=0&TCID=0&GGPPID=" + dataItem["FormatoGananciaPerdidaID"].Text.Trim() + "&FBID=0"; 
                h0.NavigateUrl = "#"
                if (double.Parse(dataItem["Apertura"].Text) == 0) dataItem["Apertura"].Text = ""
                h0.Text = dataItem["Apertura"].Text; 
                h0.CssClass = "gridtext"
                h0.Attributes.Add("OnClick", "return AbreVentana('" + currUrl + "','SGH Perú');"); 
                dataItem["Apertura"].Controls.Add(h0); 

  6. Answer
    Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 11 Nov 2009 Link to this post

    Hello Gino,

    Based on this information, it is hard to determine what is causing the unwanted behavior. If the issue persists, you can open a formal support ticket, and send us the problematic code, for further investigation.

    Sincerely yours,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 11 Nov 2009 Link to this post

    Hi Pavlina,

    I think this problem have a relation with the ajax-enable=false 'cause is the new code i use accords your sample, however i open a formal support ticket, thanks.

    Best wishes
    Gino
  8. THANGARAI
    THANGARAI avatar
    21 posts
    Member since:
    Nov 2013

    Posted 03 Dec 2013 Link to this post

     <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="Images/Excel_ExcelML.png"
                OnClick="ImageButton2_Click" AlternateText="ExcelML" />
    ------------------------------------------
    Protected
    Sub ImageButton2_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton2.Click
               
               Rg_StdCompReferrals.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML 
             Rg_StdCompReferrals.ExportSettings.IgnorePaging = True
               Rg_StdCompReferrals.ExportSettings.ExportOnlyData = True
               Rg_StdCompReferrals.ExportSettings.OpenInNewWindow = True
               Rg_StdCompReferrals.MasterTableView.ExportToExcel()
           End Sub
     
    Hi princy,
    Export to excel is not working.
    Please help me to overcome this problem
  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Dec 2013 Link to this post

    Hi THANGARAI,

    I guess your grid might be Ajaxified. The exporting feature works only with regular postbacks. This means, that the asynchronous postback should be canceled when performing an export. More information can be found here. Please try the following code snippet.

    ASPX:
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="Rg_StdCompReferrals">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="Rg_StdCompReferrals"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

    JS:
    <script type="text/javascript">
     function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("ImageButton2") >= 0) {
            args.set_enableAjax(false);
        }
     }
    </script>

    Thanks,
    Princy

  10. THANGARAI
    THANGARAI avatar
    21 posts
    Member since:
    Nov 2013

    Posted 03 Dec 2013 Link to this post

    Hi princy,
    Thank you so much.  Its working great.
  11. Kalai
    Kalai avatar
    6 posts
    Member since:
    May 2019

    Posted 27 Aug Link to this post

    Hi,

    I am trying export radgrid to xls file. File export is working fine. If the radgrid data row count <= 5 , xls file need to be export. If the row count > 5 , restrict to export file. I cannot able to restrict to download file. PFA code

  12. Attila Antal
    Admin
    Attila Antal avatar
    429 posts

    Posted 29 Aug Link to this post

    Hi Kalai,

    My first advise is to use Advanced Data Binding with NeedDataSource event to bind data to the Grid. We also have an article addressing this: How to bind RadGrid properly on server-side

    Also, when clicking on the export button, that fires the ExportCommand, which can be captured in the ItemCommand Event. If this event is not canceled, the command will happen any ways. Calling the ExportToExcel() method inside the ItemComand will basically tell the grid to export during exporting.

    Once the Binding is done, all you need to do in the command item is count the Grid's items and if the number is more than 5 cancel the command.

    Example:

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if(e.CommandName == RadGrid.ExportToExcelCommandName)
        {
            // disable paging to get all the records
            RadGrid1.AllowPaging = false;
            // rebind the grid for the changes to take effect
            RadGrid1.Rebind();
    
            // count the number of records
            var rowCount = RadGrid1.Items.OfType<GridDataItem>().Count();
            // condition if the records are more than 5
            if(rowCount > 5)
            {
                // cancel the original command
                e.Canceled = true;
            }
        }
    }

    I hope this will help resolve the issue.

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  13. Kalai
    Kalai avatar
    6 posts
    Member since:
    May 2019

    Posted 04 Sep in reply to Attila Antal Link to this post

    Thanks Attila.
Back to Top