NET中下载Excel错误

NET中下载Excel错误

本文介绍了在ASP.NET中下载Excel错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个关于在asp.net中下载excel的查询,我有一个页面,其中所有投诉都列在gridview中..而该页面是我使用Update Panel的子页面,现在当我导出gridview数据源以实现excel时发生的是,整个页面都插入到excel中,这意味着要插入excel的页面的按钮,下拉菜单等.

我怎么能只将gridview数据源导出到excel ....

我的代码是..

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:AsyncPostBackTrigger ControlID="btnExportToExcel" EventName="btnExportToExcel_Click" />--%>
          <%--<asp:PostBackTrigger ControlID="btnExportToExcel" EventName="btnExportToExcel_Click" /> --%>
          <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());
   }




我使用了上面的代码,下载的excel包含按钮,下拉列表,gridview等页面控件...

请帮助我如何仅导出数据源...

问候,
Krunal Panchal




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

推荐答案



这篇关于在ASP.NET中下载Excel错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:17