纯js 导出excle文件
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head> <body> <button onclick="aaa()">导出excle</button> <script> function aaa() { let newlist = [ {'bh':11,'lj':11}, {'bh':22,'lj':22}, {'bh':33,'lj':33} ] let filed = { title: [ "编号", "路径" ], filed: [ "bh", "lj" ], }; //导出对应的字段 exportExcel(newlist, getNowTime(), filed); } function exportExcel(JSONData, FileName, filed) { if (!JSONData) return; //转化json为object var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData; var excel = "<table>"; var row = "<tr>"; for (var i in filed["title"]) { //标题 row += "<th align='center'>" + filed["title"][i] + "</th>"; } excel += row + "</tr>"; if (filed["filed"]) { //获取表头的字段 for (var i in arrData) { var row = "<tr>"; for (var j in filed["filed"]) { row += "<td>" + arrData[i][filed["filed"][j]] + "</td>"; } excel += row + "</tr>"; } } excel += "</table>"; 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 += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">'; excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel'; excelFile += '; charset=UTF-8">'; excelFile += "<head>"; excelFile += "<!--[if gte mso 9]>"; excelFile += "<xml>"; excelFile += "<x:ExcelWorkbook>"; excelFile += "<x:ExcelWorksheets>"; excelFile += "<x:ExcelWorksheet>"; excelFile += "<x:Name>"; excelFile += "{worksheet}"; excelFile += "</x:Name>"; excelFile += "<x:WorksheetOptions>"; excelFile += "<x:DisplayGridlines/>"; excelFile += "</x:WorksheetOptions>"; excelFile += "</x:ExcelWorksheet>"; excelFile += "</x:ExcelWorksheets>"; excelFile += "</x:ExcelWorkbook>"; excelFile += "</xml>"; excelFile += "<![endif]-->"; excelFile += "</head>"; excelFile += "<body>"; excelFile += excel; excelFile += "</body>"; excelFile += "</html>"; var uri = "data:application/vnd.ms-excel;charset=utf-8," + encodeURIComponent(excelFile); var link = document.createElement("a"); link.href = uri; link.style = "visibility:hidden"; link.download = FileName + ".xls"; document.body.appendChild(link); link.click(); // document.body.removeChild(link); } //获取当前时间 function getNowTime() { var date = new Date(); //年 getFullYear():四位数字返回年份 var year = date.getFullYear(); //getFullYear()代替getYear() //月 getMonth():0 ~ 11 var month = date.getMonth() + 1; //日 getDate():(1 ~ 31) var day = date.getDate(); //时 getHours():(0 ~ 23) var hour = date.getHours(); //分 getMinutes(): (0 ~ 59) var minute = date.getMinutes(); //秒 getSeconds():(0 ~ 59) var second = date.getSeconds(); var time = year + '-' + addZero(month) + '-' + addZero(day) + ' ' + addZero(hour) + ':' + addZero(minute) + ':' + addZero(second); return time; } //小于10的拼接上0字符串 function addZero(s) { return s < 10 ? ('0' + s) : s; } </script></body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现