https://bz.apache.org/bugzilla/show_bug.cgi?id=58221
Bug ID: 58221
Summary: ShiftRows negative Shift
Product: POI
Version: 3.13-dev
Hardware: PC
Status: NEW
Severity: major
Priority: P2
Component: XSSF
Assignee: [email protected]
Reporter: [email protected]
Created attachment 32977
--> https://bz.apache.org/bugzilla/attachment.cgi?id=32977&action=edit
Shifting Rows Stacktrace
HI,
I was trying to find a way to sort data in Apache poi. I found this solution on
Stack but it's not working for it. It seems like the sheet index isn't counting
right and it can't find the original row after the shift. I've basically just
comparing letters.
I've attached an text document with the stacktrace.
Here is the code I'm trying to sort the rows.
/**
* 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;
}
}
}
}
Any suggestions would be greatly appreciate! Not being able to sort is killing
me.
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]