VaJoy童鞋的专栏

来听小V童鞋唠嗑

【转】js打印表格打印预览

<%@ 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></title>
    <script type="text/javascript">
        var hkey_root, hkey_path, hkey_key
        hkey_root = "HKEY_CURRENT_USER"
        hkey_path = " 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 setdivhidden(id) {//把指定id以外的层统统隐藏
            var divs = document.getElementsByTagName("DIV");
            for (var i = 0; i < divs.length; i++) {
                if (divs.item(i).id != id)
                    divs.item(i).style.display = "none";
            }
        }
        function setdivvisible(id) {//把指定id以外的层统统显示
            var divs = document.getElementsByTagName("DIV");
            for (var i = 0; i < divs.length; i++) {
                if (divs.item(i).id != id)
                    divs.item(i).style.display = "block";
            }
        }
        function printpr() //预览函数
        {
            pagesetup_null(); //预览之前去掉页眉,页脚
            setdivhidden("div1"); //打印之前先隐藏不想打印输出的元素
            var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
            document.body.insertAdjacentHTML('beforeEnd', WebBrowser); //在body标签内加入html(WebBrowser activeX控件)
            WebBrowser1.ExecWB(7, 1); //打印预览
            WebBrowser1.outerHTML = ""; //从代码中清除插入的html代码
            pagesetup_default(); //预览结束后页眉页脚恢复默认值
            setdivvisible("div1"); //预览结束后显示按钮
        }
        function print() //打印函数
        {
            pagesetup_null(); //打印之前去掉页眉,页脚
            setdivhidden("div1"); //打印之前先隐藏不想打印输出的元素
            var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
            document.body.insertAdjacentHTML('beforeEnd', WebBrowser); //在body标签内加入html(WebBrowser activeX控件)
            WebBrowser1.ExecWB(6, 1); //打印
            WebBrowser1.outerHTML = ""; //从代码中清除插入的html代码
            pagesetup_default(); //打印结束后页眉页脚恢复默认值
            setdivvisible("div1"); //打印结束后显示按钮
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="div0">
        <input type="button" value="打印预览" onclick="printpr()">
        <input type="button" onclick="print()" value="打印">
        表格一:<br>
    </div>
    <div id="div1">
        <table>
            <tr>
                <td>
                    1111
                </td>
                <td>
                    1111
                </td>
                <td>
                    1111
                </td>
            </tr>
            <tr>
                <td>
                    1221
                </td>
                <td>
                    1331
                </td>
                <td>
                    1411
                </td>
            </tr>
            <tr>
                <td>
                    12111
                </td>
                <td>
                    1121
                </td>
                <td>
                    111
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

阅读更多
个人分类: js/JQ
上一篇“正则表达式(/[^0-9]/g,'')中的&quot;/g&quot;是什么意思”
下一篇javascript事件模型框架-eventutil.js
想对作者说点什么? 我来说一句

js调用word 直接打印预览功能

2010年03月29日 734B 下载

没有更多推荐了,返回首页

关闭
关闭