这不是我的答案(我没有测试过),我在以下链接中找到了一个:
https://github.com/guyonroche/exceljs/issues/363#issuecomment-435692059
const sortByColumn = columnNum => {
if (worksheet) {
columnNum--;
const sortFunction = (a, b) => {
if (a[columnNum] === b[columnNum]) {
return 0;
}
else {
return (a[columnNum] < b[columnNum]) ? -1 : 1;
}
}
let rows = [];
for (let i = 1; i <= worksheet.actualRowCount; i++) {
let row = [];
for (let j = 1; j <= worksheet.columnCount; j++) {
row.push(worksheet.getRow(i).getCell(j).value);
}
rows.push(row);
}
rows.sort(sortFunction);
// Remove all rows from worksheet then add all back in sorted order
worksheet.spliceRows(1, worksheet.actualRowCount);
// Note worksheet.addRows() may add them to the end of empty rows so loop through and add to beginnning
for (let i = rows.length; i >= 0; i--) {
worksheet.spliceRows(1, 0, rows[i]);
}
}
}