关于前端table的Excel打印

小功能说明:
直播带货每个品所讲解时间的打点记录以及形成的表格能Excel打印输出

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LiveTimeTable</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #liveTimeBox {
            width: 1000px;
            margin: 30px auto 0;
        }

        #liveTimeBox table {
            width: 100%;
            border-spacing: 0;
            font-size: 16px;
        }

        #liveTimeBox h1 {
            text-align: center;
        }

        #liveTimeBox table th {
            border: 1px solid rgba(0, 0, 0, 0.6);
            text-align: center;
            background-color: skyblue;
        }

        #liveTimeBox table td {
            border: 1px solid rgba(0, 0, 0, 0.6);
            border-top: none;
            text-align: center;
        }

        #liveTimeBox div {
            margin-top: 5px;
            width: 100%;
        }

        #liveTimeBox div button {
            outline: none;
            width: 92px;
            border: none;
            font-weight: bold;
            border-radius: 3px;
            cursor: pointer;
        }

        #liveTimeBox td button {
            font-weight: bold;
            outline: none;
            width: 40%;
            border: none;
            margin-bottom: 2px;
            border-radius: 3px;
            margin-left: 5px;
            cursor: pointer;
        }

        #liveTimeBox div button.add {
            float: right;
            cursor: pointer;

        }

        #liveTimeBox div button.start {
            float: left;
            cursor: pointer;
        }

        #liveTimeBox div button.print {
            margin-left: 5px;
            float: left;
            cursor: pointer;

        }

        #liveTimeBox td button:hover {
            background-color: skyblue;
            color: #fff;
        }

        #liveTimeBox div button:hover {
            background-color: skyblue;
            color: #fff;
        }
    </style>
</head>

