在头部<head>加入
打印事件
<script language=javascript>
function doPrint() {
bdhtml=window.document.body.innerHTML;
sprnstr= "<!--startprint-->";
eprnstr= "<!--endprint-->";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
}
</script>
<script language=javascript>
function doPrint() {
bdhtml=window.document.body.innerHTML;
sprnstr= "<!--startprint-->";
eprnstr= "<!--endprint-->";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
}
</script>
在<body>间加入
打印按钮和打印设置
<OBJECT id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0"
width="0" VIEWASTEXT>
</OBJECT>
<div align="center">
<a href="javascript:; " onClick= "doPrint() "><img src="images/d-y.jpg" border="0" /></a>
<input class="NOPRINT" onclick="document.all.WebBrowser.ExecWB(8,1)" type="button" title="打印设置">
</div>
<OBJECT id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0"
width="0" VIEWASTEXT>
</OBJECT>
<div align="center">
<a href="javascript:; " onClick= "doPrint() "><img src="images/d-y.jpg" border="0" /></a>
<input class="NOPRINT" onclick="document.all.WebBrowser.ExecWB(8,1)" type="button" title="打印设置">
</div>
打印的内容
打印的内容
<!--startprint-->
<form id="form1" runat="server">
<table class="tabp" width="650" height="700" align="center">
<tr>
<td align="center" valign="top" bgcolor="#FFFFFF">
<!--打印的内容-->
</td>
</tr>
</table>
</form>
<!--endprint-->
按下打印按钮后,JS代码截取<!--startprint-->和<!--endprint--> 之间的内容打印<!--startprint-->
<form id="form1" runat="server">
<table class="tabp" width="650" height="700" align="center">
<tr>
<td align="center" valign="top" bgcolor="#FFFFFF">
<!--打印的内容-->
</td>
</tr>
</table>
</form>
<!--endprint-->
页面设置按钮是为了去除页脚页面,默认是会打印页面的路径,即http://www.???.aspx?Id=?和页面的Title。
那俩个标记一定要放在form外面,要不会多一行内容出来。
gridview导出到excel
导出事件
protected void btnLoad_Click(object sender, EventArgs e)
{
Response.Buffer = true;
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8) + ".xls\"");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void btnLoad_Click(object sender, EventArgs e)
{
Response.Buffer = true;
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8) + ".xls\"");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
需要增加一下方法,要不会报错
需增加的一个方法
public override void VerifyRenderingInServerForm( Control control )
{}
public override void VerifyRenderingInServerForm( Control control )
{}