+
95
-

js导出excel出现中文乱码?

js导出excel出现中文乱码?请问怎么解决?

网友回复

+
15
-

是不是没有设置编码格式,对比一下这段代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BFW NEW PAGE</title>

    <script type="text/javascript">

        function tableToExcel(tableID, fileName) {

            var table = document.getElementById(tableID);

            var excelContent = table.innerHTML;
            var head="head";

            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>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>";

            excelFile += "<body><table>";

            excelFile += excelContent;

            excelFile += "</table></body>";

            excelFile += "</html>";

            console.log(excelFile)

            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 base64 (content) {

            return window.btoa(unescape(encodeURIComponent(content)));

        }
    </script>
    <style>
    </style>
</head>
<body>
    <table id="score" cellspacing="0" cellpadding="0" border="1" solid="#000000">
        <tr>
            <td class="td0">姓名</td>
            <td class="td1">班级</td>
            <td class="td2">数学成绩</td>
            <td class="td3">历史成绩</td>
        </tr>
        <tr>
            <td class="td0">张水</td>
            <td class="td1">1</td>
            <td class="td2">65</td>
            <td class="td3">83</td>
        </tr>
        <tr>
            <td class="td0">李四</td>
            <td class="td1">1</td>
            <td class="td2">34</td>
            <td class="td3">89.5</td>
        </tr>
        <tr>
            <td class="td0">王五</td>
            <td class="td1">2</td>
...

点击查看剩余70%

我知道答案,我要回答