在做asp
net程序时涉及到数据显示的时候多数会要求打印
而网页上的打印格式往往又不能满足需求
经常用的方法就是导入到Excel以后再进行打印
(仿佛这已经是老生常谈)今天在网上搜了一段打印的代码
觉得不错
需要打印的朋友可以看看
网上好些代码的原理大致与此类似同样都存在一个问题就是
类型“GridView”的控件 “ctl_center_GridView”必须放在具有 runat=server 的窗体标记内 说明: 执行当前 Web 请求期间出现未处理的异常请检查堆栈跟蹤信息以了解有关该错误以及代码中导致错误的出处的详细信息 异常详细信息:SystemWebHttpException: 类型“GridView”的控件“ctl_center_GridView”必须放在具有 runat=server 的窗体标记内
这段错误描述是我在注释了这段程序是报的错
复制代码 代码如下:
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
////base
VerifyRenderingInServerForm(control);
//}
虽然这个方法里的内容也被注释了也就是说这是个空方法但是如果没有个方法程序就会报上面那个错误最初见到这段错误说明是想到了以前做ajax程 序时报的一个错误很是类似同样是因为没有重写VerifyRenderingInServerForm方法所致在此提醒使用的朋友注意下面贴出导出 到Excel的代码
复制代码 代码如下:
usingSystem;
usingSystemData;
usingSystemConfiguration;
usingSystemCollections;
usingSystemWeb;
usingSystemWebSecurity;
usingSystemWebUI;
usingSystemWebUIWebControls;
usingSystemWebUIWebControlsWebParts;
usingSystemWebUIHtmlControls;
usingSystemIO;
///<summary>
///ToExcleHelper的摘要说明
///</summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataListstring[]fieldsstring[]headTextsstringtitle)
{
GridViewgvw=newGridView();
intColCounti;
//如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if(fieldsLength!=&&fieldsLength==headTextsLength)
{
ColCount=fieldsLength;
gvwAutoGenerateColumns=false;
for(i=;i<ColCount;i++)
{
BoundFieldbf=newBoundField();
bfDataField=fields[i];
bfHeaderText=headTexts[i];
gvwColumnsAdd(bf);
}
}
else
{
gvwAutoGenerateColumns=true;
}
SetStype(gvw);
gvwDataSource=dataList;
gvwDataBind();
ExportToExcel(gvwtitle);
}
///<summary>
///导出数据到Excel
///</summary>
///<paramname="DataList">IListData</param>
///<paramname="Fields">要导出的字段</param>
///<paramname="HeadName">字段对应显示的名称</param>
publicstaticvoidExportToExcel(IListdataListstring[]fieldsstring[]headTexts)
{
ExportToExcel(dataListfieldsheadTextsstringEmpty);
}
///<summary>
///设置样式
///</summary>
///<paramname="gvw"></param>
privatestaticvoidSetStype(GridViewgvw)
{
gvwFontName="Verdana";
gvwBorderStyle=SystemWebUIWebControlsBorderStyleSolid;
gvwHeaderStyleBackColor=SystemDrawingColorLightCyan;
gvwHeaderStyleForeColor=SystemDrawingColorBlack;
gvwHeaderStyleHorizontalAlign=SystemWebUIWebControlsHorizontalAlignCenter;
gvwHeaderStyleWrap=false;
gvwHeaderStyleFontBold=true;
gvwHeaderStyleFontSize=;
gvwRowStyleFontSize=;
}
///<summary>
///导出GridView中的数据到Excel
///</summary>
///<paramname="gvw"></param>
///<paramname="DataList"></param>
publicstaticvoidExportToExcel(GridViewgvwstringtitle)
{
stringfileName;
HttpContextCurrentResponseBuffer=true;
HttpContextCurrentResponseClearContent();
HttpContextCurrentResponseClearHeaders();
fileName=stringFormat("xhmd{:yyMMddHHmm}xls"DateTimeNow);
HttpContextCurrentResponseAppendHeader("ContentDisposition""attachment;filename="+fileName);
HttpContextCurrentResponseContentType="application/vndmsexcel";
StringWritertw=newSystemIOStringWriter();
HtmlTextWriterhw=newSystemWebUIHtmlTextWriter(tw);
gvwRenderControl(hw);
if(!stringIsNullOrEmpty(title))
{
HttpContextCurrentResponseWrite("<b><center><fontsize=face=Verdanacolor=#FF>"+title+"</font></center></b>");
}
HttpContextCurrentResponseWrite(twToString());
HttpContextCurrentResponseFlush();
HttpContextCurrentResponseClose();
HttpContextCurrentResponseEnd();
gvwDispose();
twDispose();
hwDispose();
gvw=null;
tw=null;
hw=null;
}
publicstaticvoidDataTableExcel(SystemDataDataTabledtData)
{
SystemWebUIWebControlsDataGriddgExport=null;
//当前对话
SystemWebHttpContextcurContext=SystemWebHttpContextCurrent;
//IO用于导出并返回excel文件
SystemIOStringWriterstrWriter=null;
SystemWebUIHtmlTextWriterhtmlWriter=null;
if(dtData!=null)
{
//设置编码和附件格式
curContextResponseContentType="application/vndmsexcel";
curContextResponseContentEncoding=SystemTextEncodingUTF;
curContextResponseCharset="";
//导出excel文件
strWriter=newSystemIOStringWriter();
htmlWriter=newSystemWebUIHtmlTextWriter(strWriter);
//为了解决dgData中可能进行了分页的情况需要重新定义一个无分页的DataGrid
dgExport=newSystemWebUIWebControlsDataGrid();
dgExportDataSource=dtDataDefaultView;
dgExportAllowPaging=false;
dgExportDataBind();
//返回客户端
dgExportRenderControl(htmlWriter);
curContextResponseWrite(strWriterToString());
curContextResponseEnd();
}
}
}