<%
dim counti
counti=request.Form("Counti")
//数据流: 1,小w,女,跳舞|2,lq,男,篮球
response.write "<input type='button' id='OutExcel' name='OutExcel' value='导入成功数据导出' >"
%>
<input type="hidden" id="counti" name="counti" value="<%=counti%>">
<script LANGUAGE="javaScript">
var OutExcel= document.getElementById("OutExcel");
var counti = document.getElementById("counti").value;
OutExcel.onclick = function(event) {
//列标题
let str = '<tr><td>ID</td><td>名称</td><td>性别</td><td>爱好</td></tr>';
var arr = counti.split("|");
//循环遍历,每行加入tr标签,每个单元格加td标签
for(let i = 0 ; i < arr.length ; i++ ){
var row = arr[i].split(",");
str+='<tr>';
for(let j = 0 ; j < row.length ; j++ ){
//增加\t为了不让表格显示科学计数法或者其他格式
str+=`<td>${ row[j] + '\t'}</td>`;
}
str+='</tr>';
}
//Worksheet名
let worksheet = 'Sheet1'
let uri = 'data:application/vnd.ms-excel;base64,';
//下载的表格模板数据
let 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]-->
</head><body><table>${str}</table></body></html>`;
//下载模板
window.location.href = uri + base64(template)
}
//输出base64编码
function base64 (s) { return window.btoa(unescape(encodeURIComponent(s))) }
</script>
js表格导出excel
最新推荐文章于 2024-10-31 11:30:04 发布