poi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject [Bug 58221] New: ShiftRows negative Shift
Date Thu, 06 Aug 2015 21:50:41 GMT

            Bug ID: 58221
           Summary: ShiftRows negative Shift
           Product: POI
           Version: 3.13-dev
          Hardware: PC
            Status: NEW
          Severity: major
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: muckomspam@gmail.com

Created attachment 32977
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=32977&action=edit
Shifting Rows Stacktrace

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

You are receiving this mail because:
You are the assignee for the bug.

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org

View raw message