JSON格式化 動態生成表格 表格轉置 行列轉換 Excel導出

先看效果

初始化:

 

JSON格式化 :

生成表格-方式1 :

生成表格-方式2 :

Excel導出

需要行求和、列求和功能的查看 JSON格式化 動態生成表格 表格轉置 行列轉換 Excel導出 行求和 列求和 

代碼如下: 

<!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>
    <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()" />
        <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 = [];
        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 (i, n) {
                if (i == 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>" + n[cols[i]] + "</td>";
                    }
                    tbody = tbody + "</tr>";
                }
            });
            $("#tbinfo tbody").html(tbody);
        }


        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 (i, n) {
                if (i == 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>" + n[cols[i]] + "</td>";
                    }
                    tbody = tbody + "</tr>";
                }
            });
            $("#tbinfo tbody").html(tbody);

        }

        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();
        }
    </script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章