天波用户运营管理后台系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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