1

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

3 回答 3

2

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

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

于 2013-04-08T09:16:27.730 回答
0

您只需将下面的代码放在页面加载事件方法之上。肯定会奏效的。

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
于 2013-05-28T05:21:10.427 回答
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
于 2014-09-18T15:31:58.163 回答