js代码:

    function preview(ps, pe, bt) {

    document.getElementById(bt).style.display = "block";

    bdhtml = window.document.body.innerHTML;

    sprnstr = ps;

    eprnstr = pe;

    prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 18);

    prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));

    var newWin = window.open("/printer.aspx", '', '');

    newWin.document.write("<style type='text/css'>.a_blue{color:#0052AC;font-weight:normal;text-decoration:none;}table{width:620px;}</style>");

    newWin.document.write("<center><table><tr><td>");

    newWin.document.write(prnhtml);

    now = new Date();

    hours = now.getHours();  //得到小时

    minutes = now.getMinutes();  //得到分钟

    seconds = now.getSeconds();  //得到秒 

    year = now.getFullYear();  //得到年份

    month = now.getMonth() + 1;  //得到月份

    date = now.getDate();  //得到日期

    var dysj = year + "年" + month + "月" + date + "日 " + hours + ":" + minutes + ":" + seconds;

    newWin.document.write("</td></tr><tr><td align='right' style='font-size:10px'><br/><br/><br/>打印时间:" + dysj + "");

    newWin.document.write("</td></tr></center>");

    newWin.document.location.reload();

    newWin.print();

    newWin.close();

    document.getElementById(bt).style.display = "none";

}

前台代码:

    <body>

        <!--startprint1-->

          <div id="bt1" style="display:none"></div>

          <div>这里是指定的打印区域哦</div>

        <!--endprint1-->

        <input type="button" id="Button1" value="打印" />

    </body>