tags:

views:

814

answers:

3

In ASP.NET, I am exporting some data to Excel by simply binding a DataSet to a GridView and then setting the ContentType to Excel.

My ASPX page is very simple and looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExamExportReport.aspx.cs" Inherits="Cabi.CamCentral.Web.Pages.Utility.ExamExportReport" %>
<html>
<body>
  <form id="form1" runat="server">
     <asp:GridView
      ID="gridExam"
      AutoGenerateColumns="true"
      runat="server">
     </asp:GridView>
  </form>
</body>
</html>

In the Page_Load method of the code behind, I am doing this:

protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("content-disposition", "attachment; filename=ExamExport.xls");
}

Generally, everything works fine, and the Excel file pops up with the right data. The problem is that the Excel file always ends up with a blank first row right above the column headers. I just can't figure out what is causing this. Maybe it's something about the form tag? Maybe I need to add some styling or something to strip out padding or margins? I've tried a bunch of things but I just can't get rid of that dang first blank row. Has anyone else run into this? Any solutions?

A: 

Here is my code that works fine:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";
            SqlConnection myConnection = new SqlConnection(connectionString);
            SqlDataAdapter ad = new SqlDataAdapter("select * from products", myConnection);
            DataSet ds = new DataSet();
            ad.Fill(ds);

            gvProducts.DataSource = ds;
            gvProducts.DataBind(); 
        }

        protected void ExportGridView(object sender, EventArgs e)
        {
            Response.ClearContent();

            Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");

            Response.ContentType = "application/excel";

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);

            gvProducts.RenderControl(htw);

            Response.Write(sw.ToString());

            Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }
azamsharp
+1  A: 

@azamsharp - I found the solution elsewhere while you were replying. :-) It turns out that removing the form tag entirely from the ASPX page is the trick, and the only way to do this is to override the VerifyRenderingInServerForm method as you are doing.

If you update your solution to include the fact that you need to remove the form tag from the page, I will accept your answer. Thanks.

jeremcc
I just removed the form tag and it resulted in the error. You can export using the form tag if you add this line to the page directive: EnableEventValidation="false"
azamsharp
+1  A: 

An easier solution is to override the Render (HtmlTextWriter writer) method and make it empty:

protected override void Render(HtmlTextWriter writer){}

http://c-sharpe.blogspot.com/2009/05/get-rid-of-blank-row-when-exporting-to.html

Eric