table 转 excel 无插件


function base64(content) {
		return window.btoa(unescape(encodeURIComponent(content)));
}

function tableToExcel(tableID, fileName) {
	var excelContent = $("#" + tableID).html();
	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><!--[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 width='10%'  border='1'>";
	excelFile += excelContent;
	excelFile += "</table></body>";
	excelFile += "</html>";
	var link = "data:application/vnd.ms-excel;base64," + base64(excelFile);
	var a = document.createElement("a");
	a.download = fileName + ".xls";
	a.href = link;
	a.click();
}

html代码

<table class="table report-table" style="margin-top: 20px;" border=1 id = "tableExcel">
	<thead>
	<tr>
		<td class="text-center" style="background-color: #f9fafe;">公证员</td>
		<td class="text-center" style="background-color: #f9fafe;">助手</td>
		<td class="text-center" style="background-color: #f9fafe;">年度</td>
		<td class="text-center" style="background-color: #f9fafe;">类别</td>
		
		<td class="text-center" style="background-color: #f9fafe;">证号</td>
		<td class="text-center" style="background-color: #f9fafe;">当事人</td>
		<td class="text-center" style="background-color: #f9fafe;">受理时间</td>
		<td class="text-center" style="background-color: #f9fafe;">审批时间</td>
		
	</tr>
	</thead>
	<tbody>
	<%
		for (Map<String, Object> data : dataList) {
	%>
	<tr>
		<td class="text-center"><%=data.get("notryName")%></td>
		<td class="text-center"><%=data.get("assistantName")%></td>
		<td class="text-center"><%=data.get("year")%></td>
		<td class="text-center"><%=data.get("typeName")%></td>
		
		<td class="text-center"><%=data.get("serialNumber")%></td>
		<td class="text-center"><%=data.get("partyName")%></td>
		<td class="text-center"><%=data.get("acceptDate")%></td>
		<td class="text-center"><%=data.get("approveDate")%></td>
	</tr>
	<%
		}
	%>
	</tbody>
</table>
<button class="btn btn-default" onclick="tableToExcel('tableExcel', '未归档明细表(新黄浦)')" title="导出excel">导出excel</button>
纯js实现方式
通过table的Class导出excel
var tableClass = document.getElementsByClassName('ant-table-fixed');
tableClass[0].id = 'tableExcel';
var tableItem = document.getElementById('tableExcel');
var excelContent = tableItem.innerHTML;
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><!--[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 width='10%'  border='1'>";
excelFile += excelContent;
excelFile += "</table></body>";
excelFile += "</html>";
var link = "data:application/vnd.ms-excel;base64," + window.btoa(unescape(encodeURIComponent(excelFile)));
var a = document.createElement("a");
a.download =  "邮费统计.xls";
a.href = link;
a.click();

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值