Elizabeth's proactive approach involves introducing urinal toilet attachment , an ingenious concept that optimizes space and functionality.

index.vue 4.1KB

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