第一种,我认为最好用的方法
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body link="blue" vlink="purple">
<table id="print-content" border="1" cellpadding="0" cellspacing="0" style='border-collapse:collapse;table-layout:fixed;'>
<colgroup>
<col width="80 ">
<col width="80 ">
<col width="150 ">
<col width="80 ">
<col width="150 ">
<col width="80 ">
<col width="80 ">
</colgroup>
<tr height="20" style="text-align: center;font-size:18px">
<td colspan="7">会签单</td>
</tr>
<tr height="20" style="text-align: center;font-size:14px">
<td></td>
<td colspan="2">流转</td>
<td></td>
<td colspan="3">会议</td>
</tr>
<tr height="20" style="text-align: center;font-size:14px">
<td>名称</td>
<td colspan="2">自动获取</td>
<td>编号</td>
<td colspan="3">自动获取</td>
</tr>
<tr height="20" style="text-align: center;font-size:14px">
<td>主持</td>
<td colspan="2">自动获取</td>
<td>类型</td>
<td colspan="3">自动获取</td>
</tr>
<tr height="20" style="text-align: center;font-size:14px">
<td>人</td>
<td colspan="2">自动获取</td>
<td>日期</td>
<td colspan="3">自动获取</td>
</tr>
<tr height="100" style="text-align: center;font-size:14px">
<td rowspan="5">内容</td>
<td colspan="6" rowspan="5">自动获取</td>
</tr>
<tr/>
<tr/>
<tr/>
<tr/>
<tr height="20" style="text-align: center;font-size:14px">
<td>备注</td>
<td colspan="6">自动获取</td>
</tr>
<tr height="24" style="text-align: center;font-size:16px">
<td colspan="7">意见</td>
</tr>
<tr height="24" style="text-align: center;font-size:14px">
<td>序号</td>
<td>人</td>
<td>部门</td>
<td>意见</td>
<td>时间</td>
<td>节点</td>
<td>状态</td>
</tr>
<tr height="20" style="text-align: center;font-size:14px">
<td>1</td>
<td>顾</td>
<td>办公室</td>
<td>同意!</td>
<td>2018/5/14 15:21</td>
<td></td>
<td>已提交</td>
</tr>
</table>
<!-- 下载方法1.超链接_导出文件-->
<a id="print-click">超链接_导出表格</a>
<!-- 下载方法2.引入文件保存js-->
<input type="button" onclick="download()" value="函数_导出表格"></input>
<input type="button" onclick="printout()" value="打印"></input>
<!-- 下载方法2.引入文件保存js-->
<script type="application/javascript" src="js/FileSaver.js" ></script>
<script>
// 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码
var html = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementById("print-content").outerHTML + "</body></html>";
// 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
var blob = new Blob([html], {
type: "application/vnd.ms-excel"
});
var a = document.getElementById("print-click");
// 利用URL.createObjectURL()方法为a元素生成blob URL
a.href = URL.createObjectURL(blob);
// 设置文件名,目前只有Chrome和FireFox支持此属性
a.download = "会签单.xls";
function download() {
debugger
// 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码
var html2 = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementById("print-content").outerHTML + "</body></html>";
// 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
var blob2 = new Blob([html2], {
type: "application/vnd.ms-excel"
});
saveAs(blob2, "应系单.xls");
};
//打印具体实现代码
function printout() {
debugger;
var newWindow;
//打开一个新的窗口
newWindow = window.open();
// 是新窗口获得焦点
newWindow.focus();
//保存写入内容
var newContent = "<html><head><meta charset='utf-8'/><title>打印</title></head><body>"
newContent += document.getElementById("print-content").outerHTML;
newContent += "</body></html>"
// 将HTML代码写入新窗口中
newWindow.document.write(newContent);
newWindow.print();
// close layout stream
newWindow.document.close();
//关闭打开的临时窗口
newWindow.close();
return false;
};
</script>
</body>
</html>
第二种
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>html 表格导出</title>
<script language="JavaScript" type="text/javascript">
//第五种方法
var idTmr;
function getExplorer() {
var explorer = window.navigator.userAgent ;
//ie
if (explorer.indexOf("MSIE") >= 0) {
return 'ie';
}
//firefox
else if (explorer.indexOf("Firefox") >= 0) {
return 'Firefox';
}
//Chrome
else if(explorer.indexOf("Chrome") >= 0){
return 'Chrome';
}
//Opera
else if(explorer.indexOf("Opera") >= 0){
return 'Opera';
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
return 'Safari';
}
}
function method5(tableid) {
if(getExplorer()=='ie') {
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var xlsheet = oWB.Worksheets(1);
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
sel.select();
sel.execCommand("Copy");
xlsheet.Paste();
oXL.Visible = true;
try {
var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
} catch (e) {
print("Nested catch caught " + e);
} finally {
oWB.SaveAs(fname);
oWB.Close(savechanges = false);
oXL.Quit();
oXL = null;
idTmr = window.setInterval("Cleanup();", 1);
}
} else {
tableToExcel(tableid)
}
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
format = function(s, c) {
return s.replace(/{(\w+)}/g,
function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
</head>
<body>
<div >
<button type="button" onclick="method1('tableExcel')">导出Excel方法一</button>
<button type="button" onclick="method2('tableExcel')">导出Excel方法二</button>
<button type="button" onclick="getXlsFromTbl('tableExcel','myDiv')">导出Excel方法三</button>
<button type="button" onclick="method4('tableExcel')">导出Excel方法四</button>
<button type="button" onclick="method5('tableExcel')">导出Excel方法五</button>
</div>
<div id="myDiv">
<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5" align="center">html 表格导出道Excel</td>
</tr>
<tr>
<td>列标题1</td>
<td>列标题2</td>
<td>类标题3</td>
<td>列标题4</td>
<td>列标题5</td>
</tr>
<tr>
<td>aaa</td>
<td>bbb</td>
<td>ccc</td>
<td>ddd</td>
<td>eee</td>
</tr>
<tr>
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
<td>DDD</td>
<td>EEE</td>
</tr>
<tr>
<td>FFF</td>
<td>GGG</td>
<td>HHH</td>
<td>III</td>
<td>JJJ</td>
</tr>
</table>
</div>
</body>
</html>