exportExcel.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import notification from 'ant-design-vue/es/notification'
  2. import moment from 'moment'
  3. // 导出excel
  4. /*
  5. *url: 数据请求接口
  6. *params: 请求参数
  7. *fileName: 导出文件名称
  8. *callback: 回调函数
  9. */
  10. export const hdExportExcel = function (url, params, fileName, callback) {
  11. url(params).then(res => {
  12. if (res.type == 'application/json') {
  13. var reader = new FileReader()
  14. reader.addEventListener('loadend', function () {
  15. const obj = JSON.parse(reader.result)
  16. if (obj.status != 200) {
  17. notification.error({
  18. message: '提示',
  19. description: obj.message
  20. })
  21. }
  22. })
  23. reader.readAsText(res)
  24. } else {
  25. downloadExcel(res, fileName)
  26. }
  27. callback()
  28. })
  29. }
  30. // 导出下载excel
  31. export const downloadExcel = function (data, fileName) {
  32. if (!data) { return }
  33. const a = moment().format('YYYYMMDDHHmmss')
  34. const fname = fileName + a
  35. const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
  36. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  37. navigator.msSaveBlob(blob, fname + '.xlsx')
  38. } else {
  39. const link = document.createElement('a')
  40. link.style.display = 'none'
  41. var href = URL.createObjectURL(blob)
  42. link.href = href
  43. link.setAttribute('download', fname + '.xlsx')
  44. document.body.appendChild(link)
  45. link.click()
  46. document.body.removeChild(link)
  47. window.URL.revokeObjectURL(href) // 释放掉blob对象
  48. }
  49. }