ASP.NET导出数据到Excel的实现方法
程序员文章站
2024-03-01 21:55:52
网上好些代码的原理大致与此类似,同样都存在一个问题,就是: 类型“gridview”的控件“ctl00_center_gridview1”必须放在具有 runat=ser...
网上好些代码的原理大致与此类似,同样都存在一个问题,就是:
类型“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();
}
}
}