Skip to content

Commit a8602e7

Browse files
spiritreePanJiaChen
authored andcommitted
refine:export adaptive colwidth in excel
1 parent 516897b commit a8602e7

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/vendor/Export2Excel.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export function export_table_to_excel(id) {
100100
/* original data */
101101
var data = oo[0];
102102
var ws_name = "SheetJS";
103-
console.log(data);
104103

105104
var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);
106105

@@ -127,6 +126,25 @@ export function export_json_to_excel(th, jsonData, defaultTitle) {
127126

128127
var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);
129128

129+
/*设置worksheet每列的最大宽度*/
130+
const colWidth = data.map(row => row.map(val => {
131+
/*判断是否为中文*/
132+
if (val.toString().charCodeAt(0) > 255) {
133+
return {'wch': val.toString().length * 2};
134+
} else {
135+
return {'wch': val.toString().length};
136+
}
137+
}))
138+
/*以第一行为初始值*/
139+
let result = colWidth[0];
140+
for (let i = 1; i < colWidth.length; i++) {
141+
for (let j = 0; j < colWidth[i].length; j++) {
142+
if (result[j]['wch'] < colWidth[i][j]['wch']) {
143+
result[j]['wch'] = colWidth[i][j]['wch'];
144+
}
145+
}
146+
}
147+
ws['!cols'] = result;
130148

131149
/* add worksheet to workbook */
132150
wb.SheetNames.push(ws_name);

0 commit comments

Comments
 (0)