シートの行を文字列列の1つで並べ替えたいのですが。Sheet.shiftRowsメソッドを使用してそれを達成しようとしましたが、それでは管理できません。私のメソッドでは行の位置が切り替わりません。私のコードの何が問題になっていますか?または、Excelの任意の文字列列で行を並べ替えるより良い方法がありますか?
/**
* Sorts (A-Z) rows by String column
* @param sheet - sheet to sort
* @param column - String column to sort by
* @param rowStart - sorting from this row down
*/
private void sortSheet(Sheet sheet, int column, int rowStart) {
boolean sorting = true;
int lastRow = sheet.getLastRowNum();
while (sorting == true) {
sorting = false;
for (Row row : sheet) {
// skip if this row is before first to sort
if (row.getRowNum()<rowStart) continue;
// end if this is last row
if (lastRow==row.getRowNum()) break;
Row row2 = sheet.getRow(row.getRowNum()+1);
if (row2 == null) continue;
String firstValue = (row.getCell(column) != null) ? row.getCell(column).getStringCellValue() : "";
String secondValue = (row2.getCell(column) != null) ? row2.getCell(column).getStringCellValue() : "";
//compare cell from current row and next row - and switch if secondValue should be before first
if (secondValue.compareToIgnoreCase(firstValue)<0) {
sheet.shiftRows(row2.getRowNum(), row2.getRowNum(), -1);
sheet.shiftRows(row.getRowNum(), row.getRowNum(), 1);
sorting = true;
}
}
}
}
シートの行の並べ替えを管理する方法はありますか?
更新上記の方法は、Apache-POI3.9バージョン以降で機能します。
編集:不足しているブラケット-helvioを追加しました