<body>
    <div id="liveTimeBox">
        <h1>直播时间打点</h1>
        <form action="javascript:">
            <table id="table">
                <tr>
                    <th>产品名称</th>
                    <th>时间段</th>
                    <th>时长/秒</th>
                    <th>操作</th>
                </tr>
            </table>
        </form>
        <div>
            <button class="start">开始打点</button>
            <button class="print">打印</button>
            <button class="add">添加</button>
        </div>
    </div>
    <script type="text/javascript">
        function addEvent(elem, type, fn) {
            if (elem.addEventListener) {
                elem.addEventListener(type, fn, false);
            }
            else if (elem.attachEvent) {
                elem.attachEvent("on" + type, function () {
                    fn.call(elem);
                })
            }
            else {
                elem["on" + type] = fn;
            }
        }

        // 开始打点
        var startBtn = document.getElementsByClassName("start")[0];
        var time1 = "";//初始化时间段
        var time3 = 0;//初始化时长
        startBtn.onclick = function () {
            var thisTime = new Date();
            var timeS = thisTime.getTime();
            time3 = timeS;
            var timeH = thisTime.getHours();
            var timeM;
            if (thisTime.getMinutes() >= 10) {
                timeM = thisTime.getMinutes();
            }
            else {
                timeM = '0' + thisTime.getMinutes();
            }
            time1 = timeH + ":" + timeM;
            this.innerText = "已开始";
            this.style = "background-color:rgba(193, 43, 43, 0.5);"
        }

        // 添加
        var tableBoxChildren = document.getElementById("table").children;
        var addBtn = document.getElementsByClassName("add")[0];
        addEvent(addBtn, "click", addBtnFn);
        function addBtnFn() {
            if (startBtn.innerText == "已开始") {
                var divBox = document.createElement("div");
                divBox.style = "width: 100%;height: 100%;background-color: rgba(000, 000, 000, 0.5);position: fixed;top: 0;left: 0;";
                var formBox = document.createElement("form");
                formBox.style = "width: 500px; height: 300px;left: 0px;position: fixed;right: 0px;top: 0px;bottom: 0px;margin: auto auto;background-color:#fff;border-radius: 5px;";
                formBox.action = "javascript:";
                var h2 = document.createElement("h2");
                h2.style = "width: 100%;text-align: center;height: 100px;line-height: 100px;position: relative;top: 20px;";
                h2.innerText = "产品名称"
                var divBoxTwo = document.createElement("div");
                divBoxTwo.style = "height: 100px;text-align: center;";
                var inputBox = document.createElement("input");
                inputBox.style = "width: 400px;height: 30px;font-size: 20px;outline: none;text-indent: 5px;position: relative;top: 30px;background-color:rgba(128, 203, 233,0.5);";
                var divBoxThree = document.createElement("div");
                divBoxThree.style = "height: 100px;text-align: center;";
                var buttonFalse = document.createElement("button");
                buttonFalse.style = "height: 30px;width: 60px;font-weight: bold;outline: none;position: relative;top: 10px;float: left;margin-left: 80px;line-height: 30px;";
                buttonFalse.innerText = "取消";
                buttonFalse.onclick = function () {
                    document.body.removeChild(divBox);
                }
                var buttonTrue = document.createElement("button");
                buttonTrue.style = "height: 30px;width: 60px;font-weight: bold;outline: none;position: relative;top: 10px;float: right;margin-right: 80px;line-height: 30px;";
                buttonTrue.innerText = "确定";
                buttonTrue.onclick = function () {
                    var trBox = document.createElement("tr");

                    var tdBoxArray = [];
                    for (var i = 0; i < 4; i++) {
                        tdBoxArray[i] = document.createElement("td");
                    }
                    tdBoxArray[0].innerText = inputBox.value;
                    var thisTime = new Date();
                    var time4 = thisTime.getTime();
                    var timeH = thisTime.getHours();
                    var timeM;
                    if (thisTime.getMinutes() >= 10) {
                        timeM = thisTime.getMinutes();
                    }
                    else {
                        timeM = '0' + thisTime.getMinutes();
                    }
                    var time2 = timeH + ":" + timeM;
                    tdBoxArray[1].innerText = time1 + "-" + time2;
                    time1 = time2;
                    tdBoxArray[2].innerText = Math.round((time4 - time3) / 1000);
                    time3 = time4;
                    var tdBtn1 = document.createElement("button");
                    //修改
                    tdBtn1.innerText = "修改";
                    addEvent(tdBtn1, "click", tdBtn1Fn);
                    function tdBtn1Fn(e) {
                        var event = e || window.event;
                        var target = event.target || event.srcElement;
                        var divBox = document.createElement("div");
                        divBox.style = "width: 100%;height: 100%;background-color: rgba(000, 000, 000, 0.5);position: fixed;top: 0;left: 0;";
                        var divB = document.createElement("div");
                        divB.style = "background-color: #fff;width: 800px;margin: 0 auto 0;border: 1px solid rgba(0, 0, 0, 0.6);"
                        var table = document.createElement("table");
                        table.style = "width: 100%;border-spacing: 0;font-size: 16px;";
                        var tr = [];
                        for (var a = 0; a < 2; a++) {
                            tr[a] = document.createElement("tr");
                        }
                        var th = [];
                        var td = [];
                        var input = [];
                        for (var c = 0; c < 3; c++) {
                            td[c] = document.createElement("td");
                            td[c].style = "border: 1px solid rgba(0, 0, 0, 0.6);border-top: none;text-align: center;";
                            input[c] = document.createElement("input");
                            input[c].style = "width: 100%;outline: none;border: none;text-align: center;";
                            th[c] = document.createElement("th");
                            th[c].style = "border: 1px solid rgba(0, 0, 0, 0.6);text-align: center;";
                            input[c].value = target.parentElement.parentElement.children[c].innerText;
                        }
                        th[0].innerText = "产品名称";
                        th[1].innerText = "时间段";
                        th[2].innerText = "时长/秒";
                        var divC = document.createElement("div");
                        var button1 = document.createElement("button");
                        button1.style = "font-weight: bold;outline: none;width: 20%;margin-bottom: 2px;border-radius: 3px;float: left;background-color: skyblue;";
                        button1.innerText = "取消";
                        button1.onclick = function () {
                            document.body.removeChild(divBox);
                        }
                        var button2 = document.createElement("button");
                        button2.style = "font-weight: bold;outline: none;width: 20%;margin-bottom: 2px;border-radius: 3px;float: right;background-color: skyblue;";
                        button2.innerText = "确定";
                        button2.onclick = function () {
                            for (var i = 0; i < 3; i++) {
                                target.parentElement.parentElement.children[i].innerText = input[i].value;
                            }
                            document.body.removeChild(divBox);
                        }
                        for (var i = 0; i < 3; i++) {
                            td[i].appendChild(input[i]);
                        }
                        for (var j = 0; j < 3; j++) {
                            tr[0].appendChild(th[j]);
                            tr[1].appendChild(td[j]);
                        }
                        divC.appendChild(button1);
                        divC.appendChild(button2);
                        for (var b = 0; b < 2; b++) {
                            table.appendChild(tr[b]);
                        }
                        divB.appendChild(table);
                        divB.appendChild(divC);
                        divBox.appendChild(divB);
                        var script = document.getElementsByTagName("script")[0];
                        document.body.insertBefore(divBox, script);
                    }
                    //删除
                    var tdBtn2 = document.createElement("button");
                    tdBtn2.innerText = "删除";
                    tdBtn2.onclick = function () {
                        this.parentElement.parentElement.parentElement.removeChild(this.parentElement.parentElement);
                    }
                    tdBoxArray[3].appendChild(tdBtn1);
                    tdBoxArray[3].appendChild(tdBtn2);
                    for (var j = 0; j < 4; j++) {
                        trBox.appendChild(tdBoxArray[j])
                    }
                    var table = document.getElementsByTagName("table")[0];
                    table.appendChild(trBox);
                    for (var z = 0; z < tableBoxChildren.length; z++) {
                        if (z % 2 == 1) {
                            tableBoxChildren[z].style = "background-color: rgba(135, 171, 238, 0.1);"
                        }
                        else {
                            tableBoxChildren[z].style = "background-color: rgba(210, 100, 122, 0.1);"
                        }
                    }
                    document.body.removeChild(divBox);
                }
                divBoxTwo.appendChild(inputBox);
                divBoxThree.appendChild(buttonFalse);
                divBoxThree.appendChild(buttonTrue);
                formBox.appendChild(h2);
                formBox.appendChild(divBoxTwo);
                formBox.appendChild(divBoxThree);
                divBox.appendChild(formBox);
                var script = document.getElementsByTagName("script")[0];
                document.body.insertBefore(divBox, script);
            }
            else {
                alert("请点击'开始打点'");
            }
        }

        // 打印表格
        var tablesToExcel = (function () {
            var uri = 'data:application/vnd.ms-excel;base64,',
                bookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">' + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>' + '<Styles>' + '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>' + '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>' + '</Styles>' + '{worksheets}</Workbook>',
                sheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>',
                cellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>',
                base64 = function (s) {
                    return window.btoa(unescape(encodeURIComponent(s)))
                },
                format = function (s, c) {
                    return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; })
                }
            return function (tables, wsnames, wbname, appname) {
                var ctx = "";
                var workbookXML = "";
                var worksheetsXML = "";
                var rowsXML = "";

                for (var i = 0; i < tables.length; i++) {
                    if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
                    //导出的行数
                    for (var j = 0; j < tables[i].rows.length; j++) {
                        rowsXML += '<Row>';
                        //导出的列数(减去后面“操作”)
                        for (var k = 0; k < tables[i].rows[j].cells.length - 1; k++) {
                            var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");
                            var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");
                            var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");
                            dataValue = (dataValue) ? dataValue : tables[i].rows[j].cells[k].innerHTML;
                            var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");
                            dataFormula = (dataFormula) ? dataFormula : (appname == 'Calc' && dataType == 'DateTime') ? dataValue : null;
                            ctx = {
                                attributeStyleID: (dataStyle == 'Currency' || dataStyle == 'Date') ? ' ss:StyleID="' + dataStyle + '"' : '',
                                nameType: (dataType == 'Number' || dataType == 'DateTime' || dataType == 'Boolean' || dataType == 'Error') ? dataType : 'String',
                                data: (dataFormula) ? '' : dataValue,
                                attributeFormula: (dataFormula) ? ' ss:Formula="' + dataFormula + '"' : ''
                            };
                            rowsXML += format(cellXML, ctx);//
                        }
                        rowsXML += '</Row>';
                    }
                    ctx = {
                        rows: rowsXML,
                        nameWS: wsnames[i] || 'Sheet' + i
                    };
                    worksheetsXML += format(sheetXML, ctx);
                    rowsXML = "";
                }
                ctx = {
                    created: (new Date()).getTime(),
                    worksheets: worksheetsXML
                };
                workbookXML = format(bookXML, ctx);
                //查看后台的打印输出
                var link = document.createElement("A");
                link.href = uri + base64(workbookXML);
                link.download = wbname || 'Workbook.xls';
                link.target = '_blank';
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
        })();
        var print = document.getElementsByClassName("print")[0];
        print.onclick = function () {
            if (startBtn.innerText == "已开始") {
                tablesToExcel(['table'], ['ProductDay1'], 'LiveTimeTable.xls', 'Excel')
            }
            else {
                alert("请点击'开始打点'");
            }
        }

    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值