Asp.net 为什么我的Excel导出在顶部有一个空行?
在ASP.NET中,我只需将数据集绑定到GridView,然后将ContentType设置为Excel,即可将一些数据导出到Excel 我的ASPX页面非常简单,如下所示:Asp.net 为什么我的Excel导出在顶部有一个空行?,asp.net,excel,Asp.net,Excel,在ASP.NET中,我只需将数据集绑定到GridView,然后将ContentType设置为Excel,即可将一些数据导出到Excel 我的ASPX页面非常简单,如下所示: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExamExportReport.aspx.cs" Inherits="Cabi.CamCentral.Web.Pages.Utility.ExamExportReport" %> <html
<%@ 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>
通常,一切正常,Excel文件会弹出正确的数据。问题是Excel文件的第一行总是在列标题上方空白。我只是不知道是什么原因造成的。也许是表单标签的问题?也许我需要添加一些样式或东西来去除填充物或边距?我试过很多东西,但我就是摆脱不了那该死的第一排空白。还有其他人碰到过这个吗?有什么解决方案吗?以下是我的代码:
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-当您答复时,我在别处找到了解决方案。:-)事实证明,完全从ASPX页面中删除表单标记是一个诀窍,唯一的方法是在执行此操作时重写VerifyRenderingInServerForm方法
如果您更新您的解决方案以包含您需要从页面中删除表单标记的事实,我将接受您的答案。谢谢。一个更简单的解决方案是重写Render(HtmlTextWriter)方法并将其置为空: 受保护的覆盖无效呈现(HtmlTextWriter写入程序){}
这应该在内部!Page.IsPostback事件。此外,您在Page_Load方法中执行此操作的任何原因。此页面将永远不会有回发,因为它是通过Server.Transfer导航到的,并且没有用于回发任何内容的UI。Page_Load是此实例中的逻辑位置。我刚刚删除了表单标记,这导致了错误。如果将此行添加到页面指令:EnableEventValidation=“false”,则可以使用表单标记进行导出
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)
{
}