This is a migrated thread and some comments may be shown as answers.

Export To Excel from RadGrid

12 Answers 502 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gino Suarez
Top achievements
Rank 1
Gino Suarez asked on 05 Nov 2009, 11:48 PM
(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.




12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 06 Nov 2009, 06:04 AM
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.
0
Gino Suarez
Top achievements
Rank 1
answered on 06 Nov 2009, 03:12 PM
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.
0
Pavlina
Telerik team
answered on 06 Nov 2009, 03:35 PM
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.
0
Gino Suarez
Top achievements
Rank 1
answered on 10 Nov 2009, 03:06 PM
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); 

0
Accepted
Pavlina
Telerik team
answered on 11 Nov 2009, 12:32 PM
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.
0
Gino Suarez
Top achievements
Rank 1
answered on 11 Nov 2009, 02:31 PM
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
0
THANGARAI
Top achievements
Rank 1
answered on 03 Dec 2013, 10:22 AM
 <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
0
Princy
Top achievements
Rank 1
answered on 03 Dec 2013, 10:43 AM
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

0
THANGARAI
Top achievements
Rank 1
answered on 03 Dec 2013, 12:30 PM
Hi princy,
Thank you so much.  Its working great.
0
Kalai
Top achievements
Rank 1
answered on 27 Aug 2019, 05:59 AM

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

0
Attila Antal
Telerik team
answered on 29 Aug 2019, 05:21 PM

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.
0
Kalai
Top achievements
Rank 1
answered on 05 Sep 2019, 04:11 AM
Thanks Attila.
Tags
Grid
Asked by
Gino Suarez
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Gino Suarez
Top achievements
Rank 1
Pavlina
Telerik team
THANGARAI
Top achievements
Rank 1
Kalai
Top achievements
Rank 1
Attila Antal
Telerik team
Share this question
or