<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a download="统计报表" href="#" id="excelOut" class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-undo'">导出为 Excel 文档</a>
<table cellspacing="0" cellpadding="0" border="1" id="tableToExcel">
<thead>
<tr>
<td colspan="4" align="center">html 表格导出道Excel</td>
</tr>
</thead>
<tbody>
<tr>
<td style="background-color:rgb(217, 217, 217);">列标题1</td>
<td style='background-color:rgb(255, 242, 204);'>列标题2</td>
<td style='background-color:rgb(226, 239, 218);'>类标题3</td>
<td style='background-color:rgb(217, 225, 242);'>列标题4</td>
</tr>
<tr>
<td style="background-color:rgb(217, 217, 217);">aaa</td>
<td style='background-color:rgb(255, 242, 204);'>bbb</td>
<td style='background-color:rgb(226, 239, 218);'>ccc</td>
<td style='background-color:rgb(217, 225, 242);'>ddd</td>
</tr>
<tr>
<td style="background-color:rgb(217, 217, 217);">AAA</td>
<td style='background-color:rgb(255, 242, 204);'>BBB</td>
<td style='background-color:rgb(226, 239, 218);'>CCC</td>
<td style='background-color:rgb(217, 225, 242);'>DDD</td>
</tr>
<tr>
<td style="background-color:rgb(217, 217, 217);">FFF</td>
<td style='background-color:rgb(255, 242, 204);'>GGG</td>
<td style='background-color:rgb(226, 239, 218);'>HHH</td>
<td style='background-color:rgb(217, 225, 242);'>III</td>
</tr>
</tbody>
</table>
<script>
window.onload = function () {
tableToExcel('tableToExcel', '下载模板')
};
//base64转码
var base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)));
};
//替换table数据和worksheet名字
var format = function (s, c) {
return s.replace(/{(\w+)}/g,
function (m, p) {
return c[p];
});
}
function tableToExcel(tableid, sheetName) {
var uri = 'data:application/vnd.ms-excel;base64,';
var template = '<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"><head><!--[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]-->' +
' <style type="text/css">' +
'table td {' +
'border: 1px solid #000000;' +
'width: 200px;' +
'height: 30px;' +
' text-align: center;' +
'background-color: #FFFFFF;' +
'color: #000000;' +
' }' +
'</style>' +
'</head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><body ><table class="excelTable">{table}</table></body></html>';
if (!tableid.nodeType) tableid = document.getElementById(tableid);
var ctx = { worksheet: sheetName || 'Worksheet', table: tableid.innerHTML };
document.getElementById("excelOut").href = uri + base64(format(template, ctx));
}
</script>
</body>
</html>
这个能实现导出表格,但是导出的表格的背景色改不了,求问如何解决?
----------------------------------------------------2019.10.17
问题已解决:
首先感谢@i_laolarou 的帮助
只需要将 'background-color: #FFFFFF;' 改为 'background-color: #FFFFFF !important;' 即可以解决导出的表格自定义背景颜色不生效的问题。