The actual number of sheet rows when POI parses an excel file.

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.