Я предлагаю вам взглянуть на Cell
getStringCellValue()
документы java:
/**
* Get the value of the cell as a string
* <p>
* For numeric cells we throw an exception. For blank cells we return an empty string.
* For formulaCells that are not string Formulas, we throw an exception.
* </p>
* @return the value of the cell as a string
*/
String getStringCellValue();
Возможно, CellType
других ячеек в строке 2 не относится к типу STRING
.Вы можете попробовать написать вспомогательный метод следующим образом (работает с poi 3.16):
private static String myCellStringValue(Cell cell, CellType cellType) {
String data = EMPTY;
try {
switch (cellType) {
case STRING:
data = cell.getRichStringCellValue().toString();
break;
case NUMERIC:
data = String.valueOf(cell.getNumericCellValue());
break;
case BOOLEAN:
data = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA:
data = myCellStringValue(cell, cell.getCachedFormulaResultTypeEnum());
break;
case BLANK:
case ERROR:
case _NONE:
break;
}
} catch (Exception e) {
//your catch clause
}
return data;
}
И затем вызвать вспомогательный метод в вашем цикле:
for (int j = 0; j < no_of_columns; j++) {
Cell cell = sh1.getRow(i).getCell(j);
System.out.print(" " + myCellStringValue(cell, cell.getCellTypeEnum()) + " ");
}