标签:dispatch navigator chm mss creat link eve string document
node端
第一步 先创建一个路由
‘use strict‘
/**
* @param {Egg.Application} app - egg application
*/
module.exports = ({
router,
controller: {
searchStastics: {
searchRanking,
},
},
}) => {
// 搜索排行导出
router.get(‘/search/search_ranking_export‘, searchRanking.searchRankingExport)
}
第二步:
controller
/**
* 搜索排行列表导出
*/
async searchRankingExport() {
await this.searchCommomExport(‘/search/ranking‘)
}
/**
* 搜索排行/搜索记录导出
* @param { String} url 传入的接口
*/
async searchCommomExport(url) {
// 后台需要秒,同一个接口csv=1表示导出,不传或者为0表示正常的列表
const { service } = this
const query = await this.searchCommon()
query.csv = 1
const data = await service.common.tarsServer.tarsServerHttp(url, {
dataType: ‘buffer‘,
data: query,
})
this.ctx.set(‘Content-disposition‘, ‘attachment; filename="write.xlsx"‘)
this.ctx.set(‘Content-type‘, ‘application/octet-stream‘)
this.ctx.body = data
}
/**
* 搜索排行/搜索记录列表和导出的公共参数
*/
async searchCommon() {
const { ctx } = this
const { startTime, endTime, ...params } = ctx.request.query
return {
startTime: Math.round(startTime / 1000),
endTime: Math.round(endTime / 1000),
...params,
}
}
第三步 前端自己调用
const exportExcelUrl = ‘/user_exchange_record/export‘
export function exportFile(url, params) {
return request({
responseType: ‘blob‘,
headers: {
‘Content-Type‘: ‘application/json‘,
},
timeout: 1000 * 60,
url: url,
method: ‘get‘,
params,
})
}
async exportFileFun() {
const file = await exportFile(this.exportItem.apiUrl, res)
this.saveFile(file, ‘文件下载‘)
},
saveFile(file, filename) {
const ieKit = /(?:ms|\()(ie)\s([\w\.]+)|trident|(edge|edgios|edga|edg)/i.test(window.navigator.userAgent)
const blobData = new Blob([file], { type: ‘application/vnd.ms-excel‘ })
if (ieKit) {
navigator.msSaveBlob && navigator.msSaveBlob(blobData, filename)
} else {
const objectURL = window.URL.createObjectURL(blobData)
const save_link = document.createElement(‘a‘)
const event = document.createEvent(‘MouseEvents‘)
save_link.href = objectURL
save_link.download = filename + ‘.xlsx‘
event.initMouseEvent(‘click‘, true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
save_link.dispatchEvent(event)
window.URL.revokeObjectURL(objectURL)
}
},
node 端怎么通过exceljs实现excel的导出 请查看https://www.cnblogs.com/antyhouse/p/13256093.html
标签:dispatch navigator chm mss creat link eve string document
原文地址:https://www.cnblogs.com/antyhouse/p/13256265.html