Я решил свою проблему, мне нужно было создать метод:
public void printSheetAsString(int sheetIndex){
sheet = workbook.getSheetAt(sheetIndex);
Row row;
//a row iterator which iterates through rows in a specified sheet
for(Iterator<Row> rowIterator = sheet.rowIterator(); rowIterator.hasNext();){
row = rowIterator.next(); //store the next element into the variable "row"
//a cell iterator which iterates through cells in the sheet's rows
for(Iterator<Cell> cellIterator = row.cellIterator(); cellIterator.hasNext();){
cell = cellIterator.next(); //store the next element into the variable "cell"
cell.setCellType(CellType.STRING); //set all the cells to type string so that we can print them out easily
//print "%" for place holder/variable, "-" for left justified table, "30" for spaces between each column, "s" for string
//this will print the cell then move to the next cell, print that cell, so on, till we reach the next row, which repeats the process, them move to next row, and so on till rowIterator.hasNext() returns false
System.out.printf("%-30s", cell.getStringCellValue());
}
System.out.println();
}
}
Где акцент:
//print "%" for place holder/variable, "-" for left justified table, "30" for spaces between each column, "s" for string
//this will print the cell then move to the next cell, print that cell, so on, till we reach the next row, which repeats the process, them move to next row, and so on till rowIterator.hasNext() returns false
System.out.printf("%-30s", cell.getStringCellValue());
Это, наконец, печатает вывод:
Затем я создал другой метод, который правильно печатает переменные вместо преобразования их в строку и использования устаревших методов:
public void printSheet(int sheetIndex){
sheet = workbook.getSheetAt(sheetIndex);
Row row;
for(Iterator<Row> rowIterator = sheet.rowIterator(); rowIterator.hasNext();){
row = rowIterator.next();
for(Iterator<Cell> cellIterator = row.cellIterator(); cellIterator.hasNext();){
cell = cellIterator.next();
switch (cell.getCellType()) {
case STRING:
System.out.printf("%-30s", cell.getStringCellValue());
break;
case NUMERIC:
if(DateUtil.isCellDateFormatted(cell))
System.out.printf("%-30s", cell.getDateCellValue().toString());
System.out.printf("%-30f", cell.getNumericCellValue());
break;
case BOOLEAN:
System.out.printf("%-30b", cell.getBooleanCellValue());
break;
case FORMULA:
System.out.printf("%-30s", cell.getCellFormula());
break;
case ERROR:
System.out.printf("%-30x", cell.getErrorCellValue());
break;
default:
System.err.print("[ERROR]: Unidentified Cell Value: " + cell.getCellType());
}
}
System.out.println();
}
}
}
Это также дает тот же результат.
Спасибо!