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

Issue with HTML Excel Export

1 Answer 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bharat
Top achievements
Rank 1
Bharat asked on 27 Jan 2014, 03:29 PM
Hi below is my aspx and aspx.cs file , I am trying to export all data(MasterTableView and DetailsTableView), But my excel file has an icon in excel sheet
Attached is the screenshot of the icon and excel file,
please help me out.

 <script type="text/javascript" language="javascript">
        function onRequestStart(sender, args) {
            if (args.get_eventTarget().indexOf("ExportMasterDataButton") > 0) {
                args.set_enableAjax(false);
            }
            if (args.get_eventTarget().indexOf("ExportDetailDataButton") > 0) {
                args.set_enableAjax(false);
            }
            if (args.get_eventTarget().indexOf("ExportMasterGridButton") > 0) {
                args.set_enableAjax(false);
            }
        }
    </script>
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="GridView12">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="GridView12" LoadingPanelID="RadAjaxLoadingPanel1">
                    </telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
    </telerik:RadAjaxLoadingPanel> 

<telerik:RadGrid ID="GridView12" runat="server" Width="100%" AutoGenerateColumns="false"
            PageSize="10" EnableLinqExpressions="false" AllowSorting="True" AllowFilteringByColumn="True"
            OnDetailTableDataBind="GridView12_DetailTableDataBind" enablelinqexpression="false"
            AllowPaging="True" CellSpacing="1" Style="padding: 1px;" Skin="Default" OnNeedDataSource="GridView12_OnNeedDataSource"
            OnPreRender="GridView12_PreRender" OnItemCreated="GridView12_OnItemCreated" OnItemCommand="GridView12_OnItemCommand">
            <ClientSettings>
                <Selecting AllowRowSelect="False" />
                <Scrolling AllowScroll="true" ScrollHeight="400px" UseStaticHeaders="true" />
            </ClientSettings>
            <ExportSettings Excel-Format="Html" ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
            </ExportSettings>
            <MasterTableView Name="TypeSummary" DataKeyNames="type_id" ClientDataKeyNames="type_id"
                NoMasterRecordsText="<center>No viewings are available" CommandItemDisplay="Top">
                <HeaderStyle Font-Size="14px" />
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True" />
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True" />
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="Type_Name" SortOrder="Ascending" />
                </SortExpressions>
                <CommandItemTemplate>
                    <asp:Button ID="ExportMasterDataButton" runat="server" CommandName="ExportMasterData"
                        ClientIDMode="Static"></asp:Button>
                    <asp:Button ID="ExportMasterGridButton" runat="server" ClientIDMode="Static" CommandName="ExportAllMasterDetailsData" />
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridBoundColumn DataField="Type_Name" SortExpression="Type_Name" HeaderText="Type Name"
                        HeaderButtonType="None" UniqueName="TypeName" ItemStyle-Width="100px" ShowSortIcon="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Total_Duration" SortExpression="Total_Duration"
                        HeaderText="Total Duration" UniqueName="TotalDuration" ItemStyle-Width="100px"
                        ShowSortIcon="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Num_Contacts" SortExpression="Num_Contacts" HeaderText="Num Contacts"
                        UniqueName="NumContacts" ItemStyle-Width="100px" ShowSortIcon="true">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Virtual_Num_contacts" SortExpression="Virtual_Num_contacts"
                        ShowSortIcon="true" HeaderText="Num Group Contacts" UniqueName="NumGroupContacts"
                        ItemStyle-Width="100px">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Average_Duration" SortExpression="Average_Duration"
                        HeaderText="Average Duration" UniqueName="AverageDuration" ShowSortIcon="true"
                        ItemStyle-Width="100px">
                    </telerik:GridBoundColumn>
                </Columns>
                <DetailTables>
                    <telerik:GridTableView Name="TypeList" DataKeyNames="type_id" Width="100%" Height="100%"
                        NoDetailRecordsText="<center>No viewings are available</center>" AllowFilteringByColumn="true"
                        ShowHeadersWhenNoRecords="true" AllowPaging="true" AllowSorting="true" PageSize="10"
                        CommandItemDisplay="Top" HierarchyLoadMode="ServerOnDemand">
                        <HeaderStyle Font-Size="14px" />
                        <SortExpressions>
                            <telerik:GridSortExpression FieldName="Access_Date" SortOrder="Ascending" />
                        </SortExpressions>
                        <CommandItemTemplate>
                            <asp:Button ID="ExportDetailDataButton" runat="server" CommandName="ExportDetailsData"
                                ClientIDMode="Static"></asp:Button>
                        </CommandItemTemplate>
                        <Columns>
                            <telerik:GridDateTimeColumn FilterControlWidth="175px" DataField="Access_Date" HeaderText="Date of Viewing"
                                SortExpression="Access_Date" UniqueName="AccessDate" PickerType="DateTimePicker"
                                DataFormatString="{0:MM/dd/yyyy hh:mm tt}" ShowFilterIcon="True" ShowSortIcon="True"
                                DataType="System.DateTime" AllowFiltering="true">
                            </telerik:GridDateTimeColumn>
                            <telerik:GridBoundColumn DataField="First_Name" SortExpression="First_Name" HeaderText="First Name"
                                HeaderButtonType="None" UniqueName="FirstName" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Last_Name" SortExpression="Last_Name" HeaderText="Last Name"
                                HeaderButtonType="None" UniqueName="LastName" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridTemplateColumn DataField="Email_Address" HeaderText="Email Address" ItemStyle-Width="100px" FilterControlWidth="100px"
                                SortExpression="Email_Address" UniqueName="EmailAddress" ShowSortIcon="true" AllowFiltering="true">
                                <ItemTemplate>
                                    <a onclick="viewVirtualContact(<%#Eval("contact_id")%>,<%#Eval("isvirtualcontact")%>)">
                                        <%# Eval("Email_Address") %></a>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridBoundColumn DataField="Business_title" SortExpression="Business_title"
                                HeaderText="Title" HeaderButtonType="None" UniqueName="Title" ItemStyle-Width="100px"
                                FilterControlWidth="100px" ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="city" SortExpression="city" HeaderText="City"
                                HeaderButtonType="None" UniqueName="City" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="state" SortExpression="state" HeaderText="State"
                                HeaderButtonType="None" UniqueName="state" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="country" SortExpression="country" HeaderText="Country"
                                HeaderButtonType="None" UniqueName="country" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Video_Event_Name" SortExpression="Video_Event_Name"
                                HeaderText="Video Event Name" HeaderButtonType="None" UniqueName="VideoEventName"
                                ItemStyle-Width="100px" FilterControlWidth="100px" ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Video_Viewed" SortExpression="Video_Viewed" HeaderText="Session Viewed"
                                HeaderButtonType="None" UniqueName="VideoViewed" ItemStyle-Width="150px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Duration" SortExpression="Duration" HeaderText="Duration"
                                HeaderButtonType="None" UniqueName="Duration" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Sales_Status" SortExpression="Sales_Status" HeaderText="Sales Status"
                                HeaderButtonType="None" UniqueName="SalesStatus" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="ProspectValue" SortExpression="ProspectValue"
                                HeaderText="Prospect Value" HeaderButtonType="None" UniqueName="ProspectValue"
                                ItemStyle-Width="100px" FilterControlWidth="100px" ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="iplocation" SortExpression="iplocation" HeaderText="Location"
                                HeaderButtonType="None" UniqueName="iplocation" ItemStyle-Width="100px" FilterControlWidth="100px"
                                ShowSortIcon="true" AllowFiltering="true">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <PagerStyle AlwaysVisible="true" PageSizeControlType="RadDropDownList" />
                    </telerik:GridTableView>
                </DetailTables>
                <PagerStyle AlwaysVisible="true" PageSizeControlType="RadDropDownList" />
            </MasterTableView>
            <FilterMenu EnableImageSprites="False" />
        </telerik:RadGrid>

