This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!

Initialization:

JSON format:

Generate tables – Method 1:

Generate tables – Method 2:

Excel export:

Sum:

Column sum:

The complete code is as follows:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <style type="text/css">
        body
        {
            text-align: center;
        }
        
        div
        {
            margin: 0 auto;
            width: 600px;
        }
        
        table
        {
            border-collapse: collapse;
            margin: 0 auto;
            text-align: center;
        }
        
        table td, table th
        {
            border: 1px solid #cad9ea;
            color: #666;
            height: 30px;
        }
        
        table thead th
        {
            background-color: #CCE8EB;
            width: 100px;
        }
        
        table tr:nth-child(odd)
        {
            background: #fff;
        }
        
        table tr:nth-child(even)
        {
            background: #F5FAFA;
        }
    </style>
</head>
<body>
    <div>
        <input type="button" value="初始化" onclick="Init()" />
        <input type="button" value="JSON格式化" onclick="Jsonformat()" />
        <input type="button" value="生成表-方式1" onclick="Create1()" />
        <input type="button" value="生成表-方式2" onclick="Create2()" />
        <input type="button" value="导出Excel" onclick="Export()" />
        <input type="button" value="行求和" onclick="RowSum()" />
        <input type="button" value="列求和" onclick="ColSum()" />
        <br />
        <textarea id="jsonStr" style="width: 600px; height: 500px;">[{"指标":"指标","组织1":"组织1","组织2":"组织2","组织3":"组织3"},{"指标":"指标1","组织1":"1","组织2":"21","组织3":"31"},{"指标":"指标2","组织1":"2","组织2":"22","组织3":"32"},{"指标":"指标3","组织1":"3","组织2":"23","组织3":"33"},{"指标":"指标4","组织1":"4","组织2":"24","组织3":"34"}]</textarea>
    </div>
    <br />
    <table id="tbinfo">
        <thead>
        </thead>
        <tbody>
        </tbody>
    </table>
    <script type="text/javascript">
        var tbinfo = [];
        var tbinfo2 = [];
        var total_rows = 0;
        var total_cols = 0;
        function Init() {
            $("#jsonStr").val('[{"指标":"指标","组织1":"组织1","组织2":"组织2","组织3":"组织3"},{"指标":"指标1","组织1":"1","组织2":"21","组织3":"31"},{"指标":"指标2","组织1":"2","组织2":"22","组织3":"32"},{"指标":"指标3","组织1":"3","组织2":"23","组织3":"33"},{"指标":"指标4","组织1":"4","组织2":"24","组织3":"34"}]');
            $("#tbinfo thead").html("");
            $("#tbinfo tbody").html("");
        }
 
        function Jsonformat() {
            try {
                str = $("#jsonStr").val();
                tbinfo = JSON.parse(str);
                $("#jsonStr").val(JSON.stringify(tbinfo, null, '\t'));
 
            } catch (e) {
                alert("json格式不正确");
                return false;
            }
        }
 
        function Create1() {
 
            try {
                str = $("#jsonStr").val();
                tbinfo = JSON.parse(str);
            } catch (e) {
                alert("json格式不正确");
                return false;
            }
 
            var thead = "";
            var cols = [];
            var tbody = "";
            $.each(tbinfo, function (row, n) {
                if (row == 0) {
                    cols = Object.keys(n);
                    thead = thead + "<tr>";
                    for (var i = 0; i < cols.length; i++) {
                        thead = thead + "<th>" + n[cols[i]] + "</th>";
                    }
                    thead = thead + "</tr>";
                    $("#tbinfo thead").html(thead);
                } else {
                    tbody = tbody + "<tr>";
                    for (var i = 0; i < cols.length; i++) {
                        tbody = tbody + "<td  lw-row='" + row + "'  lw-col='" + i + "' >" + n[cols[i]] + "</td>";
                    }
                    tbody = tbody + "</tr>";
                }
            });
            $("#tbinfo tbody").html(tbody);
 
            total_rows = $("#tbinfo tr").length;
            total_cols = $("#tbinfo tr").find("th").length;
        }
 
 
        function Create2() {
 
            try {
                str = $("#jsonStr").val();
                tbinfo = JSON.parse(str);
            } catch (e) {
                alert("json格式不正确");
                return false;
            }
 
            tbinfo2 = [];
            var cols = Object.keys(tbinfo[0]);
            for (var i = 0; i < cols.length; i++) {
                var tr = {};
                $.each(tbinfo, function (index, n) {
                    tr[index] = n[cols[i]];
                });
                tbinfo2.push(tr);
            }
 
            var thead = "";
            var cols = [];
            var tbody = "";
            $.each(tbinfo2, function (row, n) {
                if (row == 0) {
                    cols = Object.keys(n);
                    thead = thead + "<tr>";
                    for (var i = 0; i < cols.length; i++) {
                        thead = thead + "<th>" + n[cols[i]] + "</th>";
                    }
                    thead = thead + "</tr>";
                    $("#tbinfo thead").html(thead);
                } else {
                    tbody = tbody + "<tr>";
                    for (var i = 0; i < cols.length; i++) {
                        tbody = tbody + "<td  lw-row='" + row + "'  lw-col='" + i + "' >" + n[cols[i]] + "</td>";
                    }
                    tbody = tbody + "</tr>";
                }
            });
            $("#tbinfo tbody").html(tbody);
 
            total_rows = $("#tbinfo tr").length;
            total_cols = $("#tbinfo tr").find("th").length;
 
        }
 
        function Export() {
            if ($("#tbinfo tbody tr").size() == 0) {
                alert("无数据");
            } else {
                tableToExcel("tbinfo", "测试");
            }
        }
 
        function base64(content) {
            return window.btoa(unescape(encodeURIComponent(content)));
        }
 
        function tableToExcel(tableID, fileName) {
            var excelContent = $("#" + tableID).html();
            var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
            excelFile += "<head><meta charset='utf-8'><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>sheet1</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>";
            excelFile += "<body><table width='10%'  border='1'>";
            excelFile += excelContent;
            excelFile += "</table></body>";
            excelFile += "</html>";
            var link = "data:application/vnd.ms-excel;base64," + base64(excelFile);
            var a = document.createElement("a");
            a.download = fileName + ".xls";
            a.href = link;
            a.click();
        }
 
        function RowSum() {
            if ($("#tbinfo tbody tr").size() == 0) {
                alert("无数据");
                return false;
            }
            var cols = $("#tbinfo tr").find("th").length;
            var rows = $("#tbinfo tr").length;
            var total_row = "";
 
            if (total_cols < cols) {
                for (var i = 0; i < rows; i++) {
                    if (i == 0) {
                        $("#tbinfo tr").eq(i).find("th:last").remove();
                    } else {
                        $("#tbinfo tr").eq(i).find("td:last").remove();
                    }
                }
                cols = cols - 1;
            }
 
            for (var i = 0; i < rows; i++) {
                if (i == 0) {
                    total_row = "<th  lw-row='" + i + "' lw-col='" + cols + "'>合计</th>";
                } else {
                    total_row = "<td  lw-row='" + i + "' lw-col='" + cols + "'>" + getSum($("#tbinfo td[lw-row='" + i + "']:gt(0)")) + "</td>";
                }
                $("#tbinfo tr").eq(i).append(total_row);
            }
 
 
 
 
        }
 
        function ColSum() {
            if ($("#tbinfo tbody tr").size() == 0) {
                alert("无数据");
                return false;
            }
 
            var cols = $("#tbinfo tr").find("th").length;
            var rows = $("#tbinfo tr").length;
            var total_tr = "";
 
            if (total_rows < rows) {
                $("#tbinfo tr:last").remove();
                rows = rows - 1;
            }
 
            total_tr = total_tr + "<tr>";
            for (var i = 0; i < cols; i++) {
 
                if (i == 0) {
                    total_tr = total_tr + "<td lw-row='" + rows + "' lw-col='" + i + "'>合计</td>";
                } else {
                    total_tr = total_tr + "<td lw-row='" + rows + "' lw-col='" + i + "' >" + getSum($("#tbinfo td[lw-col='" + i + "']")) + "</td>";
                }
            }
            total_tr = total_tr + "</tr>";
 
            $("#tbinfo tbody").append(total_tr);
        }
 
        function getSum(items) {
            var sum = 0;
            $.each(items, function (i, n) {
                console.log(Number($(n).html()));
                sum = sum + Number($(n).html());
            });
            return sum;
        }
 
    </script>
</body>
</html>
Copy the code