导出excel全局方法
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
/**
|
import { ExcelDownLoad } from '@/utils/request'
|
||||||
Created by hedekun
|
|
||||||
20180727
|
|
||||||
*/
|
|
||||||
import { type } from './tool' // type判断传入值的类型,比typeof更详细
|
import { type } from './tool' // type判断传入值的类型,比typeof更详细
|
||||||
export default {
|
export default {
|
||||||
install(Vue) {
|
install(Vue) {
|
||||||
Vue.prototype.exportExcel_NPOI = function(url, type, name, data) {
|
Vue.prototype.exportExcel_NPOI = function(param) {
|
||||||
console.log(url, type, name, data, 1111111111)
|
console.log(ExcelDownLoad, param, 1111111111)
|
||||||
// this.download(`${url}?param=${data}`)
|
download(`${ExcelDownLoad}?param=${param}`)
|
||||||
}
|
}
|
||||||
Vue.prototype.CreateData = function(type, name, data, pageSize, pageList) {
|
Vue.prototype.CreateData = function(type, name, data, pageSize, pageList) {
|
||||||
var paramStr = ''
|
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) {
|
function saveAs(blob, filename = '下载文件.xls') {
|
||||||
// getBlob(url, function(blob, filename) {
|
if (window.navigator.msSaveOrOpenBlob) {
|
||||||
// var filename1 = decodeURIComponent(filename)
|
navigator.msSaveBlob(blob, filename)
|
||||||
// saveAs(blob, filename1)
|
} else {
|
||||||
// })
|
var link = document.createElement('a')
|
||||||
// }
|
var body = document.querySelector('body')
|
||||||
//
|
link.href = window.URL.createObjectURL(blob)
|
||||||
// function decodeUtf8(bytes) { var encoded = ""; for (var i = 0; i < bytes.length; i++) { encoded += '%' + bytes[i].toString(16); } return decodeURIComponent(encoded); }
|
link.download = filename
|
||||||
//
|
// fix Firefox
|
||||||
// function getBlob(url, cb) {
|
link.style.display = 'none'
|
||||||
// var xhr = new XMLHttpRequest()
|
body.appendChild(link)
|
||||||
// xhr.open('GET', url, true)
|
link.click()
|
||||||
// xhr.responseType = 'blob'
|
body.removeChild(link)
|
||||||
// xhr.onload = function() {
|
window.URL.revokeObjectURL(link.href)
|
||||||
// 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)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user