Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have an gridview in Content Page inside update Panel

i have written export to excel code but when i excel file is downloaded it wont show gird data it only show
<div> </div>


here is my code



XML
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
          <asp:Panel ID="Panel1" runat="server" CssClass="PanMain">

<asp:Button ID="btnExport" runat="server" Text="Export" CssClass="CmdBtn" />

 <asp:GridView ID="GVDetails" runat="server" AutoGenerateColumns="False" BackColor="White"
                                    BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
                                    GridLines="Vertical" Width="100%" CssClass="GridView">
                                    <AlternatingRowStyle BackColor="#CCCCCC" />
                                    <FooterStyle BackColor="#CCCCCC" />
                                    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                                    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                                    <Columns>
                                        <asp:TemplateField HeaderText="" ItemStyle-Width="60px">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="LnkDetails" runat="server" Text="EDIT ->" CommandArgument='<%#Eval("nVP_Id") %>' />
                                            </ItemTemplate>
                                            <ItemStyle Width="60px" />
                                        </asp:TemplateField>
                                        <asp:BoundField HeaderText="VPO No" DataField="sVPO_Id" 
                                            ItemStyle-Width="120px" >
                                        <ItemStyle Width="120px" />
                                        </asp:BoundField>
                                        <%-- <asp:BoundField HeaderText="Region" DataField="sRegion" ItemStyle-Width="120px" />
                                        <asp:BoundField HeaderText="VPO Type" DataField="sVPO_Type" ItemStyle-Width="120px" />--%>
                                        <asp:BoundField HeaderText="Customer" DataField="sName" />
                                        <asp:BoundField HeaderText="Ref No" DataField="sRef_no" 
                                            ItemStyle-Width="120px" >
                                        <ItemStyle Width="120px" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="VPO Date" DataField="dtVPO_Dt" 
                                            ItemStyle-Width="150px" >
                                        <ItemStyle Width="150px" />
                                        </asp:BoundField>
                                        <asp:BoundField HeaderText="VPO amount" DataField="dTotal" 
                                            ItemStyle-Width="150px" >
                                        <ItemStyle Width="150px" />
                                        </asp:BoundField>
                                    </Columns>
                                </asp:GridView>

</asp:Panel>
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btnExport" />
        </Triggers>
    </asp:UpdatePanel>


and here is my Export to Excel Code

VB
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
    ' removing Template field which is inside  grid view in column 0 template is a link button
    GVDetails.Columns.RemoveAt(0)
    GVDetails.DataBind()
    Dim ReportName As String = ""
    'ReportName = ddlReportFor.SelectedItem.Text
    ReportName = "VPO_Report_" & Format(Now, "yyyy_MM_dd_HH_mm_ss")
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.Charset = ""
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & ReportName & ".xls")
    'HttpContext.Current.Response.ContentEncoding = Encoding.UTF8
    Dim tw As New StringWriter()
    Dim hw As New HtmlTextWriter(tw)
    'tbl1.RenderControl(hw)
    'TPOOutPut.RenderControl(hw)
    Dim frm As HtmlForm
    frm = New HtmlForm
    frm.Attributes("runat") = "server"
    frm.Controls.Add(GVDetails)
    'GVDetails.Parent.Controls.Add(frm)
    'frm.RenderControl(hw)
    GVDetails.RenderControl(hw)
    HttpContext.Current.Response.Write(tw.ToString())
    HttpContext.Current.Response.Flush()
    HttpContext.Current.Response.[End]()

End Sub


where its wrong kindly correct me
Posted

Use this its works for me

C#
public void ExportToExcel(System.Web.UI.Control ctl)
        {
           using (Control myCtl = ctl)
            {
                HttpContext.Current.Response.AppendHeader("Content-Disposition", attachment);
                HttpContext.Current.Response.Charset = charSet;
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.ContentType = content;
                myCtl.Page.EnableViewState = false;
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);

               //Renders the control here.
                myCtl.RenderControl(htw);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }            
        }



and for calling this

C#
dt = GetData();
     myGridView.DataSource = dt;
     myGridView.DataBind();


     ExportToExcel(myGridView);


here the GetData() method is used to get the gridview data
 
Share this answer
 
Comments
Diler Patel 30-Oct-13 8:01am    
Still Same Problem sir :( i m able to download excel file but when i open it i find Div tag and nothing else appears
IpsitaMishra 30-Oct-13 8:19am    
It works fine for me .Can you please write a try catch and debug the code to get the error.
IpsitaMishra 30-Oct-13 8:22am    
can you please make sure the datasource is not blank.
Actually when i was removing template field in code behind and again i was binding but grid was losing its data source

So i removed the template Field and than re-binned that grid view whose data source was in View state
my code is as follows in

VB
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        ' removing Template field which is inside  grid view in column 0 template is a link button
        viewstate("GVsearch") = GVDetails.datasource


        GVDetails.Columns.RemoveAt(0)
    dtCurrentTable = ViewState("GVSearch")
    GVDetails.DataSource = dtCurrentTable 'GVdeta.DataSource
    GVDetails.DataBind()

    Dim ReportName As String = &quot;&quot;
    ReportName = ddlReportFor.SelectedItem.Text
    ReportName = &quot;VPO_Report_&quot; &amp; Format(Now, &quot;yyyy_MM_dd_HH_mm_ss&quot;)
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.Charset = &quot;&quot;
    HttpContext.Current.Response.ContentType = &quot;application/vnd.ms-excel&quot;
    HttpContext.Current.Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment; filename=&quot; &amp; ReportName &amp; &quot;.xls&quot;)
    &#39;HttpContext.Current.Response.ContentEncoding = Encoding.UTF8
    Dim tw As New StringWriter()
    Dim hw As New HtmlTextWriter(tw)
    &#39;tbl1.RenderControl(hw)
    &#39;TPOOutPut.RenderControl(hw)
    Dim frm As HtmlForm
    frm = New HtmlForm
    frm.Attributes(&quot;runat&quot;) = &quot;server&quot;
    frm.Controls.Add(GVDetails)
    &#39;GVDetails.Parent.Controls.Add(frm)
    &#39;frm.RenderControl(hw)
    GVDetails.RenderControl(hw)
    HttpContext.Current.Response.Write(tw.ToString())
    HttpContext.Current.Response.Flush()
    HttpContext.Current.Response.[End]()

End Sub</pre>


And Thanks for your support every one
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900