我想问一下这个导出怎么设置导出后他的单元格宽度自适应,烦了我好多天了
1条回答 默认 最新
- been_ss 2021-10-22 17:58关注
哈喽,看看这个,有用请点采纳哦~
export function export_json_to_excel({ multiHeader = [], header, data, filename, merges = [], autoWidth = true, bookType = 'xlsx' } = {}) { /* original data */ filename = filename || 'excel-list' data = [...data] data.unshift(header); for (let i = multiHeader.length - 1; i > -1; i--) { data.unshift(multiHeader[i]) } var ws_name = "SheetJS"; var wb = new Workbook(), ws = sheet_from_array_of_arrays(data); if (merges.length > 0) { if (!ws['!merges']) ws['!merges'] = []; merges.forEach(item => { ws['!merges'].push(XLSX.utils.decode_range(item)) }) } if (autoWidth) { /*设置worksheet每列的最大宽度*/ const colWidth = data.map(row => row.map(val => { /*先判断是否为null/undefined*/ if (val == null) { return { 'wch': 10 }; } /*再判断是否为中文*/ else if (val.toString().charCodeAt(0) > 255) { return { 'wch': val.toString().length * 2 }; } else { return { 'wch': val.toString().length }; } })) /*以第一行为初始值*/ let result = colWidth[0]; for (let i = 1; i < colWidth.length; i++) { for (let j = 0; j < colWidth[i].length; j++) { if (result[j]['wch'] < colWidth[i][j]['wch']) { result[j]['wch'] = colWidth[i][j]['wch']; } } } ws['!cols'] = result; } /* add worksheet to workbook */ wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; var wbout = XLSX.write(wb, { bookType: bookType, bookSST: false, type: 'binary' }); saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), `${filename}.${bookType}`); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用 1
悬赏问题
- ¥15 openpcdet自制数据集评估bev精度和3d精度相同
- ¥15 excel 上下按钮 显示行
- ¥20 云卓h12pro 数传问题
- ¥20 请问有人知道怎么用工艺库里面的sdb文件通过virtuoso导出来library里面每个cell的symbol吗?
- ¥20 海思 nnie 编译 报错
- ¥50 决策面并仿真,要求有仿真结果图
- ¥15 关于路由器的路由协议配置
- ¥15 springboot接入微信支付SDK
- ¥50 大区域的遥感影像匹配 怎么做啊
- ¥15 求解答:pytorch跑yolov8神经网络受挫