关闭

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

590人阅读 评论(0) 收藏 举报
分类:

<%@ 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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:124914次
    • 积分:1384
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:66篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论
    vajoy的链接