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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button onclick="aaa()">导出excle</button>

    <script>
        function aaa() {
            let newlist = [
                {'bh':11,'lj':11},
                {'bh':22,'lj':22},
                {'bh':33,'lj':33}
            ]

            let filed = {
            title: [
                "编号",
                "路径"
            ],
            filed: [
                "bh",
                "lj"
            ],
        };
             //导出对应的字段
                    exportExcel(newlist, getNowTime(), filed);
        }
        


        function exportExcel(JSONData, FileName, filed) {
            if (!JSONData) return;
            //转化json为object
            var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;
            var excel = "<table>";
            var row = "<tr>";
            for (var i in filed["title"]) {  //标题
                row += "<th align='center'>" + filed["title"][i] + "</th>";
            }
            excel += row + "</tr>";
            if (filed["filed"]) {  //获取表头的字段
                for (var i in arrData) {
                    var row = "<tr>";
                    for (var j in filed["filed"]) {
                        row += "<td>" + arrData[i][filed["filed"][j]] + "</td>";
                    }
                    excel += row + "</tr>";
                }
            }
            excel += "</table>";
            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 +=
                '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
            excelFile +=
                '<meta http-equiv="content-type" content="application/vnd.ms-excel';
            excelFile += '; charset=UTF-8">';
            excelFile += "<head>";
            excelFile += "<!--[if gte mso 9]>";
            excelFile += "<xml>";
            excelFile += "<x:ExcelWorkbook>";
            excelFile += "<x:ExcelWorksheets>";
            excelFile += "<x:ExcelWorksheet>";
            excelFile += "<x:Name>";
            excelFile += "{worksheet}";
            excelFile += "</x:Name>";
            excelFile += "<x:WorksheetOptions>";
            excelFile += "<x:DisplayGridlines/>";
            excelFile += "</x:WorksheetOptions>";
            excelFile += "</x:ExcelWorksheet>";
            excelFile += "</x:ExcelWorksheets>";
            excelFile += "</x:ExcelWorkbook>";
            excelFile += "</xml>";
            excelFile += "<![endif]-->";
            excelFile += "</head>";
            excelFile += "<body>";
            excelFile += excel;
            excelFile += "</body>";
            excelFile += "</html>";
            var uri =
                "data:application/vnd.ms-excel;charset=utf-8," +
                encodeURIComponent(excelFile);
            var link = document.createElement("a");
            link.href = uri;
            link.style = "visibility:hidden";
            link.download = FileName + ".xls";
            document.body.appendChild(link);
            link.click();
            // document.body.removeChild(link);
        }

        //获取当前时间
        function getNowTime() {
            var date = new Date();
            //年 getFullYear():四位数字返回年份
            var year = date.getFullYear();  //getFullYear()代替getYear()
            //月 getMonth():0 ~ 11
            var month = date.getMonth() + 1;
            //日 getDate():(1 ~ 31)
            var day = date.getDate();
            //时 getHours():(0 ~ 23)
            var hour = date.getHours();
            //分 getMinutes(): (0 ~ 59)
            var minute = date.getMinutes();
            //秒 getSeconds():(0 ~ 59)
            var second = date.getSeconds();

            var time = year + '-' + addZero(month) + '-' + addZero(day) + ' ' + addZero(hour) + ':' + addZero(minute) + ':' + addZero(second);
            return time;
        }


        //小于10的拼接上0字符串
        function addZero(s) {
            return s < 10 ? ('0' + s) : s;
        }
    </script>
</body>

</html>