41 lines
1.5 KiB
JavaScript
41 lines
1.5 KiB
JavaScript
// 导出Excel方法
|
|
let idTmr
|
|
export function getExplorer() {
|
|
const explorer = window.navigator.userAgent
|
|
if (explorer.indexOf('Firefox') >= 0) { // firefox
|
|
return 'Firefox'
|
|
} else if (explorer.indexOf('Chrome') >= 0) { // Chrome
|
|
return 'Chrome'
|
|
} else if (explorer.indexOf('Opera') >= 0) { // Opera
|
|
return 'Opera'
|
|
} else if (explorer.indexOf('Safari') >= 0) { // Safari
|
|
return 'Safari'
|
|
}
|
|
}
|
|
const tableToExcel = (function() {
|
|
const uri = 'data:application/vnd.ms-excel;base64,'
|
|
const template = `<html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><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><meta charset="UTF-8"><style type="text/css">table td {border: 2px solid #000000;width:100px;text-align: center;color: #000000;}</style></head><body><table>{table}</table></body></html>`
|
|
const base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
|
|
const format = function(s, c) {
|
|
return s.replace(/{(\w+)}/g,
|
|
function(m, p) {
|
|
return c[p]
|
|
}
|
|
)
|
|
}
|
|
return function(table) {
|
|
if (!table.nodeType) table = document.getElementById(table)
|
|
const ctx = { worksheet: 'Worksheet', table: table.innerHTML }
|
|
window.location.href = uri + base64(format(template, ctx))
|
|
console.log(ctx)
|
|
}
|
|
})()
|
|
|
|
export function method5(tableid) {
|
|
tableToExcel(tableid)
|
|
}
|
|
export function Cleanup() {
|
|
window.clearInterval(idTmr)
|
|
}
|
|
|