是不是没有设置编码格式,对比一下这段代码:
<!DOCTYPE html>如果用jquery插件的话,代码如下:
<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>
<td class="td2">90</td>
<td class="td3">95</td>
</tr>
<tr>
<td class="td0">赵六</td>
<td class="td1">2</td>
<td class="td2">76.3</td>
<td class="td3">80.1</td>
</tr>
</table>
<button id="export" class="layui-btn icon-btn"BfwOnclick="tableToExcel('score','text.xls')">导出excel</button>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery.table2excel.min.js"></script>
<style>
#score tr:first-child td {
font-weight: 600;
}
p {
margin: 0;
margin-top: 10px;
}
#export {
margin-top: 10px;
}
</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>
<td class="td2">90</td>
<td class="td3">95</td>
</tr>
<tr>
<td class="td0">赵六</td>
<td class="td1">2</td>
<td class="td2">76.3</td>
<td class="td3">80.1</td>
</tr>
</table>
<div class="checkbox"></div>
<button id="export" class="layui-btn icon-btn">导出excel</button>
<script>
$("#export").click(function(){
$("#score").table2excel({
exclude: ".noExl",//忽略元素
name: "Excel Document Name",//表格名称
filename: "myFileName",//表格文件名
exclude_img: true,//是否忽略图片
exclude_links: true,//是否忽略链接
exclude_inputs: true//是否忽略input、输入
});
})
</script>
</body>
</html>
网友回复