JS导出带图片的Excel

时间:2025-02-15 07:47:25
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>导出图片和数据到Excel</title> </head> <body> </body> <script> /* eslint-disable */ let idTmr; const getExplorer = () => { let explorer = ; //ie if (("MSIE") >= 0) { return 'ie'; } //firefox else if (("Firefox") >= 0) { return 'Firefox'; } //Chrome else if (("Chrome") >= 0) { return 'Chrome'; } //Opera else if (("Opera") >= 0) { return 'Opera'; } //Safari else if (("Safari") >= 0) { return 'Safari'; } } // 判断浏览器是否为IE const exportToExcel = (data,name) => { // 判断是否为IE if (getExplorer() == 'ie') { tableToIE(data, name) } else { tableToNotIE(data,name) } } const Cleanup = () => { (idTmr); } // ie浏览器下执行 const tableToIE = (data, name) => { let curTbl = data; let oXL = new ActiveXObject(""); //创建AX对象excel let oWB = (); //获取workbook对象 let xlsheet = (1); //激活当前sheet let sel = (); (curTbl); //把表格中的内容移到TextRange中 ; //全选TextRange中内容 ("Copy"); //复制TextRange中内容 (); //粘贴到活动的EXCEL中 = true; //设置excel可见属性 try { let fname = ("", "Excel Spreadsheets (*.xls), *.xls"); } catch (e) { print("Nested catch caught " + e); } finally { (fname); (savechanges = false); // = false; (); oXL = null; // 结束excel进程,退出完成 ("Cleanup();", 1); idTmr = ("Cleanup();", 1); } } // 非ie浏览器下执行 const tableToNotIE = (function() { // 编码要用utf-8不然默认gbk会出现中文乱码 let uri = 'data:application/-excel;base64,', template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http:///TR/REC-html40"><head><meta charset="UTF-8"><!--[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>{table}</table></body></html>', base64 = function(s) { return (unescape(encodeURIComponent(s))); }, format = (s, c) => { return (/{(\w+)}/g, (m, p) => { return c[p]; }) } return (table, name) => { let ctx = { worksheet: name, table } //创建下载 let link = ('a'); ('href', uri + base64(format(template, ctx))); ('download', name); (); } })() // 导出函数 const export2Excel = (theadData, tbodyData, dataname) => { let re = /http/ // 字符串中包含http,则默认为图片地址 let th_len = // 表头的长度 let tb_len = // 记录条数 let width = 180 // 设置图片大小 let height = 200 // 添加表头信息 let thead = '<thead><tr>' for (let i = 0; i < th_len; i++) { thead += '<th>' + theadData[i] + '</th>' } thead += '</tr></thead>' // 添加每一行数据 let tbody = '<tbody>' for (let i = 0; i < tb_len; i++) { tbody += '<tr>' let row = tbodyData[i] // 获取每一行数据 for (let key in row) { if ((row[key])) { // 如果为图片,则需要加div包住图片 // tbody += '<td style="width:' + width + 'px; height:' + height + 'px; text-align: center; vertical-align: middle"><div style="display:inline"><img src=\'' + row[key] + '\' ' + ' ' + 'width=' + '\"' + width + '\"' + ' ' + 'height=' + '\"' + height + '\"' + '></div></td>' } else { tbody += '<td style="text-align:center">' + row[key] + '</td>' } } tbody += '</tr>' } tbody += '</tbody>' let table = thead + tbody // 导出表格 exportToExcel(table, dataname) } </script> <script> // tHeader和tbody的数据需要一一对应 let tHeader = [ '球星', '国家', '照片' ] let tbody = [ { name: 'C罗', country: '葡萄牙', pic: '/6ONWsjip0QIZ8tyhnq/it/u=3375134598,1577355399&fm=58' }, { name: '梅西', country: '阿根廷', pic: '/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=100059772,95063167&fm=111&gp=' }, { name: '内马尔', country: '巴西', pic: '/6ONWsjip0QIZ8tyhnq/it/u=2186912775,1920732168&fm=58' }, { name: '伊布', country: '瑞典', pic: '/6ONYsjip0QIZ8tyhnq/it/u=3518710826,1372793792&fm=58' } ] export2Excel(tHeader, tbody, '测试') </script> </html>