请问js如何将html内的table导出为excel表格?
网友回复
jquery有个插件叫table2excel,使用示例代码如下:
<!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>
</t...点击查看剩余70%
原生js的写法也简单,代码如下:
<!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");...点击查看剩余70%


