excel.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import service from '@/utils/request'
  2. import { ElMessage } from 'element-plus'
  3. const handleFileError = (res, fileName) => {
  4. if (typeof (res.data) !== 'undefined') {
  5. if (res.data.type === 'application/json') {
  6. const reader = new FileReader()
  7. reader.onload = function() {
  8. const message = JSON.parse(reader.result).msg
  9. ElMessage({
  10. showClose: true,
  11. message: message,
  12. type: 'error'
  13. })
  14. }
  15. reader.readAsText(new Blob([res.data]))
  16. }
  17. } else {
  18. var downloadUrl = window.URL.createObjectURL(new Blob([res]))
  19. var a = document.createElement('a')
  20. a.style.display = 'none'
  21. a.href = downloadUrl
  22. a.download = fileName
  23. var event = new MouseEvent('click')
  24. a.dispatchEvent(event)
  25. }
  26. }
  27. // @Tags excel
  28. // @Summary 导出Excel
  29. // @Security ApiKeyAuth
  30. // @accept application/json
  31. // @Produce application/octet-stream
  32. // @Param data body model.ExcelInfo true "导出Excel文件信息"
  33. // @Success 200
  34. // @Router /excel/exportExcel [post]
  35. export const exportExcel = (tableData, fileName) => {
  36. service({
  37. url: '/excel/exportExcel',
  38. method: 'post',
  39. data: {
  40. fileName: fileName,
  41. infoList: tableData
  42. },
  43. responseType: 'blob'
  44. }).then((res) => {
  45. handleFileError(res, fileName)
  46. })
  47. }
  48. // @Tags excel
  49. // @Summary 导入Excel文件
  50. // @Security ApiKeyAuth
  51. // @accept multipart/form-data
  52. // @Produce application/json
  53. // @Param file formData file true "导入Excel文件"
  54. // @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
  55. // @Router /excel/importExcel [post]
  56. export const loadExcelData = () => {
  57. return service({
  58. url: '/excel/loadExcel',
  59. method: 'get'
  60. })
  61. }
  62. // @Tags excel
  63. // @Summary 下载模板
  64. // @Security ApiKeyAuth
  65. // @accept multipart/form-data
  66. // @Produce application/json
  67. // @Param fileName query fileName true "模板名称"
  68. // @Success 200
  69. // @Router /excel/downloadTemplate [get]
  70. export const downloadTemplate = (fileName) => {
  71. return service({
  72. url: '/excel/downloadTemplate',
  73. method: 'get',
  74. params: {
  75. fileName: fileName
  76. },
  77. responseType: 'blob'
  78. }).then((res) => {
  79. handleFileError(res, fileName)
  80. })
  81. }