数据从gridview导出到ASP.NET中的Excel

时间:2023-01-03 01:57:46

Data transfer grid view to Excel sheet in ASP.NET; how to solve this type of problem Error is

ASP.NET中的Excel工作表数据传输网格视图;如何解决这类问题错误是

Control 'ContentPlaceHolder1_gvdetails' of type 'GridView' must be placed inside a form  tag with runat=server.

This is my code:

这是我的代码:

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}",
    "Customers.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gvdetails.AllowPaging = false; gvdetails.DataBind();
    gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
    gvdetails.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();

3 个解决方案

#1


2  

I am not sure what is wrong with your code but you can use the below code:

我不确定您的代码有什么问题,但您可以使用以下代码:

using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Whatever
{
 /// 

 /// This class provides a method to write a dataset to the HttpResponse as
 /// an excel file. 
 /// 

 public class ExcelExport
 {
  public static void ExportDataSetToExcel(DataSet ds, string filename)
  {
   HttpResponse response = HttpContext.Current.Response;

   // first let's clean up the response.object
   response.Clear();
   response.Charset = "";

   // set the response mime type for excel
   response.ContentType = "application/vnd.ms-excel";
   response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

   // create a string writer
   using (StringWriter sw = new StringWriter())
   {
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
     // instantiate a datagrid
     DataGrid dg = new DataGrid();
     dg.DataSource = ds.Tables[0];
     dg.DataBind();
     dg.RenderControl(htw);
     response.Write(sw.ToString());
     response.End(); 
    }
   }
  }
 }
}

You can also go to this link http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx

您也可以访问此链接http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx

#2


0  

you just place the bellow code above the page load event method. surely it will work.

您只需将波纹管代码放在页面加载事件方法之上。肯定会有用。

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }

#3


0  

Please try with the below code for the blank excel sheet export issues.

#region "   Excel Export"
    private void fnExcelUpload()
    {
        try
        {
            dgDashboard.AllowPaging = false;
            dgDashboard.Columns[0].Visible = false;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename = ExcelExport.xls");
            Response.Charset = "";
            Response.Buffer = true;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            fillDashboard();
            dgDashboard.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            HttpContext.Current.Response.Flush();
            //HttpContext.Current.Response.End();
        }
        catch (Exception Ex)
        {
            ErrorLog obj = new ErrorLog(Session["PROGRAMCODE"].ToString(), Ex.Message, Ex.StackTrace, this.Page.ToString(), new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]).HostName.ToString(), Session["EMPNUMBER"].ToString(), HttpContext.Current.User.Identity.Name.ToString());
        }
        HttpContext.Current.Response.End();
    }

    protected void imgExcelExport_Click(object sender, ImageClickEventArgs e)
    {
        fnExcelUpload();
    }
    #endregion

#1


2  

I am not sure what is wrong with your code but you can use the below code:

我不确定您的代码有什么问题,但您可以使用以下代码:

using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Whatever
{
 /// 

 /// This class provides a method to write a dataset to the HttpResponse as
 /// an excel file. 
 /// 

 public class ExcelExport
 {
  public static void ExportDataSetToExcel(DataSet ds, string filename)
  {
   HttpResponse response = HttpContext.Current.Response;

   // first let's clean up the response.object
   response.Clear();
   response.Charset = "";

   // set the response mime type for excel
   response.ContentType = "application/vnd.ms-excel";
   response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

   // create a string writer
   using (StringWriter sw = new StringWriter())
   {
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
     // instantiate a datagrid
     DataGrid dg = new DataGrid();
     dg.DataSource = ds.Tables[0];
     dg.DataBind();
     dg.RenderControl(htw);
     response.Write(sw.ToString());
     response.End(); 
    }
   }
  }
 }
}

You can also go to this link http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx

您也可以访问此链接http://tim.mackey.ie/HowtoExportADatasetToExcelCAspnet.aspx

#2


0  

you just place the bellow code above the page load event method. surely it will work.

您只需将波纹管代码放在页面加载事件方法之上。肯定会有用。

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }

#3


0  

Please try with the below code for the blank excel sheet export issues.

#region "   Excel Export"
    private void fnExcelUpload()
    {
        try
        {
            dgDashboard.AllowPaging = false;
            dgDashboard.Columns[0].Visible = false;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename = ExcelExport.xls");
            Response.Charset = "";
            Response.Buffer = true;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            fillDashboard();
            dgDashboard.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            HttpContext.Current.Response.Flush();
            //HttpContext.Current.Response.End();
        }
        catch (Exception Ex)
        {
            ErrorLog obj = new ErrorLog(Session["PROGRAMCODE"].ToString(), Ex.Message, Ex.StackTrace, this.Page.ToString(), new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, System.Net.Dns.GetHostEntry(Context.Request.ServerVariables["REMOTE_HOST"]).HostName.ToString(), Session["EMPNUMBER"].ToString(), HttpContext.Current.User.Identity.Name.ToString());
        }
        HttpContext.Current.Response.End();
    }

    protected void imgExcelExport_Click(object sender, ImageClickEventArgs e)
    {
        fnExcelUpload();
    }
    #endregion