ASP.NET导出数据到Excel的实现方法
更新时间:2021年9月22日 10:14 点击:2001
网上好些代码的原理大致与此类似,同样都存在一个问题,就是:
类型“GridView”的控件“ctl00_center_GridView1”必须放在具有 runat=server 的窗体标记内。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息:System.Web.HttpException: 类型“GridView”的控件“ctl00_center_GridView1”必须放在具有 runat=server 的窗体标记内。
这段错误描述是我在注释了这段程序是报的错,
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
虽然这个方法里的内容也被注释了,也就是说这是个空方法,但是如果没有个方法,程序就会报上面那个错误。最初见到这段错误说明是想到了以前做ajax程序时报的一个错误很是类似。同样是因为没有重写VerifyRenderingInServerForm方法所致。在此提醒使用的朋友注意,下面贴出导出到Excel的代码
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
///<summary>
///ToExcleHelper的摘要说明
///</summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
{
GridViewgvw=newGridView();
intColCount,i;
//如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if(fields.Length!=0&&fields.Length==headTexts.Length)
{
ColCount=fields.Length;
gvw.AutoGenerateColumns=false;
for(i=0;i<ColCount;i++)
{
BoundFieldbf=newBoundField();
bf.DataField=fields[i];
bf.HeaderText=headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns=true;
}
SetStype(gvw);
gvw.DataSource=dataList;
gvw.DataBind();
ExportToExcel(gvw,title);
}
///<summary>
///导出数据到Excel
///</summary>
///<paramname="DataList">IListData</param>
///<paramname="Fields">要导出的字段</param>
///<paramname="HeadName">字段对应显示的名称</param>
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts)
{
ExportToExcel(dataList,fields,headTexts,string.Empty);
}
///<summary>
///设置样式
///</summary>
///<paramname="gvw"></param>
privatestaticvoidSetStype(GridViewgvw)
{
gvw.Font.Name="Verdana";
gvw.BorderStyle=System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor=System.Drawing.Color.LightCyan;
gvw.HeaderStyle.ForeColor=System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign=System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap=false;
gvw.HeaderStyle.Font.Bold=true;
gvw.HeaderStyle.Font.Size=10;
gvw.RowStyle.Font.Size=10;
}
///<summary>
///导出GridView中的数据到Excel
///</summary>
///<paramname="gvw"></param>
///<paramname="DataList"></param>
publicstaticvoidExportToExcel(GridViewgvw,stringtitle)
{
stringfileName;
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
fileName=string.Format("xhmd{0:yyMMddHHmm}.xls",DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+fileName);
HttpContext.Current.Response.ContentType="application/vnd.ms-excel";
StringWritertw=newSystem.IO.StringWriter();
HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if(!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><fontsize=3face=Verdanacolor=#0000FF>"+title+"</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
gvw.Dispose();
tw.Dispose();
hw.Dispose();
gvw=null;
tw=null;
hw=null;
}
publicstaticvoidDataTable2Excel(System.Data.DataTabledtData)
{
System.Web.UI.WebControls.DataGriddgExport=null;
//当前对话
System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriterstrWriter=null;
System.Web.UI.HtmlTextWriterhtmlWriter=null;
if(dtData!=null)
{
//设置编码和附件格式
curContext.Response.ContentType="application/vnd.ms-excel";
curContext.Response.ContentEncoding=System.Text.Encoding.UTF8;
curContext.Response.Charset="";
//导出excel文件
strWriter=newSystem.IO.StringWriter();
htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
//为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport=newSystem.Web.UI.WebControls.DataGrid();
dgExport.DataSource=dtData.DefaultView;
dgExport.AllowPaging=false;
dgExport.DataBind();
//返回客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
}
类型“GridView”的控件“ctl00_center_GridView1”必须放在具有 runat=server 的窗体标记内。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息:System.Web.HttpException: 类型“GridView”的控件“ctl00_center_GridView1”必须放在具有 runat=server 的窗体标记内。
这段错误描述是我在注释了这段程序是报的错,
复制代码 代码如下:
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
虽然这个方法里的内容也被注释了,也就是说这是个空方法,但是如果没有个方法,程序就会报上面那个错误。最初见到这段错误说明是想到了以前做ajax程序时报的一个错误很是类似。同样是因为没有重写VerifyRenderingInServerForm方法所致。在此提醒使用的朋友注意,下面贴出导出到Excel的代码
复制代码 代码如下:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
///<summary>
///ToExcleHelper的摘要说明
///</summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
{
GridViewgvw=newGridView();
intColCount,i;
//如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if(fields.Length!=0&&fields.Length==headTexts.Length)
{
ColCount=fields.Length;
gvw.AutoGenerateColumns=false;
for(i=0;i<ColCount;i++)
{
BoundFieldbf=newBoundField();
bf.DataField=fields[i];
bf.HeaderText=headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns=true;
}
SetStype(gvw);
gvw.DataSource=dataList;
gvw.DataBind();
ExportToExcel(gvw,title);
}
///<summary>
///导出数据到Excel
///</summary>
///<paramname="DataList">IListData</param>
///<paramname="Fields">要导出的字段</param>
///<paramname="HeadName">字段对应显示的名称</param>
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts)
{
ExportToExcel(dataList,fields,headTexts,string.Empty);
}
///<summary>
///设置样式
///</summary>
///<paramname="gvw"></param>
privatestaticvoidSetStype(GridViewgvw)
{
gvw.Font.Name="Verdana";
gvw.BorderStyle=System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor=System.Drawing.Color.LightCyan;
gvw.HeaderStyle.ForeColor=System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign=System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap=false;
gvw.HeaderStyle.Font.Bold=true;
gvw.HeaderStyle.Font.Size=10;
gvw.RowStyle.Font.Size=10;
}
///<summary>
///导出GridView中的数据到Excel
///</summary>
///<paramname="gvw"></param>
///<paramname="DataList"></param>
publicstaticvoidExportToExcel(GridViewgvw,stringtitle)
{
stringfileName;
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
fileName=string.Format("xhmd{0:yyMMddHHmm}.xls",DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+fileName);
HttpContext.Current.Response.ContentType="application/vnd.ms-excel";
StringWritertw=newSystem.IO.StringWriter();
HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if(!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><fontsize=3face=Verdanacolor=#0000FF>"+title+"</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
gvw.Dispose();
tw.Dispose();
hw.Dispose();
gvw=null;
tw=null;
hw=null;
}
publicstaticvoidDataTable2Excel(System.Data.DataTabledtData)
{
System.Web.UI.WebControls.DataGriddgExport=null;
//当前对话
System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriterstrWriter=null;
System.Web.UI.HtmlTextWriterhtmlWriter=null;
if(dtData!=null)
{
//设置编码和附件格式
curContext.Response.ContentType="application/vnd.ms-excel";
curContext.Response.ContentEncoding=System.Text.Encoding.UTF8;
curContext.Response.Charset="";
//导出excel文件
strWriter=newSystem.IO.StringWriter();
htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
//为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport=newSystem.Web.UI.WebControls.DataGrid();
dgExport.DataSource=dtData.DefaultView;
dgExport.AllowPaging=false;
dgExport.DataBind();
//返回客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
}
上一篇: .Net 单例模式(Singleton)
下一篇: .net基础收集汇总
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
- 这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
Antd-vue Table组件添加Click事件,实现点击某行数据教程
这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17- 这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
- 这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
- 这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
- 在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
- 这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
- 这篇文章主要介绍了postgresql数据添加两个字段联合唯一的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
- 这篇文章主要介绍了c#读取excel方法,实例分析了C#读取excel文件的原理与相关技巧,需要的朋友可以参考下...2020-06-25
Vue生命周期activated之返回上一页不重新请求数据操作
这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26- 这篇文章主要介绍了Python导入数值型Excel数据并生成矩阵操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-09
- 这篇文章主要介绍了c# socket网络编程,server端接收,client端发送数据,大家参考使用吧...2020-06-25
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 下面小编就为大家带来一篇C# winform打开Excel文档的方法总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了解决vue watch数据的方法被调用了两次的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-07
- 这篇文章主要介绍了vue 数据(data)赋值问题的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29