yuezhonghu

悠然现南山...
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

简单的打印功能JS实现

Posted on 2008-01-14 16:50  .狐狸血.  阅读(8385)  评论(2编辑  收藏  举报

打印的设置:
关于这个组件还有其他的用法,列举如下:
WebBrowser.ExecWB(1,1) 打开
Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口
Web.ExecWB(4,1) 保存网页
Web.ExecWB(6,1) 打印
Web.ExecWB(7,1) 打印预览
Web.ExecWB(8,1) 打印页面设置
Web.ExecWB(10,1) 查看页面属性
Web.ExecWB(15,1) 好像是撤销,有待确认
Web.ExecWB(17,1) 全选
Web.ExecWB(22,1) 刷新
Web.ExecWB(45,1) 关闭窗体无提示


JS代码:

// JScript 文件
function preview1() {
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();
         }
        
         //调用该函数的页面上需要添加打印的起始位置
         //开始: <!--startprint-->
         //结束:<!--endprint-->
         //这中间包含的就是打印的内容
        //打印宽度最好是 600px

实例:

<script language="JavaScript" src="js/printer.js" mce_src="js/printer.js"></script>

 <!--startprint-->
                  //需要打印的文档和图片放在此                 
<!--endprint-->

<a href="javascript:preview1();" mce_href="javascript:preview1();" class="reda">打印此页</a>

 

点击打印此页即可

简单实用...

------------------------

报表打印
JS代码

// JScript 文件
//要打印ReportView报表的内容,只需要引用本文件,然后调用PrintReportView()函数即可。
//例如:在某按钮的点击事件中包括代码,onclick="PrintReportView(window,'ReportViewerYsqd');"

//得到ReportView控件生成的客户端代码的报表内容区的FRAME对象
//参数:objWindow——包含ReportView控件的window对象
// strReportViewerId——需要被打印的ReportViewer控件的ID
//返回:得到的报表内容区FRAME对象;如果获取失败,返回null。
function GetReportViewContentFrame(objWindow,strReportViewerId)
{
var frmContent=null; //报表内容区对象的FRAME对象
var strFrameId="ReportFrame" + strReportViewerId ; //asp.net自动生成的iframe 的id为:ReportFrame+报表控件id ReportFrameReportViewer1
try
{
frmContent=window.frames[strFrameId].frames["report"]; //报表内容框架的id为report
}
catch(e)
{
}
return frmContent;
}

//打印ReportView控件中的报表内容
//参数:objWindow——包含ReportView控件的window对象
// strReportViewerId——需要被打印的ReportViewer控件的ID
//返回:(无)
function PrintReportView(objWindow,strReportViewerId)
{
var frmContent=GetReportViewContentFrame(objWindow,strReportViewerId);
if(frmContent!=null && frmContent!=undefined)
{
frmContent.focus();
frmContent.print();
}
else
{
alert("在获取报表内容时失败,无法通过程序打印。如果要手工打印,请鼠标右键点击报表内容区域,然后选择菜单中的打印项。");
}
}

HTML中的引用
头引用
<script language="JavaScript" src="Js/ReportView.js"></script>
BODY中的代码(此处ReportViewer报表控件略去)
<input id="Button2" type="button" value="打印面单" onclick="javascript:PrintReport();" />
<script language="javascript" type="text/javascript">
<!--
//打印报表
function PrintReport()
{
PrintReportView(window,"ReportViewer1");
return false;
}
//-->
</script>