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.
在ASP.NET中,我通过简单地将DataSet绑定到GridView然后将ContentType设置为Excel将一些数据导出到Excel。
My ASPX page is very simple and looks like this:
我的ASPX页面很简单,看起来像这样:
<%@ 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:
在后面的代码的Page_Load方法中,我这样做:
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?
通常,一切正常,Excel文件会弹出正确的数据。问题是Excel文件总是在列标题正上方有一个空白的第一行。我只是想不通是什么导致了这一点。也许它是关于表单标签的东西?也许我需要添加一些样式或某些东西去除填充或边距?我已经尝试了很多东西,但我无法摆脱那个第一个空行。有没有其他人遇到这个?有解决方案?
3 个解决方案
#1
3
@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.
@azamsharp - 我在你回复时在其他地方找到了解决方案。 :-)事实证明,完全从ASPX页面删除表单标签是诀窍,唯一的方法是覆盖VerifyRenderingInServerForm方法。
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.
如果您更新解决方案以包含需要从页面中删除表单标记的事实,我将接受您的回答。谢谢。
#2
1
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)
{
}
#3
1
An easier solution is to override the Render (HtmlTextWriter writer) method and make it empty:
更简单的解决方案是覆盖Render(HtmlTextWriter writer)方法并将其设为空:
protected override void Render(HtmlTextWriter writer){}
protected override void Render(HtmlTextWriter writer){}
http://c-sharpe.blogspot.com/2009/05/get-rid-of-blank-row-when-exporting-to.html
#1
3
@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.
@azamsharp - 我在你回复时在其他地方找到了解决方案。 :-)事实证明,完全从ASPX页面删除表单标签是诀窍,唯一的方法是覆盖VerifyRenderingInServerForm方法。
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.
如果您更新解决方案以包含需要从页面中删除表单标记的事实,我将接受您的回答。谢谢。
#2
1
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)
{
}
#3
1
An easier solution is to override the Render (HtmlTextWriter writer) method and make it empty:
更简单的解决方案是覆盖Render(HtmlTextWriter writer)方法并将其设为空:
protected override void Render(HtmlTextWriter writer){}
protected override void Render(HtmlTextWriter writer){}
http://c-sharpe.blogspot.com/2009/05/get-rid-of-blank-row-when-exporting-to.html