要对行进行排序,您需要:
将所有行复制到temp
在temp中排序行
从工作表中删除所有行
使用temp中已排序行的值创建新行
public static void sortSheet(Workbook workbook, Sheet sheet) {
List rows = Lists.newArrayList(sheet.rowIterator());
rows.sort(Comparator.comparing(cells -> cells.getCell(0).getStringCellValue()));
removeAllRows(sheet);
for (int i = 0; i < rows.size(); i++) {
Row newRow = sheet.createRow(i);
Row sourceRow = rows.get(i);
// Loop through source columns to add to new row
for (int j = 0; j < sourceRow.getLastCellNum(); j++) {
// Grab a copy of the old/new cell
Cell oldCell = sourceRow.getCell(j);
Cell newCell = newRow.createCell(j);
// If the old cell is null jump to next cell
if (oldCell == null) {
newCell = null;</