@bchr/bchr-front-lib 0.6.10 → 0.6.11

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.
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
- var ExcelJS = _interopRequireWildcard(require("exceljs"));
7
+ var ExcelJS = _interopRequireWildcard(require("exceljs/dist/exceljs.bare.js"));
8
8
  var _fileSaver = require("file-saver");
9
9
  var _Values = _interopRequireDefault(require("./Values"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
@@ -222,10 +222,10 @@ var ExcelKits = {
222
222
  children: child.children && _this.extractColumns(child.children, level + 1)
223
223
  });
224
224
  },
225
- /**
226
- * 收集Excel列头信息(复合表头)
227
- * @param gridColumns
228
- * @returns {{totalColumnCount: {int}, totalColumnHeaderRowCount: {int}, columnHeaderArray: [] }}
225
+ /**
226
+ * 收集Excel列头信息(复合表头)
227
+ * @param gridColumns
228
+ * @returns {{totalColumnCount: {int}, totalColumnHeaderRowCount: {int}, columnHeaderArray: [] }}
229
229
  */
230
230
  gatherExcelColumnHeaderStructureInfo: function gatherExcelColumnHeaderStructureInfo(gridColumns) {
231
231
  var _this2 = this;
@@ -298,14 +298,14 @@ var ExcelKits = {
298
298
  columnHeaderMergeCellArray: columnHeaderMergeCellArray
299
299
  };
300
300
  },
301
- /**
302
- * @return {Array} eg -
303
- * [
304
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
305
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
306
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
307
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
308
- * ]
301
+ /**
302
+ * @return {Array} eg -
303
+ * [
304
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
305
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
306
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
307
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
308
+ * ]
309
309
  */
310
310
  gatherExcelColumnHeaderMergeCellInfo: function gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount) {
311
311
  var currentColumnHeaderRowIndex = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
@@ -386,7 +386,7 @@ var ExcelKits = {
386
386
  header: column.title,
387
387
  key: column.dataIndex,
388
388
  locked: column.locked,
389
- width: column.width * 0.1
389
+ width: column.width != null ? column.width * 0.1 : undefined
390
390
  };
391
391
  }
392
392
  };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@bchr/bchr-front-lib",
3
- "version": "0.6.10",
3
+ "version": "0.6.11",
4
4
  "description": "bchr front lib",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "prepublish": "babel src --out-dir lib --copy-files",
8
8
  "login": "npm login",
9
- "publish": "npm publish "
9
+ "publish": "npm publish ",
10
+ "unpublish": "npm unpublish "
10
11
  },
