Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I have one query regarding downloading excel in asp.net, I have one page in which all complaints are listed in gridview.. and that page is a child page in which i have used Update Panel, now when i export gridview datasource to excel what happens is , the whole page is inserted into the excel i mean the buttons , dropdowns etc of the page in to excel.

How could i export only gridview datasource to excel....

My code is ..

ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ComplaintActions.aspx.cs" Inherits="ComplaintActions" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<pre lang="xml"><asp:UpdatePanel ID="upComplaintActions" runat ="server"   UpdateMode="Conditional" >
    <ContentTemplate >
 <asp:Button ID="btnExportToExcel" runat ="server" 
                                            Text="Export Selected Complaints to Excel" onclick="btnExportToExcel_Click" />
 <asp:GridView id="gdvComplaints" runat ="server" BackColor="White" 
                            BorderColor="#CCCCCC" BorderStyle="None" 
        BorderWidth="1px" CellPadding="3" 
                            EnableModelValidation="True" EmptyDataText ="~No Records Found~" 
                            AutoGenerateColumns="False" AllowPaging="True" 
                            onpageindexchanging="gdvComplaints_PageIndexChanging"  
                            PageSize="5" onrowcommand="gdvComplaints_RowCommand">
                            <Columns>
                                     boundfield 1.....boundfield10
                               </columns>
                        </asp:GridView>
        
       
    </ContentTemplate>
    <Triggers >
    <%--<asp:AsyncPostBackTrigger ControlID="btnExportToExcel" EventName="btnExportToExcel_Click" />--%>
          <%--<asp:PostBackTrigger ControlID="btnExportToExcel" EventName="btnExportToExcel_Click" /> --%>
          <asp:PostBackTrigger ControlID ="btnExportToExcel" />
    </Triggers>
</asp:UpdatePanel>

      
                 
                    
      
</asp:Content>

codebehind...
C#
public void ExportDetails(DataTable dt)
   {
       string fileName = "ComplaintReport-" + DateTime.Today.Day.ToString() + "" + DateTime.Today.Month.ToString() + "" + DateTime.Today.Year.ToString();
       HttpResponse response = HttpContext.Current.Response;
       response.Clear();
       response.ContentType = "application/vnd.ms-excel";
       response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\".xls");
       StringWriter sw = new StringWriter();
       HtmlTextWriter htw = new HtmlTextWriter(sw);
       DataGrid dg = new DataGrid();
       dg.DataSource = dt;
       dg.DataBind();
       dg.RenderControl(htw);
       response.Write(sw.ToString());
   }



I have used the above code, and the excel downloaded contains the page controls like button,dropdownlist,gridview etc...

Please help me how to export only datasource...

Regards,
Krunal Panchal
Posted
Updated 12-Sep-22 0:43am
v2

End the response after write.
use this Response.End();
 
Share this answer
 
Hi Use this.......

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "application/vnd.xls";
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.End();
 
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