將下列 ScriptX 元件加入 .aspx 網頁中
<object id=factory style="display:none"
classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
codebase="http://[your path here]/ScriptX.cab#Version=6,1,432,1">
</object>
[your path here] 為伺服器名稱或 IP 及存入 ScriptX.cab 之路徑
設定列印格式之語法:

代码
<script>
function printWindow( ) {
factory.printing.header = "This is MeadCo"; '頁首
factory.printing.footer = "Printing by ScriptX"; '頁尾
factory.printing.portrait = false; '橫印
factory.printing.leftMargin = 1.0;
factory.printing.topMargin = 1.0;
factory.printing.rightMargin = 1.0;
factory.printing.bottomMargin = 1.0;
factory.printing.Print(false);
}
</script>
設計按鈕執行列印:
<input type="button" name="Print" value="列印" onclick="printWindow('');">
====================================================================
一、 介绍ScriptX控件
对于B/S架构的项目中,Web打印最是让程序员头痛的技术,在这次项目中运用了第三方控件ScriptX解决了此技术包括各种汇总表,详细清单等等,但最大弊端就是打印的格式是死的,你要打印出怎样的格式必须自己来做,还有就是打印分页,它是按你设置的纸张大小来分的,所以必须人为控制,ScriptX控件所需文件ScriptX.cab。(ScriptX下载地址
http://www.meadroid.com/scriptx/freedep.asp )
打印功能使用:
⑴ 用户在客户端单击“打印”按钮,第一次使用打印会弹出如下对话框
⑵ 单击“是”按钮,此时会把控件ScriptX下载到本机
⑶ 在打印窗口中有大标题、打印条件、打印列表,单击“打印”按钮进入打印浏览界面。
⑷ 单击“Print”按钮,听打印机声音响起。
二、 一个简单的ScriptX控件使用实例
1.界面文件 WebExample.aspx (其中kg.css是界面样式在这里就不介绍了)
<%@ Page language="c#" Codebehind="WebExample.aspx.cs" AutoEventWireup="false" Inherits="WebPrint.WebExample" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>一个简单的ScriptX打印</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="kg.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="WebExample" method="post" runat="server">
<TABLE width="100%" height="100%">
<tr>
<td height="100">
</td>
</tr>
<tr>
<td style="HEIGHT: 14px" align="center">
<div id="divTableResult" name="divTableResult">
<asp:Label id="Label1" runat="server" Width="232px" Height="32px" Font-Size="Large">欢迎使用ScriptX打印</asp:Label>
</div>
</td>
</tr>
<tr>
<td align="center" valign="top">
<div id="divChartResult" name="divChartResult">
<asp:Image id="Image1" runat="server" ImageUrl="win2000.gif"></asp:Image>
</div>
</td>
</tr>
<tr>
<td align="center" valign="top">
<input id="btnPrint" style="WIDTH: 32px; HEIGHT: 19px" onclick="printResult();" type="button"
size="1" value="打印" name="btnPrint">
</td>
</tr>
</TABLE>
</form>
<script language="javascript">
function printResult()
{
//针对不同模块,设置打印参数
var title = 'ScriptX打印';//报表名称
var orien = 'true';//打印页面方向,true:纵向打印/false横向打印
//参数设置结束
var strURL;
title = escape(title);
//condition = escape(condition);
strURL = 'printContainer2.aspx?title='+title+'&orien='+orien;
window.open(strURL,'printResult','height=600, width=800, top=0, left=50,toolbar=no , menubar=no, scrollbars=yes, resizable=no, location=no, status=no');
}
</script>
</body>
</HTML>
说明:
⑴ <div id="divTableResult" name="divTableResult">和<div id="divChartResult" name="divChartResult"> 是在printContainer2.aspx文件中定义的传值标签,因此必须要有。
⑵ printResult()方法用来声明传值内容,具体在方法中已说明
2.打印界面 printContainer2.aspx (其中print.css是打印样式就不介绍了)
<%@ Page language="c#" Codebehind="printContainer2.aspx.cs" AutoEventWireup="false" Inherits="WebPrint.printContainer2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<%
string title = "查询打印";
‘用于title获取标题
if(Request.QueryString["title"].ToString().Trim()!=""){
title = Request.QueryString["title"].ToString().Trim();
}
‘用于orientation获取打印页面方向,true:纵向打印/false横向打印
string orientation = "true";
if(Request.QueryString["orien"].ToString().Trim()!=""){
orientation = Request.QueryString["orien"].ToString().Trim();
}
%>
<html>
<head>
<title>ScriptX 打印</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href="kg.css" rel="stylesheet" type="text/css">
<link href="print.css" rel="stylesheet" type="text/css" media="print" >
<script FOR=document event="onclick">
if(event.srcElement.tagName=='A'){
//alert('a click!');
event.cancelBubble = true;
}
</script>
<style type="text/css">
a:link {text-decoration:none}
a:visited {text-decoration:none}
a:active {text-decoration:none}
a:hover {text-decoration:none}
A { COLOR: #000000;TEXT-DECORATION: none; FONT-SIZE: 8pt;FONT-FAMILY: "";}
</style>
</head>
<body MS_POSITIONING="GridLayout" >
<!--这里调用控件ScriptX.cab-->
<object id="factory" viewastext style="display:none"
classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
codebase="ScriptX.cab#Version=6,1,431,2">
</object>
<SCRIPT defer>
//用于设置打印参数
function printBase() {
factory.printing.footer = "&b ScriptX 打印 &b" //页眉
factory.printing.footer = "" //页脚
factory.printing.portrait = <%=orientation%> //true为纵向打印,flase为横向打印
factory.printing.leftMargin = 1.5 //左页边距
factory.printing.topMargin = 0.5 //上页边距
factory.printing.rightMargin = 0.5 //右页边距
factory.printing.bottomMargin = 1.0 //下页边距
}
</SCRIPT>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bordercolor="#FFFFFF">
<tr bordercolor="#FFFFFF">
<td >
<table width="70%" border="0" cellspacing="0" cellpadding="2" align="center">
<tr>
<td align="center"><font size="5" face="宋体"><b><div name="divTitle" id="divTitle"></div></b></font></td>
</tr>
<tr>
<td align="center">
<div id="divReport" name="divReport">
<input type="button" name="cmdPRINT" id="cmdPRINT" onclick="printReport()" value="打印">
<input type="button" onclick="window.close();" value="关闭">
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="2" align="center">
<tr><td align="center">
<!-- 用于显示divTableContainer标签的值 -->
<div id="divTableContainer" name="divTableContainer"></div>
</td></tr>
<tr><td align="center">
<!-- 用于显示divChartContainer标签的值 -->
<div id="divChartContainer" name="divChartContainer"></div>
</td></tr>
</table>
</body>
<script language="javascript">
//使界面最大化
maxWin();
var needcon='';
document.all.divTitle.innerText = unescape('<%=title%>');
//用于把父窗体的divTableResult和divChartResult标签的值分别转入divTableContainer 和divChartContainer标签中
var tableContent = window.opener.document.all.divTableResult.innerHTML;
tableContent = replaceAll(tableContent,"<A ","<span ");
tableContent = replaceAll(tableContent,"</A> ","</span> ");
tableContent = replaceAll(tableContent,"<a ","<span ");
tableContent = replaceAll(tableContent,"</a> ","</span> ");
tableContent = replaceAll(tableContent,"<input ","<font ");
tableContent = replaceAll(tableContent,"<INPUT ","<font ");
document.all.divTableContainer.innerHTML = tableContent;
document.all.divChartContainer.innerHTML = window.opener.document.all.divChartResult.innerHTML;
//用于调用设置打印参数的方法和显示预览界面
function printReport(){
printBase();
//window.print();
factory.printing.Preview();
}
function replaceAll( str, from, to ) {
var idx = str.indexOf( from );
while ( idx > -1 ) {
str = str.replace( from, to );
idx = str.indexOf( from );
}
return str;
}
function maxWin()
{
var aw = screen.availWidth;
var ah = screen.availHeight;
window.moveTo(0, 0);
window.resizeTo(aw, ah);
}
</script>
</html>
==================================================================================\
web打印一直困扰着大家,小弟最近做一项目,稍有心得,特来跟大家交流交流。
我在那个项目中是利用scriptx.cab这个控件来解决问题的,包括各种汇总表,周详清单等等,不过最大弊端就是打印的格式是死的,你要打印出怎样的格式必须自己来做,更有就是打印分页,他是按你设置的纸张大小来分的,所以必须人为控制,我已搞定了。
scriptx.cab大家能到网上去down一个。
好了,废话不多说了,代码才是大家最需要看到的。
包括三个文件,一个是框架页,一个是工具栏页,还一个就是打印的内容页了。
框架页:
<html>
<head>
<title>打印 / 预览 / 库存汇总表</title>
<link type="text/css" rel="stylesheet" href="style.css">
<script language="javascript" src="main.js"></script>
<script language="javascript">maxwin();</script>
</head>
<frameset rows="42,*" style="border:none">
<frame src="toolbar.asp" name="toolbar" target="mainbar" frameborder="0" scrolling="no" noresize>
<frame src="kc.asp" name="mainbar" target="mainbar" frameborder="0" scrolling="auto" noresize>
</frameset>
</html>
工具栏页:([预览],[设置]和[打印]等按钮,代码里边的图片大家自己去做一个好了)
<body bgcolor="#d4d0c8" topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" onselectstart="return false" oncontextmenu="return false">
<table width="100%" height="41" cellpadding="0" cellspacing="0" border="0" background="../images/bg_toolbar.gif">
<tr>
<td width="10"><img src="../images/bar_toolbar.gif"></td>
<td width="85" align="center"><img src="../images/ico_print.gif" onmouseover="this.src= ../images/ico_print1.gif " onmousedown="this.src= ../images/ico_print2.gif " onmouseout="this.src= ../images/ico_print.gif " onclick="top.mainbar.factory.printing.print(false);"></td>
<td width="2"><img src="../images/line_toolbar.gif"></td>
<td width="85" align="center"><img src="../images/ico_setup.gif" onmouseover="this.src= ../images/ico_setup1.gif " onmousedown="this.src= ../images/ico_setup2.gif " onmouseout="this.src= ../images/ico_setup.gif " onclick="top.mainbar.factory.printing.pagesetup();"></td>
<td width="2"><img src="../images/line_toolbar.gif"></td>
<td width="85" align="center"><img src="../images/ico_view.gif" onmouseover="this.src= ../images/ico_view1.gif " onmousedown="this.src= ../images/ico_view2.gif " onmouseout="this.src= ../images/ico_view.gif " onclick="top.mainbar.factory.printing.preview();"></td>
<td width="2"><img src="../images/line_toolbar.gif"></td>
<td> </td>
</tr>
</table>
</body>
内容页:(代码里边的数据查询请大家换成自己的数据查询)
<style>
td{font-size:14px;line-height:180%}
.ybk{border-right:1px #000000 solid;}
.sbk{border-top:1px #000000 solid;}
</style>
<!--这里调用控件scriptx.cab-->
<object id="factory" style="display:none" viewastext classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" codebase="scriptx.cab#version=5,60,0,360"></object>
<script defer>
function window.onload() {
factory.printing.header = "" //页眉
factory.printing.footer = "" //页脚
factory.printing.portrait = false //true为纵向打印,flase为横向打印
factory.printing.leftmargin = 15.0 //左页边距
factory.printing.topmargin = 5.0 //上页边距
factory.printing.rightmargin = 0.75 //右页边距
factory.printing.bottommargin = 1.5 //下页边距
}
</script>
<body oncontextmenu="return false">
<!--#include file="conn.asp"--
>
<%
sql="exec showkc_print"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,3,2
if not rs.eof then
rs.pagesize=21 每页记录条数即每页打印的条数
xs=rs.pagesize
rc=rs.pagecount %>
<!--自动分页开始-->
<% for n = 1 to rc
xs_s=(xs*n-xs)+1 每页起始序数
xs_e=xs*n 每页结束序数 %>
<p>
<table width="1000" height="700" cellpadding="0" cellspacing="0" border="0">
<tr><td valign="top">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td height="50" align="center" valign="top"><font size="5">库存汇总表</font></td></tr>
</table>
<table width="1000" cellpadding="0" cellspacing="0" style="border:1px #000000 solid" align="center">
<tr height="20" align="center">
<td width="5%" class="ybk">序号</td>
<td width="10%" class="ybk">人工编号</td>
<td width="15%" class="ybk">材料名称</td>
<td width="25%" class="ybk">型号规格</td>
<td width="10%" class="ybk">单价</td>
<td width="8%" class="ybk">数量</td>
<td width="12%" class="ybk">金额</td>
<td width="5%" class="ybk">单位</td>
<td width="10%">更新日期</td>
</tr>
<%
dim i
i=1
do while not rs.eof and i<=xs %>
<tr><td colspan="10" height="1"><img src="../images/dot.gif" width="100%" height="1"></td></tr>
<tr height="20">
<td width="5%" class="ybk"> <%=xs_s+(i-1)%></td>
<td width="10%" class="ybk"> <%=rs("rgbh")%></td>
<td width="15%" class="ybk"> <%=rs("clmc")%></td>
<td width="25%" class="ybk"> <%=rs("clxh")%></td>
<td width="10%" class="ybk" align="right"><%=formatnumber(rs("pjj"),3,true)%> </td>
<td width="8%" class="ybk" align="right"><%=formatnumber(rs("kcsl"),2,true)%> </td>
<td width="12%" class="ybk" align="right"><%xj=formatnumber(rs("pjj"),3)*formatnumber(rs("kcsl"),2)%><%=formatnumber(xj,2,true)%> </td>
<td width="5%" class="ybk"> <%=rs("dw")%></td>
<td width="10%"> <%=datevalue(rs("gxsj"))%></td>
</tr>
<%
rs.movenext
i=i+1
hj=hj+xj
zj=zj+xj
loop %>
<% if rc>1 then %>
<tr><td colspan="10" height="1"><img src="../images/dot.gif" width="100%" height="1"></td></tr>
<tr height="20">
<td width="5%" class="ybk"> </td>
<td width="10%" class="ybk"> </td>
<td width="15%" class="ybk"> </td>
<td width="25%" class="ybk"> 本 页 合 计</td>
<td width="8%" class="ybk"> </td>
<td width="8%" class="ybk"> </td>
<td width="12%" class="ybk" align="righ
t"><%=formatnumber(hj,2,true)%><% hj=0%> </td>
<td width="5%" class="ybk"> </td>
<td width="10%"> </td>
</tr>
<% end if %>
<% if n=rc then %>
<tr><td colspan="10" height="1"><img src="../images/dot.gif" width="100%" height="1"></td></tr>
<tr height="20">
<td width="5%" class="ybk"> </td>
<td width="10%" class="ybk"> </td>
<td width="15%" class="ybk"> </td>
<td width="25%" class="ybk"> 总 计</td>
<td width="8%" class="ybk"> </td>
<td width="8%" class="ybk"> </td>
<td width="12%" class="ybk" align="right"><%=formatnumber(zj,2,true)%> </td>
<td width="5%" class="ybk"> </td>
<td width="10%"> <%=now%></td>
</tr>
<% end if %>
</table>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td height="15"> </td></tr>
<tr><td height="30" align="center"><font size="3">第<%=n%>页,共<%=rc%>页</font></td></tr>
</table>
</td></tr>
</table>
<% next %>
<!--分页结束-->
<%
end if
rs.close
set rs=nothing
conn.close
set conn=nothing %>
以上代码实现一个类似excel格式的库存汇总表打印,大家能把代码拷过去感觉一下,效果还是不错的,就如我上面所说,因为他的打印格式是要自己做的,所以大家能改为自己想要的格式,必须自己做了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述