index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>导出图片和数据到Excel</title>
</head>
<body></body>
<script src="js/export2Excel.js"></script>
<script>
let tHeader = ['鲜花', '颜色', '图片']
let tbody = [{
name: '玫瑰花',
color: '红色',
pic: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2801998497,4036145562&fm=27&gp=0.jpg'
},
{
name: '菊花',
color: '黄色',
pic: 'https://img2020.cnblogs.com/blog/1742906/202105/1742906-20210514231342257-1073217931.jpg'
},
{
name: '牵牛花',
color: '紫色',
pic: 'https://img2020.cnblogs.com/blog/1742906/202105/1742906-20210514231502756-707130600.jpg'
},
{
name: '梅花',
color: '白色',
pic: 'https://img2020.cnblogs.com/blog/1742906/202105/1742906-20210514231510233-31637764.jpg'
},
{
name: '桃花花',
color: '粉色',
pic: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=602076004,4209938077&fm=27&gp=0.jpg'
}
]
const export2Excel = (theadData, tbodyData, dataname) => {
let re = /http/
let th_len = theadData.length
let tb_len = tbodyData.length
let width = 60
let height = 60
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 (re.test(row[key])) {
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)
}
export2Excel(tHeader, tbody, '我爱花朵')
</script>
</html>
export2Excel.js
let idTmr
const getExplorer = () => {
let explorer = window.navigator.userAgent
if (explorer.indexOf('MSIE') >= 0) {
return 'ie'
}
else if (explorer.indexOf('Firefox') >= 0) {
return 'Firefox'
}
else if (explorer.indexOf('Chrome') >= 0) {
return 'Chrome'
}
else if (explorer.indexOf('Opera') >= 0) {
return 'Opera'
}
else if (explorer.indexOf('Safari') >= 0) {
return 'Safari'
}
}
const exportToExcel = (data, name) => {
if (getExplorer() == 'ie') {
tableToIE(data, name)
} else {
tableToNotIE(data, name)
}
}
const Cleanup = () => {
window.clearInterval(idTmr)
}
const tableToIE = (data, name) => {
let curTbl = data
let oXL = new ActiveXObject('Excel.Application')
let oWB = oXL.Workbooks.Add()
let xlsheet = oWB.Worksheets(1)
let sel = document.body.createTextRange()
sel.moveToElementText(curTbl)
sel.select
sel.execCommand('Copy')
xlsheet.Paste()
oXL.Visible = true
try {
let 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
window.setInterval('Cleanup();', 1)
idTmr = window.setInterval('Cleanup();', 1)
}
}
const tableToNotIE = (function() {
let uri = 'data:application/vnd.ms-excel;base64,',
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><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 window.btoa(unescape(encodeURIComponent(s)))
},
format = (s, c) => {
return s.replace(/{(\w+)}/g, (m, p) => {
return c[p]
})
}
return (table, name) => {
let ctx = {
worksheet: name,
table
}
let link = document.createElement('a')
link.setAttribute('href', uri + base64(format(template, ctx)))
link.setAttribute('download', name)
link.click()
}
})()
导出excel

查看excel
