导出excel全局方法

This commit is contained in:
Vergil
2020-02-10 11:02:50 +08:00
parent 495aeb30cf
commit 69d7870b46

View File

@@ -1,13 +1,11 @@
/**
Created by hedekun
20180727
*/
import { ExcelDownLoad } from '@/utils/request'
import { type } from './tool' // type判断传入值的类型比typeof更详细
export default {
install(Vue) {
Vue.prototype.exportExcel_NPOI = function(url, type, name, data) {
console.log(url, type, name, data, 1111111111)
// this.download(`${url}?param=${data}`)
Vue.prototype.exportExcel_NPOI = function(param) {
console.log(ExcelDownLoad, param, 1111111111)
download(`${ExcelDownLoad}?param=${param}`)
}
Vue.prototype.CreateData = function(type, name, data, pageSize, pageList) {
var paramStr = ''
@@ -272,40 +270,39 @@ export default {
}
}
}
function download(url) {
getBlob(url, function(blob, filename) {
saveAs(blob, filename)
})
}
// function decodeUtf8(bytes) { var encoded = ''; for (var i = 0; i < bytes.length; i++) { encoded += '%' + bytes[i].toString(16) } return decodeURIComponent(encoded) }
function getBlob(url, cb) {
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
xhr.onload = function() {
if (xhr.status === 200) {
const name = xhr.getResponseHeader('content-disposition').split('=')[1]
var filename1 = decodeURIComponent(name)
cb(xhr.response, filename1)
}
}
xhr.send()
}
// function download(url) {
// getBlob(url, function(blob, filename) {
// var filename1 = decodeURIComponent(filename)
// saveAs(blob, filename1)
// })
// }
//
// function decodeUtf8(bytes) { var encoded = ""; for (var i = 0; i < bytes.length; i++) { encoded += '%' + bytes[i].toString(16); } return decodeURIComponent(encoded); }
//
// function getBlob(url, cb) {
// var xhr = new XMLHttpRequest()
// xhr.open('GET', url, true)
// xhr.responseType = 'blob'
// xhr.onload = function() {
// if (xhr.status === 200) {
// var name = decodeUtf8(xhr.getResponseHeader('content-disposition').split('=')[1])
// }
// }
// xhr.send()
// }
//
// function saveAs(blob, filename = '下载文件.xls') {
// if (window.navigator.msSaveOrOpenBlob) {
// navigator.msSaveBlob(blob, filename)
// } else {
// var link = document.createElement('a')
// var body = document.querySelector('body')
// link.href = window.URL.createObjectURL(blob)
// link.download = filename
// link.style.display = 'none'
// body.appendChild(link)
// link.click()
// body.removeChild(link)
// window.URL.revokeObjectURL(link.href)
// }
// }
function saveAs(blob, filename = '下载文件.xls') {
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, filename)
} else {
var link = document.createElement('a')
var body = document.querySelector('body')
link.href = window.URL.createObjectURL(blob)
link.download = filename
// fix Firefox
link.style.display = 'none'
body.appendChild(link)
link.click()
body.removeChild(link)
window.URL.revokeObjectURL(link.href)
}
}