以下是我从网上查到的相关代码,稍微改了改,但运行后点击"打印"或"打印预览"按钮会报网页有错误或wb未定义之类的错误,不能打印,请高手帮看看,谢谢!<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>JavaScript利用IE内置打印控件IEWebBrowser进行打印/打印页面设置/打印预览/控制分页打印</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" /><script type="text/javascript"> 
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER";
<!--地址的写法很严格的用双斜杠-->
hkey_path="\Software\Microsoft\Internet Explorer\PageSetup";//设置网页打印的页眉页脚为空
function pagesetup_null(){
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="\header";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
hkey_key="\footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
}catch(e){}
}//设置网页打印的页眉页脚为默认值
function pagesetup_default(){
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="\header" ;
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b页码,&p/&P");
hkey_key="\footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d");
}catch(e){}
}function printsetup(){ 
wb.execwb(8,1); // 打印页面设置

function printpreview(){ 
wb.execwb(7,1);// 打印页面预览

function printit() { 
if (confirm('确定打印吗?')) { 
wb.execwb(6,1); //打印


</script><!--media=print 这个属性说明可以在打印时有效-->
<!--希望打印时不显示的内容设置class="Noprint"样式-->
<!--希望人为设置分页的位置设置class="PageNext"样式-->
<style media="print">
<!--
.Noprint{display:none;}
.PageNext{page-break-after:always;}
-->
</style>
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
</head><body>
    <form id="form1" runat="server">
<!--IE内置打印控件IEWebBrowser-->
<object id="wb" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" name="wb"></object>
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" class="STYLE1">
<tr>
    <td height="17" colspan="3" bgcolor="#0099CC">&gt;&gt;监控信息&gt;&gt;</td>
</tr>
<tr>
    <td height="27" align="center" bgcolor="#C6C4DF">序号</td>
    <td align="center" bgcolor="#C6C4DF">学校代码</td>
    <td align="center" bgcolor="#C6C4DF">设备状态</td>
</tr>
    <tr>
    <td height="17" align="center">1</td>
    <td height="17" align="center">1001</td>
    <td height="17" align="center">正常</td>
</tr>
<tr class="PageNext"><!--希望人为设置分页的位置设置class="PageNext"样式-->
    <td height="17" align="center">2</td>
    <td height="17" align="center">1002</td>
    <td height="17" align="center">正常</td>
</tr>
<tr>
    <td height="17" align="center">3</td>
    <td height="17" align="center">1003</td>
    <td height="17" align="center">正常</td>
</tr>
</table>
<div align="center">
<!--希望打印时不显示的内容设置class="Noprint"样式-->
<input onclick="javascript:printit()"; type="button" value="打印" name="button_print" class="Noprint"/> 
<input onclick="javascript:printsetup()"; type="button" value="打印页面设置" name="button_setup" class="Noprint" /> 
<input onclick="javascript:printpreview()"; type="button" value="打印预览" name="button_show" class="Noprint" /> 
<input type="button" value="清空页码" onclick="javascript:pagesetup_null()" class="Noprint" />
<input type="button" value="恢复页码" onclick="javascript:pagesetup_default()" class="Noprint" />
</div></form>
</body>
</html>

解决方案 »

  1.   

    最好使用document.getElementByID('wb')来获得activex对象。
    更多信息看看这里http://blog.csdn.net/ETZRP/archive/2009/01/04/3706220.aspx
      

  2.   

    我发现就是Javascript语句中不认wb 所以会报错,如:
    function printpreview(){ 
    wb.execwb(7,1); // 这里的wb不认 
    } 应该如此解决此问题呢?请高手帮帮忙,谢谢!
      

  3.   

    自己摸索解决了,就是用以下的语句:// 打印页面预览
    function printpreview(){ 
        document.all.WebBrowser.ExecWB(7,1);
      

  4.   

    关注
    是不是这样
    <script type="text/javascript">
       function Print()
       {
            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>
      

  5.   

    不是,打印用的是这个:// 打印
    function printit() { 
    if (confirm('确定打印吗?')) { 
        document.all.WebBrowser.execwb(6,1); //打印