Search code examples
javajxl

How to read from an Excel file columns with different number of rows?


I have this method which adds in an ArrayList named column an entire column from am Excel file. But i have a problem if a column is shorter( it doesn't have as many rows as others)empty String error appears.

 public initializingWorkbook( Sheet sheet,ArrayList <Double> column, int index) 
   {
     int rows = sheet.getRows();
     for(int row = 1;row < rows;row++)

        {
            String i = sheet.getCell(index, row).getContents();
            column.add(Double.parseDouble(i));
        }
   }

Here is the sheet. So first 6 columns have the same number of rows and the test column fewer. Cells format are on General.


Solution

  • You have to check first, how much columns the actual row contains:

    {
    if (index < sheet.getRow(row).length) {
        String i = sheet.getCell(index, row).getContents();
        column.add(Double.parseDouble(i));
        }
    }
    

    sheet.getRow(int row) returns all cells of this row; that is an array of cells. So you can check the dimension of the returned array.