11
12
  "publishConfig": {
12
13
  "registry": "https://registry.npmjs.org",
@@ -1,373 +1,373 @@
1
- import * as ExcelJS from "exceljs";
2
- import {saveAs} from "file-saver";
3
- import Values from "./Values";
4
-
5
-
6
- const DEFAULT_BORDER_COLOR_FOR_EXCEL = "CCCCCC";
7
- const DEFAULT_FG_COLOR_FOR_EXCEL = "FFFFFF";
8
- const DEFAULT_COL_HEADER_BG_COLOR = "F2F3F5";
9
-
10
- const excelHeaderRowStyle = {
11
- font: {
12
- size: 10,
13
- bold: true
14
- },
15
- alignment: {
16
- vertical: 'middle',
17
- horizontal: 'center'
18
- },
19
- fill: {
20
- type: 'pattern',
21
- pattern: 'solid',
22
- fgColor: {
23
- argb: DEFAULT_COL_HEADER_BG_COLOR
24
- }
25
- },
26
- border: {
27
- top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
28
- left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
29
- bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
30
- right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
31
- }
32
- }
33
-
34
- const excelBodyRowStyle = {
35
- font: {
36
- size: 10,
37
- bold: true
38
- },
39
- alignment: {
40
- vertical: 'middle',
41
- horizontal: 'center'
42
- },
43
- fill: {
44
- type: 'pattern',
45
- pattern: 'solid',
46
- fgColor: {
47
- argb: DEFAULT_FG_COLOR_FOR_EXCEL
48
- }
49
- },
50
- border: {
51
- top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
52
- left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
53
- bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
54
- right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
55
- }
56
- }
57
-
58
- const ExcelKits = {
59
-
60
- exportHandsontableData2CSVFile(handsonTableInstance, fileName) {
61
- if (handsonTableInstance === null || handsonTableInstance === void 0) {
62
- throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2CSVFile");
63
- }
64
-
65
- if (Values.isBlankString(fileName)) {
66
- fileName = `${t('ui.export.csv.file')}-` + new Date().getTime();
67
- }
68
-
69
- const exportPlugin = handsonTableInstance.getPlugin('exportFile');
70
- exportPlugin.downloadFile('csv', {
71
- bom: false,
72
- columnDelimiter: ',',
73
- columnHeaders: false,
74
- exportHiddenColumns: true,
75
- exportHiddenRows: true,
76
- fileExtension: 'csv',
77
- filename: fileName,
78
- mimeType: 'text/csv',
79
- rowDelimiter: '\r\n',
80
- rowHeaders: true
81
- });
82
- },
83
-
84
- exportHandsontableData2ExcelFile(handsontableInstance, fileName) {
85
- if (handsontableInstance === null || handsontableInstance === void 0) {
86
- throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2ExcelFile");
87
- }
88
-
89
- if (Values.isBlankString(fileName)) {
90
- fileName = t('ui.export.excel.file') + "-" + new Date().getTime();
91
- }
92
-
93
- let exportGridColumns = [];
94
- let exportGridData = handsontableInstance.hotInstance.getSourceData();
95
-
96
- window.handsontableInstance = handsontableInstance;
97
-
98
- if (handsontableInstance.columnSettings && handsontableInstance.columnSettings.length) {
99
- handsontableInstance.columnSettings.forEach(column => {
100
- exportGridColumns.push({
101
- ...column,
102
- dataIndex: column.data
103
- })
104
- })
105
- }
106
-
107
- console.log('exportGridColumns :', exportGridColumns)
108
- console.log('exportGridData :', exportGridData)
109
-
110
- this.exportTableData2ExcelFile(exportGridColumns, exportGridData, fileName);
111
- },
112
-
113
- exportTableData2ExcelFile(columns = [], data = [], fileName) {
114
-
115
- if (Values.isBlankString(fileName)) {
116
- fileName = `${t('ui.export.excel.file')}-` + new Date().getTime();
117
- }
118
-
119
- const workbook = new ExcelJS.Workbook();
120
- const worksheet = workbook.addWorksheet('sheet');
121
-
122
- let exportGridColumnsData = this.gatherExcelColumnHeaderStructureInfo(this.extractColumns(columns));
123
- let totalColumnHeaderRowCount = exportGridColumnsData.totalColumnHeaderRowCount;
124
- let columnHeaderData = exportGridColumnsData.columnHeaderData;
125
- let rowHeaderArray = exportGridColumnsData.rowHeaderArray;
126
- let columnHeaderMergeCellArray = exportGridColumnsData.columnHeaderMergeCellArray;
127
-
128
- let exportGridData = data;
129
-
130
- worksheet.columns = columnHeaderData;
131
- worksheet.spliceRows(1, 1);
132
- worksheet.insertRows(1, rowHeaderArray);
133
-
134
- exportGridData.forEach(gridItem => {
135
- worksheet.addRow(gridItem);
136
- })
137
-
138
- columnHeaderMergeCellArray.forEach(mergeCells => {
139
- worksheet.mergeCells(mergeCells)
140
- })
141
-
142
- // 设置表头样式
143
- const headerRows = worksheet.getRows(1, totalColumnHeaderRowCount);
144
- headerRows.forEach((row, rowIndex) => {
145
- row.eachCell((cell, cellIndex) => {
146
- cell.style = excelHeaderRowStyle;
147
- })
148
- })
149
-
150
- // 设置表体样式
151
- const bodyRows = worksheet.getRows(totalColumnHeaderRowCount + 1, totalColumnHeaderRowCount + exportGridData.length + 5);
152
- bodyRows.forEach((row) => {
153
- row.eachCell((cell, cellIndex) => {
154
- cell.style = excelBodyRowStyle;
155
- })
156
- })
157
-
158
- workbook.xlsx.writeBuffer().then(buffer => {
159
- saveAs(new Blob([buffer], {
160
- type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
161
- }), fileName + ".xlsx")
162
- })
163
- },
164
-
165
- extractColumns(children, level) {
166
- let _this = this;
167
- if (level === void 0) {
168
- level = 0;
169
- }
170
- return Array.isArray(children)
171
- ? children.map(function (child) {
172
- return _this.extractChild(child, level);
173
- })
174
- : [children, _this.extractChild(children, level)];
175
- },
176
-
177
- extractChild(child, level) {
178
- let _this = this;
179
- if (level === void 0) {
180
- level = 0;
181
- }
182
- return Object.assign(Object.assign({}, child), {
183
- level: level,
184
- children: child.children && _this.extractColumns(child.children, level + 1)
185
- })
186
- },
187
-
188
- /**
189
- * 收集Excel列头信息(复合表头)
190
- * @param gridColumns
191
- * @returns {{totalColumnCount: {int}, totalColumnHeaderRowCount: {int}, columnHeaderArray: [] }}
192
- */
193
- gatherExcelColumnHeaderStructureInfo(gridColumns) {
194
- let [totalColumnCount, totalColumnHeaderRowCount] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, 0, 1);
195
-
196
- let columnHeaderArray = [];
197
- for (let i = 0, count = gridColumns.length; i < count; i++) {
198
- let column = gridColumns[i];
199
- this.$buildColumnHeaderArray(column, columnHeaderArray);
200
- }
201
-
202
- for (let i = 0, count = columnHeaderArray.length; i < count; i++) {
203
- let subArray = columnHeaderArray[i];
204
- let lastElem = subArray[subArray.length - 1];
205
- for (let r = subArray.length; r < totalColumnHeaderRowCount; r++) {
206
- subArray.push(lastElem);
207
- }
208
- }
209
-
210
- let columnHeaderMergeCellArray = [];
211
- let nextColIndex = 1;
212
- for (let i = 0, count = gridColumns.length; i < count; i++) {
213
- let gridColumn = gridColumns[i];
214
- nextColIndex = this.gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, 1, nextColIndex);
215
- }
216
-
217
- let rowHeaderArray = [];
218
- for (let totalColumnHeaderRowStep = 0; totalColumnHeaderRowStep < totalColumnHeaderRowCount; totalColumnHeaderRowStep++) {
219
- let rowHeaderColumns = [];
220
- columnHeaderArray.forEach(columns => {
221
- rowHeaderColumns.push(columns[totalColumnHeaderRowStep])
222
- })
223
- rowHeaderArray.push(rowHeaderColumns);
224
- }
225
-
226
- let columnHeaderData = [];
227
- let columnIndex = 0;
228
-
229
- let addColumns = (columns, result) => {
230
- columns.forEach((column) => {
231
- let exporterColumn = this.$normalizedExportColumn(column);
232
- result.push(exporterColumn);
233
- if (column.children && column.children.length > 1) {
234
- let children = exporterColumn.columns = [];
235
- addColumns(column.children, children);
236
- } else {
237
- columnIndex++;
238
- }
239
- });
240
- };
241
- addColumns(gridColumns, columnHeaderData);
242
- const autoCreateArrayItems = function (column, columnStep, columns) {
243
- if (column.columns && column.columns.length) {
244
- columns.splice(columnStep, 1, ...column.columns);
245
- }
246
-
247
- if (columnStep === columns.length - 1) {
248
- console.log('success');
249
- } else {
250
- columnStep++;
251
- autoCreateArrayItems(columns[columnStep], columnStep, columns);
252
- }
253
- }
254
-
255
- autoCreateArrayItems(columnHeaderData[0], 0, columnHeaderData);
256
-
257
- return {
258
- totalColumnCount,
259
- totalColumnHeaderRowCount,
260
- columnHeaderArray,
261
- columnHeaderData,
262
- rowHeaderArray,
263
- columnHeaderMergeCellArray
264
- };
265
- },
266
-
267
- /**
268
- * @return {Array} eg -
269
- * [
270
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
271
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
272
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
273
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
274
- * ]
275
- */
276
- gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, currentColumnHeaderRowIndex = 1, colIndex) {
277
-
278
- let allChildColumnCount = this.$countAllChildColumnCount(gridColumn);
279
-
280
- if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
281
- let gridColumns = gridColumn.children;
282
- let gridColumnCount = gridColumns.length;
283
-
284
- columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, currentColumnHeaderRowIndex, colIndex + allChildColumnCount - 1]);
285
-
286
- for (let i = 0; i < gridColumnCount; i++) {
287
- this.gatherExcelColumnHeaderMergeCellInfo(gridColumns[i], columnHeaderMergeCellArray, totalColumnHeaderRowCount,
288
- currentColumnHeaderRowIndex + 1, colIndex + i);
289
- }
290
- } else {
291
- if (currentColumnHeaderRowIndex !== totalColumnHeaderRowCount) {
292
- columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, totalColumnHeaderRowCount, colIndex]);
293
- }
294
- }
295
-
296
- return colIndex + allChildColumnCount;
297
- },
298
-
299
- $countAllChildColumnCount(gridColumn) {
300
- let columnCount = 0;
301
- if (gridColumn === null || gridColumn === void 0) {
302
- return columnCount;
303
- }
304
-
305
- if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
306
- let gridColumns = gridColumn.children;
307
- let gridColumnCount = gridColumns.length;
308
-
309
- for (let i = 0; i < gridColumnCount; i++) {
310
- let childColumnCount = this.$countAllChildColumnCount(gridColumns[i]);
311
- columnCount = columnCount + childColumnCount;
312
- }
313
- } else {
314
- columnCount = 1;
315
- }
316
-
317
- return columnCount;
318
- },
319
-
320
- getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, totalColumnCount, totalColumnHeaderCount) {
321
- let maxN2 = totalColumnHeaderCount;
322
- for (let i = 0, count = gridColumns.length; i < count; i++) {
323
- let column = gridColumns[i];
324
- if (Array.isArray(column.children) && column.children.length) {
325
- let [n1, n2] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(column.children, 0, totalColumnHeaderCount + 1);
326
- totalColumnCount = totalColumnCount + n1;
327
- if (n2 > maxN2) {
328
- maxN2 = n2;
329
- }
330
- } else {
331
- totalColumnCount = totalColumnCount + 1;
332
- }
333
- }
334
-
335
- return [totalColumnCount, maxN2]
336
- },
337
-
338
- $buildColumnHeaderArray(column, columnHeaderArray, parentHeaderTitles) {
339
- if (Array.isArray(column.children) && column.children.length) {
340
- if (parentHeaderTitles === null || parentHeaderTitles == void 0) {
341
- parentHeaderTitles = [column.title];
342
- } else {
343
- parentHeaderTitles.push(column.title);
344
- }
345
-
346
- for (let i = 0, count = column.children.length; i < count; i++) {
347
- let childColumn = column.children[i];
348
- this.$buildColumnHeaderArray(childColumn, columnHeaderArray, parentHeaderTitles);
349
- }
350
- } else {
351
- let columnSelfHeaderTitleArray = [];
352
- if (Array.isArray(parentHeaderTitles)) {
353
- columnSelfHeaderTitleArray.push(...parentHeaderTitles);
354
- }
355
-
356
- columnSelfHeaderTitleArray.push(column.title);
357
-
358
- columnHeaderArray.push(columnSelfHeaderTitleArray)
359
- }
360
- },
361
-
362
-
363
- $normalizedExportColumn(column) {
364
- return {
365
- header : column.title,
366
- key : column.dataIndex,
367
- locked : column.locked,
368
- width : column.width * 0.1
369
- }
370
- }
371
- }
372
-
373
- export default ExcelKits;
1
+ import * as ExcelJS from "exceljs/dist/exceljs.bare.js";
2
+ import {saveAs} from "file-saver";
3
+ import Values from "./Values";
4
+
5
+
6
+ const DEFAULT_BORDER_COLOR_FOR_EXCEL = "CCCCCC";
7
+ const DEFAULT_FG_COLOR_FOR_EXCEL = "FFFFFF";
8
+ const DEFAULT_COL_HEADER_BG_COLOR = "F2F3F5";
9
+
10
+ const excelHeaderRowStyle = {
11
+ font: {
12
+ size: 10,
13
+ bold: true
14
+ },
15
+ alignment: {
16
+ vertical: 'middle',
17
+ horizontal: 'center'
18
+ },
19
+ fill: {
20
+ type: 'pattern',
21
+ pattern: 'solid',
22
+ fgColor: {
23
+ argb: DEFAULT_COL_HEADER_BG_COLOR
24
+ }
25
+ },
26
+ border: {
27
+ top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
28
+ left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
29
+ bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
30
+ right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
31
+ }
32
+ }
33
+
34
+ const excelBodyRowStyle = {
35
+ font: {
36
+ size: 10,
37
+ bold: true
38
+ },
39
+ alignment: {
40
+ vertical: 'middle',
41
+ horizontal: 'center'
42
+ },
43
+ fill: {
44
+ type: 'pattern',
45
+ pattern: 'solid',
46
+ fgColor: {
47
+ argb: DEFAULT_FG_COLOR_FOR_EXCEL
48
+ }
49
+ },
50
+ border: {
51
+ top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
52
+ left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
53
+ bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
54
+ right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
55
+ }
56
+ }
57
+
58
+ const ExcelKits = {
59
+
60
+ exportHandsontableData2CSVFile(handsonTableInstance, fileName) {
61
+ if (handsonTableInstance === null || handsonTableInstance === void 0) {
62
+ throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2CSVFile");
63
+ }
64
+
65
+ if (Values.isBlankString(fileName)) {
66
+ fileName = `${t('ui.export.csv.file')}-` + new Date().getTime();
67
+ }
68
+
69
+ const exportPlugin = handsonTableInstance.getPlugin('exportFile');
70
+ exportPlugin.downloadFile('csv', {
71
+ bom: false,
72
+ columnDelimiter: ',',
73
+ columnHeaders: false,
74
+ exportHiddenColumns: true,
75
+ exportHiddenRows: true,
76
+ fileExtension: 'csv',
77
+ filename: fileName,
78
+ mimeType: 'text/csv',
79
+ rowDelimiter: '\r\n',
80
+ rowHeaders: true
81
+ });
82
+ },
83
+
84
+ exportHandsontableData2ExcelFile(handsontableInstance, fileName) {
85
+ if (handsontableInstance === null || handsontableInstance === void 0) {
86
+ throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2ExcelFile");
87
+ }
88
+
89
+ if (Values.isBlankString(fileName)) {
90
+ fileName = t('ui.export.excel.file') + "-" + new Date().getTime();
91
+ }
92
+
93
+ let exportGridColumns = [];
94
+ let exportGridData = handsontableInstance.hotInstance.getSourceData();
95
+
96
+ window.handsontableInstance = handsontableInstance;
97
+
98
+ if (handsontableInstance.columnSettings && handsontableInstance.columnSettings.length) {
99
+ handsontableInstance.columnSettings.forEach(column => {
100
+ exportGridColumns.push({
101
+ ...column,
102
+ dataIndex: column.data
103
+ })
104
+ })
105
+ }
106
+
107
+ console.log('exportGridColumns :', exportGridColumns)
108
+ console.log('exportGridData :', exportGridData)
109
+
110
+ this.exportTableData2ExcelFile(exportGridColumns, exportGridData, fileName);
111
+ },
112
+
113
+ exportTableData2ExcelFile(columns = [], data = [], fileName) {
114
+
115
+ if (Values.isBlankString(fileName)) {
116
+ fileName = `${t('ui.export.excel.file')}-` + new Date().getTime();
117
+ }
118
+
119
+ const workbook = new ExcelJS.Workbook();
120
+ const worksheet = workbook.addWorksheet('sheet');
121
+
122
+ let exportGridColumnsData = this.gatherExcelColumnHeaderStructureInfo(this.extractColumns(columns));
123
+ let totalColumnHeaderRowCount = exportGridColumnsData.totalColumnHeaderRowCount;
124
+ let columnHeaderData = exportGridColumnsData.columnHeaderData;
125
+ let rowHeaderArray = exportGridColumnsData.rowHeaderArray;
126
+ let columnHeaderMergeCellArray = exportGridColumnsData.columnHeaderMergeCellArray;
127
+
128
+ let exportGridData = data;
129
+
130
+ worksheet.columns = columnHeaderData;
131
+ worksheet.spliceRows(1, 1);
132
+ worksheet.insertRows(1, rowHeaderArray);
133
+
134
+ exportGridData.forEach(gridItem => {
135
+ worksheet.addRow(gridItem);
136
+ })
137
+
138
+ columnHeaderMergeCellArray.forEach(mergeCells => {
139
+ worksheet.mergeCells(mergeCells)
140
+ })
141
+
142
+ // 设置表头样式
143
+ const headerRows = worksheet.getRows(1, totalColumnHeaderRowCount);
144
+ headerRows.forEach((row, rowIndex) => {
145
+ row.eachCell((cell, cellIndex) => {
146
+ cell.style = excelHeaderRowStyle;
147
+ })
148
+ })
149
+
150
+ // 设置表体样式
151
+ const bodyRows = worksheet.getRows(totalColumnHeaderRowCount + 1, totalColumnHeaderRowCount + exportGridData.length + 5);
152
+ bodyRows.forEach((row) => {
153
+ row.eachCell((cell, cellIndex) => {
154
+ cell.style = excelBodyRowStyle;
155
+ })
156
+ })
157
+
158
+ workbook.xlsx.writeBuffer().then(buffer => {
159
+ saveAs(new Blob([buffer], {
160
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
161
+ }), fileName + ".xlsx")
162
+ })
163
+ },
164
+
165
+ extractColumns(children, level) {
166
+ let _this = this;
167
+ if (level === void 0) {
168
+ level = 0;
169
+ }
170
+ return Array.isArray(children)
171
+ ? children.map(function (child) {
172
+ return _this.extractChild(child, level);
173
+ })
174
+ : [children, _this.extractChild(children, level)];
175
+ },
176
+
177
+ extractChild(child, level) {
178
+ let _this = this;
179
+ if (level === void 0) {
180
+ level = 0;
181
+ }
182
+ return Object.assign(Object.assign({}, child), {
183
+ level: level,
184
+ children: child.children && _this.extractColumns(child.children, level + 1)
185
+ })
186
+ },
187
+
188
+ /**
189
+ * 收集Excel列头信息(复合表头)
190
+ * @param gridColumns
191
+ * @returns {{totalColumnCount: {int}, totalColumnHeaderRowCount: {int}, columnHeaderArray: [] }}
192
+ */
193
+ gatherExcelColumnHeaderStructureInfo(gridColumns) {
194
+ let [totalColumnCount, totalColumnHeaderRowCount] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, 0, 1);
195
+
196
+ let columnHeaderArray = [];
197
+ for (let i = 0, count = gridColumns.length; i < count; i++) {
198
+ let column = gridColumns[i];
199
+ this.$buildColumnHeaderArray(column, columnHeaderArray);
200
+ }
201
+
202
+ for (let i = 0, count = columnHeaderArray.length; i < count; i++) {
203
+ let subArray = columnHeaderArray[i];
204
+ let lastElem = subArray[subArray.length - 1];
205
+ for (let r = subArray.length; r < totalColumnHeaderRowCount; r++) {
206
+ subArray.push(lastElem);
207
+ }
208
+ }
209
+
210
+ let columnHeaderMergeCellArray = [];
211
+ let nextColIndex = 1;
212
+ for (let i = 0, count = gridColumns.length; i < count; i++) {
213
+ let gridColumn = gridColumns[i];
214
+ nextColIndex = this.gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, 1, nextColIndex);
215
+ }
216
+
217
+ let rowHeaderArray = [];
218
+ for (let totalColumnHeaderRowStep = 0; totalColumnHeaderRowStep < totalColumnHeaderRowCount; totalColumnHeaderRowStep++) {
219
+ let rowHeaderColumns = [];
220
+ columnHeaderArray.forEach(columns => {
221
+ rowHeaderColumns.push(columns[totalColumnHeaderRowStep])
222
+ })
223
+ rowHeaderArray.push(rowHeaderColumns);
224
+ }
225
+
226
+ let columnHeaderData = [];
227
+ let columnIndex = 0;
228
+
229
+ let addColumns = (columns, result) => {
230
+ columns.forEach((column) => {
231
+ let exporterColumn = this.$normalizedExportColumn(column);
232
+ result.push(exporterColumn);
233
+ if (column.children && column.children.length > 1) {
234
+ let children = exporterColumn.columns = [];
235
+ addColumns(column.children, children);
236
+ } else {
237
+ columnIndex++;
238
+ }
239
+ });
240
+ };
241
+ addColumns(gridColumns, columnHeaderData);
242
+ const autoCreateArrayItems = function (column, columnStep, columns) {
243
+ if (column.columns && column.columns.length) {
244
+ columns.splice(columnStep, 1, ...column.columns);
245
+ }
246
+
247
+ if (columnStep === columns.length - 1) {
248
+ console.log('success');
249
+ } else {
250
+ columnStep++;
251
+ autoCreateArrayItems(columns[columnStep], columnStep, columns);
252
+ }
253
+ }
254
+
255
+ autoCreateArrayItems(columnHeaderData[0], 0, columnHeaderData);
256
+
257
+ return {
258
+ totalColumnCount,
259
+ totalColumnHeaderRowCount,
260
+ columnHeaderArray,
261
+ columnHeaderData,
262
+ rowHeaderArray,
263
+ columnHeaderMergeCellArray
264
+ };
265
+ },
266
+
267
+ /**
268
+ * @return {Array} eg -
269
+ * [
270
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
271
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
272
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
273
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
274
+ * ]
275
+ */
276
+ gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, currentColumnHeaderRowIndex = 1, colIndex) {
277
+
278
+ let allChildColumnCount = this.$countAllChildColumnCount(gridColumn);
279
+
280
+ if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
281
+ let gridColumns = gridColumn.children;
282
+ let gridColumnCount = gridColumns.length;
283
+
284
+ columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, currentColumnHeaderRowIndex, colIndex + allChildColumnCount - 1]);
285
+
286
+ for (let i = 0; i < gridColumnCount; i++) {
287
+ this.gatherExcelColumnHeaderMergeCellInfo(gridColumns[i], columnHeaderMergeCellArray, totalColumnHeaderRowCount,
288
+ currentColumnHeaderRowIndex + 1, colIndex + i);
289
+ }
290
+ } else {
291
+ if (currentColumnHeaderRowIndex !== totalColumnHeaderRowCount) {
292
+ columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, totalColumnHeaderRowCount, colIndex]);
293
+ }
294
+ }
295
+
296
+ return colIndex + allChildColumnCount;
297
+ },
298
+
299
+ $countAllChildColumnCount(gridColumn) {
300
+ let columnCount = 0;
301
+ if (gridColumn === null || gridColumn === void 0) {
302
+ return columnCount;
303
+ }
304
+
305
+ if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
306
+ let gridColumns = gridColumn.children;
307
+ let gridColumnCount = gridColumns.length;
308
+
309
+ for (let i = 0; i < gridColumnCount; i++) {
310
+ let childColumnCount = this.$countAllChildColumnCount(gridColumns[i]);
311
+ columnCount = columnCount + childColumnCount;
312
+ }
313
+ } else {
314
+ columnCount = 1;
315
+ }
316
+
317
+ return columnCount;
318
+ },
319
+
320
+ getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, totalColumnCount, totalColumnHeaderCount) {
321
+ let maxN2 = totalColumnHeaderCount;
322
+ for (let i = 0, count = gridColumns.length; i < count; i++) {
323
+ let column = gridColumns[i];
324
+ if (Array.isArray(column.children) && column.children.length) {
325
+ let [n1, n2] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(column.children, 0, totalColumnHeaderCount + 1);
326
+ totalColumnCount = totalColumnCount + n1;
327
+ if (n2 > maxN2) {
328
+ maxN2 = n2;
329
+ }
330
+ } else {
331
+ totalColumnCount = totalColumnCount + 1;
332
+ }
333
+ }
334
+
335
+ return [totalColumnCount, maxN2]
336
+ },
337
+
338
+ $buildColumnHeaderArray(column, columnHeaderArray, parentHeaderTitles) {
339
+ if (Array.isArray(column.children) && column.children.length) {
340
+ if (parentHeaderTitles === null || parentHeaderTitles == void 0) {
341
+ parentHeaderTitles = [column.title];
342
+ } else {
343
+ parentHeaderTitles.push(column.title);
344
+ }
345
+
346
+ for (let i = 0, count = column.children.length; i < count; i++) {
347
+ let childColumn = column.children[i];
348
+ this.$buildColumnHeaderArray(childColumn, columnHeaderArray, parentHeaderTitles);
349
+ }
350
+ } else {
351
+ let columnSelfHeaderTitleArray = [];
352
+ if (Array.isArray(parentHeaderTitles)) {
353
+ columnSelfHeaderTitleArray.push(...parentHeaderTitles);
354
+ }
355
+
356
+ columnSelfHeaderTitleArray.push(column.title);
357
+
358
+ columnHeaderArray.push(columnSelfHeaderTitleArray)
359
+ }
360
+ },
361
+
362
+
363
+ $normalizedExportColumn(column) {
364
+ return {
365
+ header : column.title,
366
+ key : column.dataIndex,
367
+ locked : column.locked,
368
+ width : column.width != null ? column.width * 0.1 : undefined
369
+ }
370
+ }
371
+ }
372
+
373
+ export default ExcelKits;