handleExport (data) {
if (this.benchList.length === 0 || this.benchList === null) {
this.$message.warning('不能导出空数据')
return
}
let titlestr = ''
let arr = []
for (let i = 0; i < this.benchList.length; i++) {
let obj = {}
obj.index = i + 1
obj.securityName = this.benchList[i].securityName
obj.securityCode = this.benchList[i].securityCode
arr.push(obj)
}
if (window.navigator.msSaveOrOpenBlob) {
// 加上 \ufeff BOM 头,适配ie
titlestr = '\ufeff'
}
this.$refs.scoreTable.columns.forEach((item) => {
if (item.label === '序号' || item.label === '证券代码' || item.label === '证券名称') {
titlestr += item.label + ','
}
})
titlestr = titlestr.slice(0, titlestr.length - 1) + '\n'
arr.forEach(item => {
Object.keys(item).forEach(nitem => {
titlestr += item[nitem] + ','
})
titlestr = titlestr.slice(0, titlestr.length - 1) + '\n'
})
if (window.navigator.msSaveOrOpenBlob) {
// IE 浏览器 \n.使用后会发现列可以分开,但是不换行,使用decodeURIComponent 进行编码
var blob = new window.Blob([decodeURIComponent(decodeURI(titlestr))], {
type: 'data:text/csv;charset=utf-8,'
})
// Internet Explorer 10 的 msSaveBlob 和 msSaveOrOpenBlob 方法允许用户在客户端上保存文件
navigator.msSaveBlob(blob, '成分股配股信息列表')
} else {
let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(titlestr)
let link = document.createElement('a')
link.href = uri
link.download = '成分股配股信息列表'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
},
前端代码excel导出,适配ie
最新推荐文章于 2022-01-10 15:42:21 发布