Commit 9316d950b14da779d59ea41356727c50e5f6db64
Committed by
蒋琴
1 parent
5abb0f2b
fix: excel export set width (#2206)
Showing
1 changed file
with
23 additions
and
1 deletions
src/components/Excel/src/Export2Excel.ts
... | ... | @@ -6,6 +6,28 @@ const { utils, writeFile } = xlsx; |
6 | 6 | |
7 | 7 | const DEF_FILE_NAME = 'excel-list.xlsx'; |
8 | 8 | |
9 | +/** | |
10 | + * @param data source data | |
11 | + * @param worksheet worksheet object | |
12 | + * @param min min width | |
13 | + */ | |
14 | +function setColumnWidth(data, worksheet, min = 3) { | |
15 | + const obj = {}; | |
16 | + worksheet['!cols'] = []; | |
17 | + data.forEach((item) => { | |
18 | + Object.keys(item).forEach((key) => { | |
19 | + const cur = item[key]; | |
20 | + const length = cur.length; | |
21 | + obj[key] = Math.max(min, length); | |
22 | + }); | |
23 | + }); | |
24 | + Object.keys(obj).forEach((key) => { | |
25 | + worksheet['!cols'].push({ | |
26 | + wch: obj[key], | |
27 | + }); | |
28 | + }); | |
29 | +} | |
30 | + | |
9 | 31 | export function jsonToSheetXlsx<T = any>({ |
10 | 32 | data, |
11 | 33 | header, |
... | ... | @@ -20,7 +42,7 @@ export function jsonToSheetXlsx<T = any>({ |
20 | 42 | } |
21 | 43 | |
22 | 44 | const worksheet = utils.json_to_sheet(arrData, json2sheetOpts); |
23 | - | |
45 | + setColumnWidth(arrData, worksheet); | |
24 | 46 | /* add worksheet to workbook */ |
25 | 47 | const workbook: WorkBook = { |
26 | 48 | SheetNames: [filename], | ... | ... |