Rachel
Rachel

Reputation: 103397

Issue with Apache Poi while Parsing excel in java?

I am using Apache POI to parse excel file and to get values back am doing by row.getCell(5).getNumericCellValue() assuming that cell(5) on that row would always be number.

Is it possible to intelligently parse file so that even if order of columns is changed or jumbled even then we can look for values based on columnName so that contract year always comes from the column labelled Contract Year - regardless of whether that column is the 6th or 7th column?

Upvotes: 0

Views: 348

Answers (1)

Francis P
Francis P

Reputation: 13655

You could implement a function called getContractYearColumnIndex() which returns the index you need.

private int getContractYearColumnIndex(){
    final int maxColIndex = 25;
    int colIndex = 0;
    HSSFRow titleRow = yourSheet.getRow(0);
    String value = titleRow.getCell(colIndex).getStringCellValue();

    while(value != "Contract Year" && colIndex < maxColIndex){
        colIndex = colIndex + 1;
        value = titleRow.getCell(colIndex).getStringCellValue();
    }
    return colIndex;
}

Of course, this implementation supposes the title you're looking for is in the first row (index 0).

Upvotes: 1

Related Questions