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 ..
<%@ 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:PostBackTrigger ControlID ="btnExportToExcel" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
codebehind...
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