ExportToExcel does not work for the RADGRID ?

5 posts, 0 answers
  1. Smruti Ranjan
    Smruti Ranjan avatar
    22 posts
    Member since:
    Feb 2012

    Posted 13 Feb 2012 Link to this post

    Hi All,

    My requirement is to export the grid data into excell sheet and promt user to save this excell file on the click of a radgrid toolbar button.
    I have made my set ups for this feature to work at my end like mentioned below :-

    1) Added a new GridToolBar button "Export".
        
    <telerik:RadToolBarButton Text="Excel" ImageUrl="~/Images/excel.png" CommandName="ExportToExcel" Value="Export" />

    2) On page load i have added the code like below :-

    if (!Page.IsPostBack)
               {
                   rgMyGrid.ExportSettings.ExportOnlyData = true;
                   rgMyGrid.MasterTableView.UseAllDataFields = true;
                   rgMyGrid.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
                   rgMyGrid.ExportSettings.IgnorePaging = true;
                   rgMyGrid.ExportSettings.FileName = "CropList";
                   rgMyGrid.ExportSettings.OpenInNewWindow = true;
               }
    3) Handling the button click of the grid to export the data to Excell file, used code as below :
    Ex :-
    protected void rtbCommand_ButtonClick(object sender, RadToolBarEventArgs e)
           {
               if (e.Item.Value == "Export")
               {
                   ExportGrid();
               }
           }
    The ExportGrid method is as below :
    protected void ExportGrid()
           {
                   rgMyGrid.ExportSettings.ExportOnlyData = true;
                   rgMyGrid.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
                   rgMyGrid.ExportSettings.IgnorePaging = true;
                   rgMyGrid.ExportSettings.FileName = "CropList";
                   rgMyGrid.ExportSettings.OpenInNewWindow = true;
                   rgMyGrid.MasterTableView.ExportToExcel();               
               
           }

    Still it does not seems to be working rather it makes the toolbar to disappear when click on the EXPORT button. :-(

    Is this the right way i am going through or need to change the coding any way ?
    Please help me in this regard. Thanks in advance.
  2. jumpstart
    jumpstart avatar
    479 posts
    Member since:
    Nov 2011

    Posted 13 Feb 2012 Link to this post

    Smruti:

    You would find your task greatly simplified by taking advantage of the "Excel" CommandItem button functionality that is already a part of RadGrid.

    Default.aspx:
    <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AllowSorting="True"
        AllowPaging="True" PageSize="10" runat="server" GridLines="None" Width="95%"
        OnItemCommand="RadGrid1_ItemCommand">
        <ExportSettings HideStructureColumns="true" />
        <MasterTableView Width="100%" CommandItemDisplay="Top">
            <PagerStyle Mode="NextPrevNumericAndAdvanced" />
            <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true"
                ShowExportToCsvButton="true" />
        </MasterTableView>
    </telerik:RadGrid>

     Default.aspx.cs:
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
        {
            ConfigureExport();
        }
    }
     
    public void ConfigureExport()
    {
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true
    }


    Take a look at the Grid / MS Excel/MS Word/CSV online demo. It also demonstrates how to fire the export using external ASP:Button click events.

    Hope this helps!


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 13 Feb 2012 Link to this post

    Hello Smruthi,

    Check for the CommandName in ItemCommand event as shown below.
    C#:
    protected void grid_ItemCommand(object sender, GridCommandEventArgs e)
    {
      if (e.CommandName == "ExportToExcel")
      {
        ExportGrid();
      }
    }

    -Shinu.
  5. Smruti Ranjan
    Smruti Ranjan avatar
    22 posts
    Member since:
    Feb 2012

    Posted 15 Feb 2012 Link to this post

    Hello Shinu,

    Thanks for your efforts but, above did not worked for me.

    With a work around and going through the demo provided for the RadGrid Excel export i found the result to be working.

    The main issue was that i was using the RadAzaxPanel in which the rad grid was being put into. So i need to make the ajax false on the client side event "OnRequestStart" and again make it enable in the client side event "OnResponseEnd" handlers.

    Now it working fine for me. Thanks to the demo provided by the Telerik supprt team, this explains the use of RadGrid Export to Excell with RadAjaxPanel, RadAjaxScriptmanager...

    Hope this will be helpfull for others also facing the same kind of problems. :-)
  6. Les
    Les avatar
    29 posts
    Member since:
    Feb 2011

    Posted 06 Jul 2012 Link to this post

    It sounds like I have the same problem. RadGid in a RadPanel. Do you have some sample code for this?
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017