简体   繁体   中英

Reading Excel file using poi and storing in multi-dimensional array doesn't bring all the data in Java

Hello I am trying to bring data from excel to multi-dimensional array by looping, it brings the data except for the last row. What could be done to fix it please?

Thanks.

Here is my code:

    String filePath = "C:\\testXL.xlsx";

    FileInputStream fis = new FileInputStream(filePath);
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet ws = wb.getSheet("Sheet1");


    int rows = ws.getLastRowNum(); //will give us how many rows until the last row
    int cols = ws.getRow(0).getLastCellNum();

    String[][] rowCol = new String[rows][cols];

    for (int r = 0; r < rows; r++){

        XSSFRow myRow = ws.getRow(r);

        for (int c = 0; c<cols; c++){

            String value = myRow.getCell(c).toString();

            rowCol[r][c] = value;

            System.out.print(rowCol[r][c]+ "\t");

        }
        System.out.println();
    }

it prints upto 4th row and doesn't print the last row

This is the excel file:

擅长

Edit:

int rows = ws.getLastRowNum()+1;

This seems to solve the issue, if there is any other better way to solve it please let me know.

Thanks

int rows = ws.getLastRowNum()+1;

This seems to solve the issue, if there is any other better way to solve it please let me know.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM