Rap 原分销系统代码Web
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

139 行
3.7KB

  1. <template>
  2. <div>
  3. <input ref="excel-upload-input" class="excel-upload-input" type="file" accept=".xlsx, .xls" @change="handleClick">
  4. <div class="drop" @drop="handleDrop" @dragover="handleDragover" @dragenter="handleDragover">
  5. 拖拽excel文件到此处 或者
  6. <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">
  7. 浏览
  8. </el-button>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import XLSX from 'xlsx'
  14. export default {
  15. props: {
  16. beforeUpload: Function, // eslint-disable-line
  17. onSuccess: Function// eslint-disable-line
  18. },
  19. data() {
  20. return {
  21. loading: false,
  22. excelData: {
  23. header: null,
  24. results: null
  25. }
  26. }
  27. },
  28. methods: {
  29. generateData({ header, results }) {
  30. this.excelData.header = header
  31. this.excelData.results = results
  32. this.onSuccess && this.onSuccess(this.excelData)
  33. },
  34. handleDrop(e) {
  35. e.stopPropagation()
  36. e.preventDefault()
  37. if (this.loading) return
  38. const files = e.dataTransfer.files
  39. if (files.length !== 1) {
  40. this.$message.error('只支持单个文件上传!')
  41. return
  42. }
  43. const rawFile = files[0]
  44. if (!this.isExcel(rawFile)) {
  45. this.$message.error('只支持.xlsx, .xls, .csv 格式文件')
  46. return false
  47. }
  48. this.upload(rawFile)
  49. e.stopPropagation()
  50. e.preventDefault()
  51. },
  52. handleDragover(e) {
  53. e.stopPropagation()
  54. e.preventDefault()
  55. e.dataTransfer.dropEffect = 'copy'
  56. },
  57. handleUpload() {
  58. this.$refs['excel-upload-input'].click()
  59. },
  60. handleClick(e) {
  61. const files = e.target.files
  62. const rawFile = files[0] // only use files[0]
  63. if (!rawFile) return
  64. this.upload(rawFile)
  65. },
  66. upload(rawFile) {
  67. this.$refs['excel-upload-input'].value = null // fix can't select the same excel
  68. if (!this.beforeUpload) {
  69. this.readerData(rawFile)
  70. return
  71. }
  72. const before = this.beforeUpload(rawFile)
  73. if (before) {
  74. this.readerData(rawFile)
  75. }
  76. },
  77. readerData(rawFile) {
  78. this.loading = true
  79. return new Promise((resolve, reject) => {
  80. const reader = new FileReader()
  81. reader.onload = e => {
  82. const data = e.target.result
  83. const workbook = XLSX.read(data, { type: 'array' })
  84. const firstSheetName = workbook.SheetNames[0]
  85. const worksheet = workbook.Sheets[firstSheetName]
  86. const header = this.getHeaderRow(worksheet)
  87. const results = XLSX.utils.sheet_to_json(worksheet)
  88. this.generateData({ header, results })
  89. this.loading = false
  90. resolve()
  91. }
  92. reader.readAsArrayBuffer(rawFile)
  93. })
  94. },
  95. getHeaderRow(sheet) {
  96. const headers = []
  97. const range = XLSX.utils.decode_range(sheet['!ref'])
  98. let C
  99. const R = range.s.r
  100. /* start in the first row */
  101. for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
  102. const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
  103. /* find the cell in the first row */
  104. let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
  105. if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
  106. headers.push(hdr)
  107. }
  108. return headers
  109. },
  110. isExcel(file) {
  111. return /\.(xlsx|xls|csv)$/.test(file.name)
  112. }
  113. }
  114. }
  115. </script>
  116. <style scoped>
  117. .excel-upload-input{
  118. display: none;
  119. z-index: -9999;
  120. }
  121. .drop{
  122. border: 2px dashed #bbb;
  123. width: 600px;
  124. height: 160px;
  125. line-height: 160px;
  126. margin: 0 auto;
  127. font-size: 24px;
  128. border-radius: 5px;
  129. text-align: center;
  130. color: #bbb;
  131. position: relative;
  132. }
  133. </style>