How to obtain the actual number of rows when Apache POI parses excel
String filePath = "C: // test.xls ";
HSSFWorkbook workbook = null;
Try {
Workbook = new HSSFWorkbook (new POIFSFileSystem (
New FileInputStream (filePath )));
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
HSSFSheet sheetLi = workbook. getSheet ("Li ");
System. out. println (sheetLi. getLastRowNum (); Result 1
System. out. println (sheetLi. getFirstRowNum (); Result 2
System. out. println (sheetLi. getPhysicalNumberOfRows (); Result 3
1. Result 3 is always better than result 1.
The index of the number of rows starts from 0. Result 2 shows the actual number displayed on the left of excel.
2. The number of rows here is not the actual number of rows with data
When you create a new sheet without adding any content, the returned sheetLi is null.
Similarly, row and cell are null.
However, when the data is inserted and the content is cleared, the printed result does not change. row and cell are still objects and no longer null.
Then, when you delete all rows with existing data, everything will be restored to the original state and the newly created results will be null.
Therefore, you have to determine the number of rows with data.
3 cell type
The cell format set in excel only represents the style in excel.
POI's own cellType
HSSFCell. CELL_TYPE_NUMERIC the cell format is Numeric to get the number and date value. If you set the decimal point to 2, for example, if the input is 1, the excel display is 1.00, but the actual getNumericCellValue () 1.0 is returned.
The format of HSSFCell. CELL_TYPE_STRING cells is text. Only getStringCellValue () can be used ()
The cell format is regular. If you enter text, you can only get text. If you enter a number, you can get the value of the number and date. The character is also invalid. For example, if you enter 1, the resulting number is 1.0.
To obtain other types, you must convert them by yourself.