Below is the code snippet in my aspx.cs file
 GridView12.ExportSettings.FileName = "AllDetails";
                GridView12.MasterTableView.UseAllDataFields = true;
                GridView12.MasterTableView.HierarchyDefaultExpanded = true;
                GridView12.MasterTableView.ExportToExcel();

HTML is the excel format of export 

 GridView12.ExportSettings.FileName = "AllDetails";
                GridView12.MasterTableView.UseAllDataFields = true;
                GridView12.MasterTableView.HierarchyDefaultExpanded = true;
                GridView12.MasterTableView.ExportToExcel();






1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Jan 2014, 12:57 PM
Hi Bharat,

I tried your code and it was working fine at my end. Below is a sample code snippet that i tried, please try and check the difference:

ASPX:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGrid1">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1">
                </telerik:AjaxUpdatedControl>
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
    OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="True" OnItemCommand="RadGrid1_OnItemCommand">
    <ExportSettings Excel-Format="Html" ExportOnlyData="true" IgnorePaging="true" OpenInNewWindow="true">
    </ExportSettings>
    <MasterTableView Name="Master" DataKeyNames="CustomerID" CommandItemDisplay="Top"
        HierarchyLoadMode="ServerBind">
        <CommandItemTemplate>
            <asp:Button ID="ExportMaster" runat="server" CommandName="ExportMasterData" Text="Master">
            </asp:Button>
        </CommandItemTemplate>
        <Columns>
            <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" UniqueName="CustomerID">
            </telerik:GridBoundColumn>
        </Columns>
        <DetailTables>
            <telerik:GridTableView Name="Child" AllowPaging="true" UseAllDataFields="true" CommandItemDisplay="Top">
                <CommandItemTemplate>
                    <asp:Button ID="ExportDetail" runat="server" CommandName="ExportDetailsData" Text="Child">
                    </asp:Button>
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

JS:
<script type="text/javascript">
    function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("ExportMaster") >= 0)
            args.set_enableAjax(false);
        if (args.get_eventTarget().indexOf("ExportDetail") >= 0)
            args.set_enableAjax(false);
    }
</script>

C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    if (!e.IsFromDetailTable)
    {
        RadGrid1.DataSource = GetDataTable("SELECT * FROM Customers");
    }
}
 
protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
{
    GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
    switch (e.DetailTableView.Name)
    {
        case "Child":
            {
                string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString();
                e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = '" + CustomerID + "'");
                break;
            }
    }
}
 
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
 
    return myDataTable;
}
 
protected void RadGrid1_OnItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "ExportMasterData")
    {
        RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
        RadGrid1.MasterTableView.ExportToExcel();
    }
    else if ((e.CommandName == "ExportDetailsData") && (e.Item.OwnerTableView.Name == "Child"))
    {
        e.Item.OwnerTableView.ExportToExcel();
    }
}

Thanks,
Shinu
Tags
Grid
Asked by
Bharat
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or