vxe-table-plugin-export-xlsx-xhx 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts ADDED
@@ -0,0 +1,219 @@
1
+ /* eslint-disable no-unused-vars */
2
+ import XEUtils from 'xe-utils/methods/xe-utils'
3
+ import {
4
+ VXETable,
5
+ Table,
6
+ InterceptorExportParams,
7
+ InterceptorImportParams,
8
+ ColumnConfig,
9
+ ExportOptons
10
+ } from 'vxe-table/lib/vxe-table'
11
+ import XLSX from 'xlsx'
12
+ /* eslint-enable no-unused-vars */
13
+
14
+ let _vxetable: typeof VXETable
15
+
16
+
17
+ function getCellLabel (column: ColumnConfig, cellValue: any) {
18
+ if (cellValue) {
19
+ switch (column.cellType) {
20
+ case 'string':
21
+ return XEUtils.toValueString(cellValue)
22
+ case 'number':
23
+ if (!isNaN(cellValue)) {
24
+ return Number(cellValue)
25
+ }
26
+ break
27
+ default:
28
+ if (cellValue.length < 12 && !isNaN(cellValue)) {
29
+ return Number(cellValue)
30
+ }
31
+ break
32
+ }
33
+ }
34
+ return cellValue
35
+ }
36
+
37
+
38
+
39
+ function getFooterCellValue ($table: Table, opts: ExportOptons, rows: any[], column: ColumnConfig) {
40
+ var cellValue = XEUtils.toString(rows[$table.$getColumnIndex(column)])
41
+ return cellValue
42
+ }
43
+
44
+ function toBuffer (wbout: any) {
45
+ let buf = new ArrayBuffer(wbout.length)
46
+ let view = new Uint8Array(buf)
47
+ for (let index = 0; index !== wbout.length; ++index) view[index] = wbout.charCodeAt(index) & 0xFF
48
+ return buf
49
+ }
50
+
51
+ function exportXLSX (params: InterceptorExportParams) {
52
+ const { $table, options, columns, datas } = params
53
+ const { sheetName, type, isHeader, isFooter, original, message, footerFilterMethod } = options
54
+ const colHead: { [key: string]: any } = {}
55
+ const footList: { [key: string]: any }[] = []
56
+ // const rowList = datas
57
+
58
+ if (isHeader) {
59
+ columns.forEach((column) => {
60
+ colHead[column.id] = XEUtils.toString(original ? column.property : column.getTitle())
61
+ })
62
+ }
63
+
64
+ // 新增部分
65
+ const rowList = datas.map(item => {
66
+ const rest: any = {}
67
+ columns.forEach((column) => {
68
+ rest[column.id] = getCellLabel(column, item[column.id])
69
+ })
70
+ return rest
71
+ })
72
+
73
+ if (isFooter) {
74
+ const { footerData } = $table.getTableData()
75
+ const footers = footerFilterMethod ? footerData.filter(footerFilterMethod) : footerData
76
+ footers.forEach((rows) => {
77
+ const item: { [key: string]: any } = {}
78
+ columns.forEach((column) => {
79
+ item[column.id] = getFooterCellValue($table, options, rows, column)
80
+ })
81
+ footList.push(item)
82
+ })
83
+ }
84
+ const book = XLSX.utils.book_new()
85
+ const sheet = XLSX.utils.json_to_sheet((isHeader ? [colHead] : []).concat(rowList).concat(footList), { skipHeader: true })
86
+ // 转换数据
87
+ XLSX.utils.book_append_sheet(book, sheet, sheetName)
88
+ const wbout = XLSX.write(book, { bookType: type, bookSST: false, type: 'binary' })
89
+ const blob = new Blob([toBuffer(wbout)], { type: 'application/octet-stream' })
90
+ // 保存导出
91
+ downloadFile(blob, options)
92
+ if (message !== false) {
93
+ _vxetable.modal.message({ message: _vxetable.t('vxe.table.expSuccess'), status: 'success' })
94
+ }
95
+ }
96
+
97
+ function downloadFile (blob: Blob, options: ExportOptons) {
98
+ if (window.Blob) {
99
+ const { filename, type } = options
100
+ if (navigator.msSaveBlob) {
101
+ navigator.msSaveBlob(blob, `${filename}.${type}`)
102
+ } else {
103
+ var linkElem = document.createElement('a')
104
+ linkElem.target = '_blank'
105
+ linkElem.download = `${filename}.${type}`
106
+ linkElem.href = URL.createObjectURL(blob)
107
+ document.body.appendChild(linkElem)
108
+ linkElem.click()
109
+ document.body.removeChild(linkElem)
110
+ }
111
+ } else {
112
+ console.error(_vxetable.t('vxe.error.notExp'))
113
+ }
114
+ }
115
+
116
+ function replaceDoubleQuotation (val: string) {
117
+ return val.replace(/^"/, '').replace(/"$/, '')
118
+ }
119
+
120
+ function parseCsv (columns: ColumnConfig[], content: string) {
121
+ const list = content.split('\n')
122
+ const fields: string[] = []
123
+ const rows: any[] = []
124
+ if (list.length) {
125
+ const rList = list.slice(1)
126
+ list[0].split(',').map(replaceDoubleQuotation)
127
+ rList.forEach((r) => {
128
+ if (r) {
129
+ const item: { [key: string]: any } = {}
130
+ r.split(',').forEach((val, colIndex) => {
131
+ if (fields[colIndex]) {
132
+ item[fields[colIndex]] = replaceDoubleQuotation(val)
133
+ }
134
+ })
135
+ rows.push(item)
136
+ }
137
+ })
138
+ }
139
+ return { fields, rows }
140
+ }
141
+
142
+ function checkImportData (columns: ColumnConfig[], fields: string[], rows: any[]) {
143
+ let tableFields: string[] = []
144
+ columns.forEach((column) => {
145
+ let field = column.property
146
+ if (field) {
147
+ tableFields.push(field)
148
+ }
149
+ })
150
+ return tableFields.every((field) => fields.includes(field))
151
+ }
152
+
153
+ function importXLSX (params: InterceptorImportParams) {
154
+ const { columns, options, file } = params
155
+ const $table: any = params.$table
156
+ const { _importResolve } = $table
157
+ const fileReader = new FileReader()
158
+ fileReader.onload = (e: any) => {
159
+ const workbook = XLSX.read(e.target.result, { type: 'binary' })
160
+ const csvData: string = XLSX.utils.sheet_to_csv(workbook.Sheets.Sheet1)
161
+ const { fields, rows } = parseCsv(columns, csvData)
162
+ const status = checkImportData(columns, fields, rows)
163
+ if (status) {
164
+ $table.createData(rows)
165
+ .then((data: any[]) => {
166
+ if (options.mode === 'append') {
167
+ $table.insertAt(data, -1)
168
+ } else {
169
+ $table.reloadData(data)
170
+ }
171
+ })
172
+ if (options.message !== false) {
173
+ _vxetable.modal.message({ message: XEUtils.template(_vxetable.t('vxe.table.impSuccess'), [rows.length]), status: 'success' })
174
+ }
175
+ } else if (options.message !== false) {
176
+ _vxetable.modal.message({ message: _vxetable.t('vxe.error.impFields'), status: 'error' })
177
+ }
178
+ if (_importResolve) {
179
+ _importResolve(status)
180
+ $table._importResolve = null
181
+ }
182
+ }
183
+ fileReader.readAsBinaryString(file)
184
+ }
185
+
186
+ function handleImportEvent (params: InterceptorImportParams) {
187
+ if (params.options.type === 'xlsx') {
188
+ importXLSX(params)
189
+ return false
190
+ }
191
+ }
192
+
193
+ function handleExportEvent (params: InterceptorExportParams) {
194
+ if (params.options.type === 'xlsx') {
195
+ exportXLSX(params)
196
+ return false
197
+ }
198
+ }
199
+
200
+ /**
201
+ * 基于 vxe-table 表格的增强插件,支持导出 xlsx 格式
202
+ */
203
+ export const VXETablePluginExportXLSX = {
204
+ install (xtable: typeof VXETable) {
205
+ const { interceptor } = xtable
206
+ _vxetable = xtable
207
+ Object.assign(xtable.types, { xlsx: 1 })
208
+ interceptor.mixin({
209
+ 'event.import': handleImportEvent,
210
+ 'event.export': handleExportEvent
211
+ })
212
+ }
213
+ }
214
+
215
+ if (typeof window !== 'undefined' && window.VXETable) {
216
+ window.VXETable.use(VXETablePluginExportXLSX)
217
+ }
218
+
219
+ export default VXETablePluginExportXLSX
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "vxe-table-plugin-export-xlsx-xhx",
3
+ "version": "1.0.1",
4
+ "description": "基于 vxe-table 表格的增强插件,支持导出 xlsx 格式",
5
+ "scripts": {
6
+ "lib": "gulp build"
7
+ },
8
+ "files": [
9
+ "dist",
10
+ "*.ts",
11
+ "*.d.ts"
12
+ ],
13
+ "main": "dist/index.common.js",
14
+ "style": "dist/style.min.css",
15
+ "unpkg": "dist/index.min.js",
16
+ "jsdelivr": "dist/index.min.js",
17
+ "typings": "index.ts",
18
+ "devDependencies": {
19
+ "@babel/core": "^7.4.4",
20
+ "@babel/plugin-transform-runtime": "^7.4.4",
21
+ "@babel/preset-env": "^7.4.4",
22
+ "@babel/runtime": "^7.4.4",
23
+ "@typescript-eslint/eslint-plugin": "^2.3.1",
24
+ "@typescript-eslint/parser": "^2.3.1",
25
+ "del": "^5.1.0",
26
+ "eslint": "^5.15.1",
27
+ "eslint-config-prettier": "^6.3.0",
28
+ "eslint-config-standard": "^12.0.0",
29
+ "eslint-friendly-formatter": "^4.0.1",
30
+ "eslint-plugin-import": "^2.16.0",
31
+ "eslint-plugin-node": "^8.0.1",
32
+ "eslint-plugin-prettier": "^3.1.1",
33
+ "eslint-plugin-promise": "^4.0.1",
34
+ "eslint-plugin-standard": "^4.0.0",
35
+ "eslint-plugin-typescript": "^0.14.0",
36
+ "gulp": "^4.0.2",
37
+ "gulp-autoprefixer": "^6.1.0",
38
+ "gulp-babel": "^8.0.0",
39
+ "gulp-clean-css": "^4.2.0",
40
+ "gulp-concat": "^2.6.1",
41
+ "gulp-rename": "^1.4.0",
42
+ "gulp-replace": "^1.0.0",
43
+ "gulp-sass": "^4.0.2",
44
+ "gulp-sourcemaps": "^2.6.5",
45
+ "gulp-typescript": "^5.0.1",
46
+ "gulp-uglify": "^3.0.2",
47
+ "markdown-doctest": "^0.9.1",
48
+ "prettier": "^1.18.2",
49
+ "typescript": "^3.8.3",
50
+ "vue": "^2.6.11",
51
+ "vxe-table": "^2.8.33",
52
+ "xe-utils": "^2.4.2",
53
+ "xlsx": "^0.15.6"
54
+ },
55
+ "peerDependencies": {
56
+ "vxe-table": ">=2.8.0",
57
+ "xe-utils": ">=2.4.0",
58
+ "xlsx": ">=0.15.0"
59
+ },
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "git+https://github.com/xuliangzhan/vxe-table-plugin-export-xlsx.git"
63
+ },
64
+ "keywords": [
65
+ "vxe-table-plugin-export-xlsx-xhx"
66
+ ],
67
+ "author": {
68
+ "name": "shihao.sun",
69
+ "email": "shihao.sun@dhc.com.cn"
70
+ },
71
+ "license": "MIT",
72
+ "bugs": {
73
+ "url": "https://github.com/xuliangzhan/vxe-table-plugin-export-xlsx/issues"
74
+ },
75
+ "homepage": "https://github.com/xuliangzhan/vxe-table-plugin-export-xlsx#readme"
76
+ }