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

本文关键字:数据 Excel gridview ASP NET 中从 | 更新日期: 2023-09-27 18:08:04

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

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

这是我的代码:

    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();

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

我不知道你的代码有什么问题,但你可以使用下面的代码:

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(); 
    }
   }
  }
 }
}

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

您只需将下面的代码置于页面加载事件方法之上。肯定会成功的。

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
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