|
@@ -182,6 +182,7 @@ export function export_json_to_excel({
|
182
|
182
|
if (val == null) {
|
183
|
183
|
return {
|
184
|
184
|
'wch': 10
|
|
185
|
+ // 'wch': 1
|
185
|
186
|
};
|
186
|
187
|
}
|
187
|
188
|
/*再判断是否为中文*/
|
|
@@ -199,9 +200,12 @@ export function export_json_to_excel({
|
199
|
200
|
let result = colWidth[0];
|
200
|
201
|
for (let i = 1; i < colWidth.length; i++) {
|
201
|
202
|
for (let j = 0; j < colWidth[i].length; j++) {
|
202
|
|
- if (result[j]['wch'] < colWidth[i][j]['wch']) {
|
203
|
|
- result[j]['wch'] = colWidth[i][j]['wch'];
|
204
|
|
- }
|
|
203
|
+ // console.log('7777',result[j]['wch'] ,colWidth[i][j]['wch']);
|
|
204
|
+ // if(colWidth[i][j]['wch']!=''){
|
|
205
|
+ if (result[j]['wch'] < colWidth[i][j]['wch']) {
|
|
206
|
+ result[j]['wch'] = colWidth[i][j]['wch'];
|
|
207
|
+ }
|
|
208
|
+ // }
|
205
|
209
|
}
|
206
|
210
|
}
|
207
|
211
|
ws['!cols'] = result;
|
|
@@ -387,3 +391,83 @@ export function export_json_to_excel2({
|
387
|
391
|
type: "application/octet-stream"
|
388
|
392
|
}), `${filename}.${bookType}`);
|
389
|
393
|
}
|
|
394
|
+
|
|
395
|
+export function export_json_to_excel3({
|
|
396
|
+ multiHeader = [],
|
|
397
|
+ header,
|
|
398
|
+ data,
|
|
399
|
+ filename,
|
|
400
|
+ merges = [],
|
|
401
|
+ autoWidth = true,
|
|
402
|
+ bookType = 'xlsx'
|
|
403
|
+} = {}) {
|
|
404
|
+ /* original data */
|
|
405
|
+ filename = filename || 'excel-list'
|
|
406
|
+ data = [...data]
|
|
407
|
+ data.unshift(header);
|
|
408
|
+ console.log('wwwww',data);
|
|
409
|
+ for (let i = multiHeader.length - 1; i > -1; i--) {
|
|
410
|
+ data.unshift(multiHeader[i])
|
|
411
|
+ }
|
|
412
|
+ console.log('qqqqq',data);
|
|
413
|
+ var ws_name = "SheetJS";
|
|
414
|
+ var wb = new Workbook(),
|
|
415
|
+ ws = sheet_from_array_of_arrays(data);
|
|
416
|
+
|
|
417
|
+ if (merges.length > 0) {
|
|
418
|
+ if (!ws['!merges']) ws['!merges'] = [];
|
|
419
|
+ merges.forEach(item => {
|
|
420
|
+ console.log(XLSX.utils.decode_range(item))
|
|
421
|
+ ws['!merges'].push(XLSX.utils.decode_range(item))
|
|
422
|
+ })
|
|
423
|
+ }
|
|
424
|
+
|
|
425
|
+ if (autoWidth) {
|
|
426
|
+ /*设置worksheet每列的最大宽度*/
|
|
427
|
+ const colWidth = data.map(row => row.map(val => {
|
|
428
|
+ /*先判断是否为null/undefined*/
|
|
429
|
+ if (val == null) {
|
|
430
|
+ return {
|
|
431
|
+ 'wch': 10
|
|
432
|
+ // 'wch': 1
|
|
433
|
+ };
|
|
434
|
+ }
|
|
435
|
+ /*再判断是否为中文*/
|
|
436
|
+ else if (val.toString().charCodeAt(0) > 255) {
|
|
437
|
+ return {
|
|
438
|
+ 'wch': val.toString().length * 2
|
|
439
|
+ };
|
|
440
|
+ } else {
|
|
441
|
+ return {
|
|
442
|
+ 'wch': val.toString().length
|
|
443
|
+ };
|
|
444
|
+ }
|
|
445
|
+ }))
|
|
446
|
+ /*以第一行为初始值*/
|
|
447
|
+ let result = colWidth[0];
|
|
448
|
+ for (let i = 1; i < colWidth.length; i++) {
|
|
449
|
+ for (let j = 0; j < colWidth[i].length; j++) {
|
|
450
|
+ // console.log('7777',result[j]['wch'] ,colWidth[i][j]['wch']);
|
|
451
|
+ if(colWidth[i][j]['wch']!=''){
|
|
452
|
+ if (result[j]['wch'] < colWidth[i][j]['wch']) {
|
|
453
|
+ result[j]['wch'] = colWidth[i][j]['wch'];
|
|
454
|
+ }
|
|
455
|
+ }
|
|
456
|
+ }
|
|
457
|
+ }
|
|
458
|
+ ws['!cols'] = result;
|
|
459
|
+ }
|
|
460
|
+
|
|
461
|
+ /* add worksheet to workbook */
|
|
462
|
+ wb.SheetNames.push(ws_name);
|
|
463
|
+ wb.Sheets[ws_name] = ws;
|
|
464
|
+
|
|
465
|
+ var wbout = XLSX.write(wb, {
|
|
466
|
+ bookType: bookType,
|
|
467
|
+ bookSST: false,
|
|
468
|
+ type: 'binary'
|
|
469
|
+ });
|
|
470
|
+ saveAs(new Blob([s2ab(wbout)], {
|
|
471
|
+ type: "application/octet-stream"
|
|
472
|
+ }), `${filename}.${bookType}`);
|
|
473
|
+}
|