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

[Solved] How to export grid to MS Excel?

1 Answer 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Goran
Top achievements
Rank 1
Goran asked on 07 Feb 2013, 11:43 AM
Hi,

We use Telerik grid for VS2008, version 2011.3.1115.0.

We load grid dynamically into our user control that wrapped Telerik grid. There are master and content pages and our user control is placed on content page.

I need to export grid data to MS Excel file and save it to web server. There are 2 options. I can choose between exporting whole grid or selected rows.

How can I do that?

Thank you in advance.

Goran

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 15 Feb 2013, 11:15 AM
Hi Goran,

You can have a condition before exporting that whether you want to export entire grid or selected rows. Please have a look into the following code which is doing exporting based on these condition. [Here I am using an external button for exporting]

ASPX:
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
           <asp:ListItem Text="Export All" Selected="True"></asp:ListItem>
           <asp:ListItem Text="Export Selected"></asp:ListItem>
       </asp:RadioButtonList>
       <asp:Button ID="Button1" Width="150px" Text="Export to Excel" runat="server" OnClick="ExporttoExcel_Click">
       </asp:Button>

C#:
protected void ExporttoExcel_Click(object sender, EventArgs e)
   {
       if (RadioButtonList1.Items[1].Selected)
       {
           foreach (GridDataItem item in RadGrid1.Items)
           {
               if(!item.Selected)
                 item.Visible = false;
           }
       }
       RadGrid1.ExportSettings.ExportOnlyData = true;
       RadGrid1.ExportSettings.OpenInNewWindow = true;
       RadGrid1.MasterTableView.ExportToExcel();
   }

Thanks,
Princy.
Tags
Grid
Asked by
Goran
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or