Solved

Why is my export not working when I try to export data from TELERIK RADGRID to EXCEL?

Posted on 2011-10-30
4
1,584 Views
Last Modified: 2013-11-25
I trying to export data in TELERIK RADRGID to EXCEL SPREADSHEET on button click.
I tried below code but it not showing the OPEN window for me to SAVE/OPEN the excel sheet.
Below is my code.
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.ExportSettings.ExportOnlyData = true;
            grd.ExportSettings.IgnorePaging = true;
            grd.ExportSettings.OpenInNewWindow = true;
            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

0
Comment
Question by:beginner_help
4 Comments
 

Author Comment

by:beginner_help
Comment Utility
Someone please help me with this
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
Doesn't setting OpenInNewWindow to true open the export in a browser window rather than actually exporting it to an Excel file?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now