List of usage examples for org.apache.poi.ss.usermodel CellValue toString
public String toString()
From source file:nl.detoren.ijsco.io.ExcelExport.java
License:Open Source License
/** * Sorts (A-Z) rows by String column/*from w w w . j a v a 2 s . co m*/ * @param sheet - sheet to sort * @param column - String column to sort by * @param rowStart - sorting from this row down */ private void sortSheet(XSSFSheet sheet, int column, int rowStart, int rowEnd) { try { FormulaEvaluator evaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator(); logger.log(Level.INFO, "sorting sheet: " + sheet.getSheetName()); 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 (rowEnd == row.getRowNum()) break; Row row2 = sheet.getRow(row.getRowNum() + 1); if (row2 == null) continue; int rownum1 = row.getRowNum(); int rownum2 = row2.getRowNum(); CellValue firstValue; CellValue secondValue; firstValue = evaluator.evaluate(row.getCell(column)); secondValue = evaluator.evaluate(row2.getCell(column)); //compare cell from current row and next row - and switch if secondValue should be before first if (secondValue.toString().compareToIgnoreCase(firstValue.toString()) < 0) { logger.log(Level.INFO, "Shifting rows" + sheet.getSheetName() + rownum1 + " - " + rownum2); sheet.shiftRows(row2.getRowNum(), row2.getRowNum(), -1); logger.log(Level.INFO, "Shifting rows" + sheet.getSheetName() + rownum1 + " - " + rownum2); sheet.shiftRows(row.getRowNum(), row.getRowNum(), 1); sorting = true; } } } } catch (Exception ex) { logger.log(Level.WARNING, "Failing Shifting rows" + sheet.getSheetName() + "Error " + ex.getMessage()); } }