Link to home
Start Free TrialLog in
Avatar of beginner_help
beginner_help

asked on

How to export data in telerik RadGrid to Excel spreadsheet?

I am using telerik RadGrid in ASP.net page, I would like to export the data in this RadGrid to open or save into excel spreadsheet on button click.
Can anyone help in doing this.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of binaryevo
binaryevo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beginner_help
beginner_help

ASKER

I have already tried those links previously, I am not sure why it not working for me.
Below is the code I am working on, please let me know the required changes to be made.
ASPX CODE:
----------
<telerik:RadGrid AllowPaging="true" ID="grdQueues" runat="server" OnSelectedIndexChanged="grd_SelectedIndexChanged"  
                        ShowStatusBar="true" AllowSorting="true" PageSize="10" AutoGenerateColumns="false" 
                        OnPageIndexChanged="grd_PageIndexChanged" OnNeedDataSource="grd_NeedDataSource"
                        Width="100%" Visible="false" OnExcelMLExportStylesCreated="grd_ExcelMLExportStylesCreated"
                        OnExcelMLExportRowCreated="grd_ExcelMLExportRowCreated">             
                    <PagerStyle AlwaysVisible="true" Mode="NextPrevAndNumeric" PagerTextFormat="Change page: {4} | Displaying page {0} of {1} |, items {2} to {3} of {5}." />
                    <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
                        <Selecting AllowRowSelect="true" />
                        <Scrolling AllowScroll="true" UseStaticHeaders="true" />                        
                    </ClientSettings>
                    <MasterTableView ShowHeadersWhenNoRecords="true" EditMode="PopUp">      
                        <NoRecordsTemplate><div align="center">
                                No Records to Display
                            </div>
                        </NoRecordsTemplate>
                        <Columns>
                            <telerik:GridTemplateColumn HeaderText="Acct Nbr">
                                <ItemTemplate>
                                    <asp:Label ID="lblNbr" ClientIDMode="Static" runat="server" Text='<%# Bind("NBR") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="Center" Font-Bold="true" />
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="Customer Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblName" ClientIDMode="Static" runat="server" Text='<%# Bind("NAME") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                                    <HeaderStyle HorizontalAlign="Center" Font-Bold="true" />
                            </telerik:GridTemplateColumn>                            
                        </Columns>                       
                    </MasterTableView>                      
                    </telerik:RadGrid>

--------------------------
ASPX.CS CODE
-----------------------------
protected void btnExport_Click(object sender, EventArgs e)
        {
            grd.Rebind();
            grd.ExportSettings.ExportOnlyData = true;
            grd.ExportSettings.IgnorePaging = true;
            grd.ExportSettings.OpenInNewWindow = true;
            grd.MasterTableView.NestedViewTemplate = null;
            grd.MasterTableView.ExportToExcel();
        }

        protected void grd_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)
        {
            if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.DataRow)
            {
                if (e.Row.Cells[0] != null && ((string)e.Row.Cells[0].Data.DataItem).Contains("U"))
                {
                    e.Row.Cells[0].StyleValue = "MyCustomStyle";
                }
            }
        }

        protected void grd_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)
        {
            foreach (Telerik.Web.UI.GridExcelBuilder.StyleElement style in e.Styles)
            {
                if (style.Id == "headerStyle")
                {
                    style.FontStyle.Bold = true;
                    style.FontStyle.Color = System.Drawing.Color.Gainsboro;
                    style.InteriorStyle.Color = System.Drawing.Color.Wheat;
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
                }
                else if (style.Id == "itemStyle")
                {
                    style.InteriorStyle.Color = System.Drawing.Color.WhiteSmoke;
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
                }
                else if (style.Id == "alternatingItemStyle")
                {
                    style.InteriorStyle.Color = System.Drawing.Color.LightGray;
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
                }
            }

            Telerik.Web.UI.GridExcelBuilder.StyleElement myStyle = new Telerik.Web.UI.GridExcelBuilder.StyleElement("MyCustomStyle");
            myStyle.FontStyle.Bold = true;
            myStyle.FontStyle.Italic = true;
            myStyle.InteriorStyle.Color = System.Drawing.Color.Gray;
            myStyle.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;
            e.Styles.Add(myStyle);
        }

Open in new window

Well one thing thats sticks out initially is you are calling grd.Rebind()... Why is this being called?   Disable all the extra methods and take them out of your aspx code and on your btnExport_Click event try to add just the following code:

grd.MasterTableView.ExportToExcel();

Open in new window


Test this out and see if you can get it to work without all the extras.
Initially I did not have grd.Rebind() method, when It did not work I added it to the button click method, but still it is not working
Those link helped me partially, I figured out what the problem was by myself.