将gridview数据导出为excel不起作用 [英] Export gridview data to excel is not working
问题描述
我的代码如下
protected void btnextract_Click(object sender,EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset =;
string FileName =Vithal+ DateTime.Now +。xls;
System.IO.StringWriter strwritter = new System.IO.StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =application / vnd.ms-excel;
Response.AddHeader(Content-Disposition,attachment; filename =+ FileName);
grdRpt.GridLines = GridLines.Both;
grdRpt.He aderStyle.Font.Bold = true;
grdRpt.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
当我运行上面的代码时,Response.End();
}
。 excel没有下载,错误显示在下面的行
Response.End();
上面代码中的错误是什么。
<%@ Page Language =C#AutoEventWireup =trueCodeBehind =report.aspx.csEnableEventValidation =falseInherits =transaction%>
i也将Enableeventvalidation设置为false。
如何解决上述错误。
我的尝试:
我的代码如下
protected void btnextract_Click(object sender,EventArgs e)
{
回复。清除();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset =;
string FileName =Vithal+ DateTime.Now +.xls;
System.IO.StringWriter strwritter = new System.IO.StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache );
Response.ContentType =application / vnd.ms-excel;
Response.AddHeader(Content-Disposition,attachment; filename =+ FileName );
grdRpt.GridLines = GridLines.Both;
grdRpt.HeaderStyle.Font.Bold = true;
grdRpt.RenderControl(htmltextwrtter) ;
Response.Write(strwritter.ToString());
Response.End();
}
当我运行上面的代码时,
。 excel没有下载,错误显示在下面的行
Response.End();
上面代码中的错误是什么。
<%@ Page Language =C#AutoEventWireup =trueCodeBehind =report.aspx.csEnableEventValidation =falseInherits =transaction%>
i也将Enableeventvalidation设置为false。
如何解决上述错误
My code as follows
protected void btnextract_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "Vithal" + DateTime.Now + ".xls";
System.IO.StringWriter strwritter = new System.IO.StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
grdRpt.GridLines = GridLines.Both;
grdRpt.HeaderStyle.Font.Bold = true;
grdRpt.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
when i run the above code. the excel is not downloading, the error shows in below line
Response.End();
what is the mistake in my above code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="report.aspx.cs" EnableEventValidation = "false" Inherits="transaction" %>
i also set the Enableeventvalidation false.
how to solve the above error.
What I have tried:
My code as follows
protected void btnextract_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "Vithal" + DateTime.Now + ".xls";
System.IO.StringWriter strwritter = new System.IO.StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
grdRpt.GridLines = GridLines.Both;
grdRpt.HeaderStyle.Font.Bold = true;
grdRpt.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
when i run the above code. the excel is not downloading, the error shows in below line
Response.End();
what is the mistake in my above code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="report.aspx.cs" EnableEventValidation = "false" Inherits="transaction" %>
i also set the Enableeventvalidation false.
how to solve the above error
推荐答案
嗯...你正在向客户端渲染一个HTML控件并告诉客户端它是一个Excel工作簿。那不行。差远了。 HTML不是Excel知道如何处理的事情。
网上有大量的例子向您展示如何将DataTable导出到Excel工作簿。但是,您不能使用Excel或Office,Interop。您必须使用其他库,如OpenXML SDK或ClosedXML SDK,或类似的。
谷歌: C#将DataTable导出到Excel Asp.net [ ^ ]
Ummm...you're rendering an HTML control out to the client and telling the client it's an Excel workbook. That's not going to work. Not even close. HTML is not something Excel knows what to do with.
There's tons of examples on the web that show you how to export a DataTable to an Excel workbook. But, you can NOT use Excel, or Office, Interop. You must use another library, like the OpenXML SDK, or the ClosedXML SDK, or similar.
Google: "C# export DataTable to Excel Asp.net"[^]
这篇关于将gridview数据导出为excel不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!