ASP.NET 打印
通过缓存Cache,来缓存Gridview里数据,在需要打印时就可以直接打印了。
打印界面:
缓存类:
Code
public class CacheData
{
private static CacheData instance;
private CacheData() { }
public static CacheData GetCache()
{
if (instance == null)
{
instance = new CacheData();
}
return instance;
}
public void CacheDataSet(DataTable dt) //设置要缓存的数据
{
HttpRuntime.Cache.Insert("printDT", dt, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
}
public DataTable GetCacheData() //返回缓存的数据
{
if ((DataTable)HttpRuntime.Cache["printDT"] != null)
return (DataTable)HttpRuntime.Cache["printDT"];
else return null;
}
}
public class CacheData
{
private static CacheData instance;
private CacheData() { }
public static CacheData GetCache()
{
if (instance == null)
{
instance = new CacheData();
}
return instance;
}
public void CacheDataSet(DataTable dt) //设置要缓存的数据
{
HttpRuntime.Cache.Insert("printDT", dt, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
}
public DataTable GetCacheData() //返回缓存的数据
{
if ((DataTable)HttpRuntime.Cache["printDT"] != null)
return (DataTable)HttpRuntime.Cache["printDT"];
else return null;
}
}
在数据绑定时就 CacheData.GetCache().CacheDataSet(dt); //缓存数据,用以打印
打印界面代码
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Printer.aspx.cs" Inherits="DataManage_AppComFun_DeptManage_Printer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>打印设置</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<!--media=print 这个属性可以在打印时有效-->
<style media=print type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
<style type="text/css">
.tdp
{
border-bottom: 1 solid #000000;
border-left: 1 solid #000000;
border-right: 0 solid #ffffff;
border-top: 0 solid #ffffff;
}
.tabp
{
border-color: #000000 #000000 #000000 #000000;
border-style: solid;
border-top-width: 2px;
border-right-width: 2px;
border-bottom-width: 1px;
border-left-width: 1px;
}
.NOPRINT
{
font-family: "宋体";
font-size: 9pt;
}
</style>
</head>
<body style="text-align:center">
<center class="Noprint" >
<object id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0">
</object>
<input type="button" value="打印" onclick="document.all.WebBrowser.ExecWB(6,1)" />
<input type="button" value="直接打印" onclick="document.all.WebBrowser.ExecWB(6,6)" />
<input type="button" value="页面设置" onclick="document.all.WebBrowser.ExecWB(8,1)" />
<input type="button" value="打印预览" onclick="document.all.WebBrowser.ExecWB(7,1)" />
<hr align="center" width="90%" size="1" noshade style="height: 1px">
</center>
<form id="form1" runat="server">
<div style="text-align:center;margion:0px">
<asp:GridView ID="gvPrint" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Printer.aspx.cs" Inherits="DataManage_AppComFun_DeptManage_Printer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>打印设置</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<!--media=print 这个属性可以在打印时有效-->
<style media=print type="text/css">
.Noprint{display:none;}
.PageNext{page-break-after: always;}
</style>
<style type="text/css">
.tdp
{
border-bottom: 1 solid #000000;
border-left: 1 solid #000000;
border-right: 0 solid #ffffff;
border-top: 0 solid #ffffff;
}
.tabp
{
border-color: #000000 #000000 #000000 #000000;
border-style: solid;
border-top-width: 2px;
border-right-width: 2px;
border-bottom-width: 1px;
border-left-width: 1px;
}
.NOPRINT
{
font-family: "宋体";
font-size: 9pt;
}
</style>
</head>
<body style="text-align:center">
<center class="Noprint" >
<object id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0">
</object>
<input type="button" value="打印" onclick="document.all.WebBrowser.ExecWB(6,1)" />
<input type="button" value="直接打印" onclick="document.all.WebBrowser.ExecWB(6,6)" />
<input type="button" value="页面设置" onclick="document.all.WebBrowser.ExecWB(8,1)" />
<input type="button" value="打印预览" onclick="document.all.WebBrowser.ExecWB(7,1)" />
<hr align="center" width="90%" size="1" noshade style="height: 1px">
</center>
<form id="form1" runat="server">
<div style="text-align:center;margion:0px">
<asp:GridView ID="gvPrint" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
后代代码:
Code
if (!IsPostBack)
{
if (CacheData.GetCache().GetCacheData()!=null)
{
this.gvPrint.DataSource = CacheData.GetCache().GetCacheData();
this.gvPrint.DataBind();
}
else Alert("没有要打印的数据,不能打印!");
}
if (!IsPostBack)
{
if (CacheData.GetCache().GetCacheData()!=null)
{
this.gvPrint.DataSource = CacheData.GetCache().GetCacheData();
this.gvPrint.DataBind();
}
else Alert("没有要打印的数据,不能打印!");
}
如果本文的描述的方法或内容有问题,请给我留言。