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

ExportToExcel does not work for the RADGRID ?

4 Answers 221 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Smruti Ranjan
Top achievements
Rank 1
Smruti Ranjan asked on 13 Feb 2012, 11:29 AM
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.

4 Answers, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 13 Feb 2012, 10:52 PM
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!


0
Shinu
Top achievements
Rank 2
answered on 14 Feb 2012, 05:23 AM
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.
0
Smruti Ranjan
Top achievements
Rank 1
answered on 15 Feb 2012, 09:02 AM
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. :-)
0
Les
Top achievements
Rank 1
answered on 06 Jul 2012, 10:23 AM
It sounds like I have the same problem. RadGid in a RadPanel. Do you have some sample code for this?
Tags
Grid
Asked by
Smruti Ranjan
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Smruti Ranjan
Top achievements
Rank 1
Les
Top achievements
Rank 1
Share this question
or