소스 검색

上传文件至 'src/xt_pages/workforce/components/scheduleUploadExcel'

yangq 2 년 전
부모
커밋
04c0ee5ce5
1개의 변경된 파일182개의 추가작업 그리고 180개의 파일을 삭제
  1. 182 180
      src/xt_pages/workforce/components/scheduleUploadExcel/index.vue

+ 182 - 180
src/xt_pages/workforce/components/scheduleUploadExcel/index.vue 파일 보기

@@ -1,180 +1,182 @@
1
-<template>
2
-  <div>
3
-    <el-dialog
4
-      :visible.sync="msgTipVisible"
5
-      width="40%"
6
-    >
7
-      <span>若执行排班导入,系统将会清除当前已有临时排班数据,是否继续导入?</span>
8
-      <span slot="footer" class="dialog-footer">
9
-    <el-button @click="msgTipVisible = false">取 消</el-button>
10
-    <el-button type="primary" @click="handleUpload()">确 定</el-button>
11
-  </span>
12
-    </el-dialog>
13
-     <input id="excel-upload-input" ref="excel-upload-input" type="file"  accept=".xlsx,.xls,.xltx," @change="handleClick" >
14
-    <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="msgTipVisible = true">导入排班
15
-    </el-button>
16
-  </div>
17
-</template>
18
-
19
-<script>
20
-  import XLSX from 'xlsx'
21
-
22
-  export default {
23
-    name: "scheduleUploadExcel",
24
-    props: {
25
-      beforeUpload: Function,
26
-      onSuccess: Function
27
-    },
28
-    data() {
29
-      return {
30
-        loading: false,
31
-        msgTipVisible:false,
32
-        excelData: {
33
-          header: null,
34
-          results: null
35
-        }
36
-      }
37
-    },
38
-    methods: {
39
-      generateDate({ header, results }) {
40
-        console.log("header",header)
41
-        console.log("results",results)
42
-        this.excelData.header = header
43
-        this.excelData.results = results
44
-
45
-        this.onSuccess && this.onSuccess(this.excelData)
46
-      },
47
-      handleDrop(e) {
48
-        e.stopPropagation()
49
-        e.preventDefault()
50
-        if (this.loading) return
51
-        const files = e.dataTransfer.files
52
-        if (files.length !== 1) {
53
-          this.$message.error('Only support uploading one file!')
54
-          return
55
-        }
56
-        const rawFile = files[0] // only use files[0]
57
-
58
-        if (!this.isExcel(rawFile)) {
59
-          this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
60
-          return false
61
-        }
62
-        this.upload(rawFile)
63
-        e.stopPropagation()
64
-        e.preventDefault()
65
-      },
66
-      handleDragover(e) {
67
-        e.stopPropagation()
68
-        e.preventDefault()
69
-        e.dataTransfer.dropEffect = 'copy'
70
-      },
71
-      handleUpload() {
72
-        this.msgTipVisible = false
73
-
74
-        document.getElementById('excel-upload-input').click()
75
-      },
76
-      handleClick(e) {
77
-
78
-        const files = e.target.files
79
-        const rawFile = files[0] // only use files[0]
80
-        console.log("rawfiel",rawFile)
81
-        if (!rawFile)
82
-        return
83
-        this.upload(rawFile)
84
-
85
-      },
86
-      upload(rawFile) {
87
-
88
-        this.$refs['excel-upload-input'].value = null // fix can't select the same excel
89
-        console.log("3333",this.beforeUpload)
90
-        if (!this.beforeUpload) {
91
-          this.readerData(rawFile)
92
-          return
93
-        }
94
-        const before = this.beforeUpload(rawFile)
95
-        console.log("before",before)
96
-        if (before) {
97
-          this.readerData(rawFile)
98
-        }
99
-      },
100
-      readerData(rawFile) {
101
-
102
-        this.loading = true
103
-        return new Promise((resolve, reject) => {
104
-          const reader = new FileReader()
105
-          reader.onload = e => {
106
-
107
-            const data = e.target.result
108
-            console.log("data22222",data)
109
-            const fixedData = this.fixdata(data)
110
-            const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
111
-            const firstSheetName = workbook.SheetNames[0]
112
-            const worksheet = workbook.Sheets[firstSheetName]
113
-
114
-            const header = this.get_header_row(worksheet)
115
-
116
-            const results = XLSX.utils.sheet_to_json(worksheet)
117
-            this.generateDate({ header, results })
118
-            this.loading = false
119
-            resolve()
120
-          }
121
-          reader.readAsArrayBuffer(rawFile)
122
-        })
123
-      },
124
-      fixdata(data) {
125
-        let o = ''
126
-        let l = 0
127
-        const w = 10240
128
-        for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
129
-        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
130
-        return o
131
-      },
132
-      get_header_row(sheet) {
133
-
134
-        if(sheet['!ref'] == undefined){
135
-          this.loading = false
136
-          return
137
-        }
138
-
139
-        const headers = []
140
-        const range = XLSX.utils.decode_range(sheet['!ref'])
141
-        let C
142
-        const R = range.s.r /* start in the first row */
143
-        for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
144
-          var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
145
-          var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
146
-          if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
147
-          headers.push(hdr)
148
-        }
149
-        return headers
150
-
151
-
152
-
153
-      },
154
-      isExcel(file) {
155
-
156
-        return /\.(xlsx|xls|csv)$/.test(file.name)
157
-      }
158
-    }
159
-  }
160
-</script>
161
-
162
-<style scoped>
163
-  #excel-upload-input {
164
-    display: none;
165
-    z-index: -9999;
166
-  }
167
-
168
-  #drop {
169
-    border: 2px dashed #bbb;
170
-    width: 600px;
171
-    height: 160px;
172
-    line-height: 160px;
173
-    margin: 0 auto;
174
-    font-size: 24px;
175
-    border-radius: 5px;
176
-    text-align: center;
177
-    color: #bbb;
178
-    position: relative;
179
-  }
180
-</style>
1
+<template>
2
+  <div>
3
+    <el-dialog
4
+      :visible.sync="msgTipVisible"
5
+      width="40%"
6
+      :modal-append-to-body='false'
7
+      :close-on-click-modal="false"
8
+    >
9
+      <span>若执行排班导入,系统将会清除当前已有临时排班数据,是否继续导入?</span>
10
+      <span slot="footer" class="dialog-footer">
11
+    <el-button @click="msgTipVisible = false">取 消</el-button>
12
+    <el-button type="primary" @click="handleUpload()">确 定</el-button>
13
+  </span>
14
+    </el-dialog>
15
+     <input id="excel-upload-input" ref="excel-upload-input" type="file"  accept=".xlsx,.xls,.xltx," @change="handleClick" >
16
+    <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="msgTipVisible = true">导入排班
17
+    </el-button>
18
+  </div>
19
+</template>
20
+
21
+<script>
22
+  import XLSX from 'xlsx'
23
+
24
+  export default {
25
+    name: "scheduleUploadExcel",
26
+    props: {
27
+      beforeUpload: Function,
28
+      onSuccess: Function
29
+    },
30
+    data() {
31
+      return {
32
+        loading: false,
33
+        msgTipVisible:false,
34
+        excelData: {
35
+          header: null,
36
+          results: null
37
+        }
38
+      }
39
+    },
40
+    methods: {
41
+      generateDate({ header, results }) {
42
+        console.log("header",header)
43
+        console.log("results",results)
44
+        this.excelData.header = header
45
+        this.excelData.results = results
46
+
47
+        this.onSuccess && this.onSuccess(this.excelData)
48
+      },
49
+      handleDrop(e) {
50
+        e.stopPropagation()
51
+        e.preventDefault()
52
+        if (this.loading) return
53
+        const files = e.dataTransfer.files
54
+        if (files.length !== 1) {
55
+          this.$message.error('Only support uploading one file!')
56
+          return
57
+        }
58
+        const rawFile = files[0] // only use files[0]
59
+
60
+        if (!this.isExcel(rawFile)) {
61
+          this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
62
+          return false
63
+        }
64
+        this.upload(rawFile)
65
+        e.stopPropagation()
66
+        e.preventDefault()
67
+      },
68
+      handleDragover(e) {
69
+        e.stopPropagation()
70
+        e.preventDefault()
71
+        e.dataTransfer.dropEffect = 'copy'
72
+      },
73
+      handleUpload() {
74
+        this.msgTipVisible = false
75
+
76
+        document.getElementById('excel-upload-input').click()
77
+      },
78
+      handleClick(e) {
79
+
80
+        const files = e.target.files
81
+        const rawFile = files[0] // only use files[0]
82
+        console.log("rawfiel",rawFile)
83
+        if (!rawFile)
84
+        return
85
+        this.upload(rawFile)
86
+
87
+      },
88
+      upload(rawFile) {
89
+
90
+        this.$refs['excel-upload-input'].value = null // fix can't select the same excel
91
+        console.log("3333",this.beforeUpload)
92
+        if (!this.beforeUpload) {
93
+          this.readerData(rawFile)
94
+          return
95
+        }
96
+        const before = this.beforeUpload(rawFile)
97
+        console.log("before",before)
98
+        if (before) {
99
+          this.readerData(rawFile)
100
+        }
101
+      },
102
+      readerData(rawFile) {
103
+
104
+        this.loading = true
105
+        return new Promise((resolve, reject) => {
106
+          const reader = new FileReader()
107
+          reader.onload = e => {
108
+
109
+            const data = e.target.result
110
+            console.log("data22222",data)
111
+            const fixedData = this.fixdata(data)
112
+            const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
113
+            const firstSheetName = workbook.SheetNames[0]
114
+            const worksheet = workbook.Sheets[firstSheetName]
115
+
116
+            const header = this.get_header_row(worksheet)
117
+
118
+            const results = XLSX.utils.sheet_to_json(worksheet)
119
+            this.generateDate({ header, results })
120
+            this.loading = false
121
+            resolve()
122
+          }
123
+          reader.readAsArrayBuffer(rawFile)
124
+        })
125
+      },
126
+      fixdata(data) {
127
+        let o = ''
128
+        let l = 0
129
+        const w = 10240
130
+        for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
131
+        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
132
+        return o
133
+      },
134
+      get_header_row(sheet) {
135
+
136
+        if(sheet['!ref'] == undefined){
137
+          this.loading = false
138
+          return
139
+        }
140
+
141
+        const headers = []
142
+        const range = XLSX.utils.decode_range(sheet['!ref'])
143
+        let C
144
+        const R = range.s.r /* start in the first row */
145
+        for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
146
+          var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
147
+          var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
148
+          if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
149
+          headers.push(hdr)
150
+        }
151
+        return headers
152
+
153
+
154
+
155
+      },
156
+      isExcel(file) {
157
+
158
+        return /\.(xlsx|xls|csv)$/.test(file.name)
159
+      }
160
+    }
161
+  }
162
+</script>
163
+
164
+<style scoped>
165
+  #excel-upload-input {
166
+    display: none;
167
+    z-index: -9999;
168
+  }
169
+
170
+  #drop {
171
+    border: 2px dashed #bbb;
172
+    width: 600px;
173
+    height: 160px;
174
+    line-height: 160px;
175
+    margin: 0 auto;
176
+    font-size: 24px;
177
+    border-radius: 5px;
178
+    text-align: center;
179
+    color: #bbb;
180
+    position: relative;
181
+  }
182
+</style>