fix: excel export set width (#2206)
This commit is contained in:
parent
5abb0f2b37
commit
9316d950b1
|
|
@ -6,6 +6,28 @@ const { utils, writeFile } = xlsx;
|
||||||
|
|
||||||
const DEF_FILE_NAME = 'excel-list.xlsx';
|
const DEF_FILE_NAME = 'excel-list.xlsx';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param data source data
|
||||||
|
* @param worksheet worksheet object
|
||||||
|
* @param min min width
|
||||||
|
*/
|
||||||
|
function setColumnWidth(data, worksheet, min = 3) {
|
||||||
|
const obj = {};
|
||||||
|
worksheet['!cols'] = [];
|
||||||
|
data.forEach((item) => {
|
||||||
|
Object.keys(item).forEach((key) => {
|
||||||
|
const cur = item[key];
|
||||||
|
const length = cur.length;
|
||||||
|
obj[key] = Math.max(min, length);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
Object.keys(obj).forEach((key) => {
|
||||||
|
worksheet['!cols'].push({
|
||||||
|
wch: obj[key],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function jsonToSheetXlsx<T = any>({
|
export function jsonToSheetXlsx<T = any>({
|
||||||
data,
|
data,
|
||||||
header,
|
header,
|
||||||
|
|
@ -20,7 +42,7 @@ export function jsonToSheetXlsx<T = any>({
|
||||||
}
|
}
|
||||||
|
|
||||||
const worksheet = utils.json_to_sheet(arrData, json2sheetOpts);
|
const worksheet = utils.json_to_sheet(arrData, json2sheetOpts);
|
||||||
|
setColumnWidth(arrData, worksheet);
|
||||||
/* add worksheet to workbook */
|
/* add worksheet to workbook */
|
||||||
const workbook: WorkBook = {
|
const workbook: WorkBook = {
|
||||||
SheetNames: [filename],
|
SheetNames: [filename],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue