Commit 9316d950b14da779d59ea41356727c50e5f6db64

Authored by c37csq
Committed by 蒋琴
1 parent 5abb0f2b

fix: excel export set width (#2206)

src/components/Excel/src/Export2Excel.ts
@@ -6,6 +6,28 @@ const { utils, writeFile } = xlsx; @@ -6,6 +6,28 @@ const { utils, writeFile } = xlsx;
6 6
7 const DEF_FILE_NAME = 'excel-list.xlsx'; 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 export function jsonToSheetXlsx<T = any>({ 31 export function jsonToSheetXlsx<T = any>({
10 data, 32 data,
11 header, 33 header,
@@ -20,7 +42,7 @@ export function jsonToSheetXlsx&lt;T = any&gt;({ @@ -20,7 +42,7 @@ export function jsonToSheetXlsx&lt;T = any&gt;({
20 } 42 }
21 43
22 const worksheet = utils.json_to_sheet(arrData, json2sheetOpts); 44 const worksheet = utils.json_to_sheet(arrData, json2sheetOpts);
23 - 45 + setColumnWidth(arrData, worksheet);
24 /* add worksheet to workbook */ 46 /* add worksheet to workbook */
25 const workbook: WorkBook = { 47 const workbook: WorkBook = {
26 SheetNames: [filename], 48 SheetNames: [filename],