ddd5cde690a68e78799e3dde3ced04171c0a10b5.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Drop excel file here or
  6. <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">
  7. Browse
  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('Only support uploading one file!')
  41. return
  42. }
  43. const rawFile = files[0] // only use files[0]
  44. if (!this.isExcel(rawFile)) {
  45. this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
  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>