index.vue 4.2KB

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