Reputation: 2686
I've got a spreadsheet that I'm loading into a Java program using org.apache.poi
. Loading the data is fine, but i'm concerned the columns may be re-arranged at times, which would cause issues with my code. The program expects data to be in a specific column order.
There is a row that contains the column names that I have been tossing away. Is there a way to tell the SpreadSheet
object that I do have a named set of columns that I can index against?
For example, I'd like to load object data like so(psuedo code):
myObject.setName(row.getCell('columnName'));
Upvotes: 0
Views: 1106
Reputation: 5315
Something like this (not tested), if the name of the columns are in row 0 :
public short getCellIdx(Row nameRow, String colName) {
for (short idx = nameRow.getFirstCellNum(); idx <= nameRow.getLastCellNum(); idx++) {
if (rownameRowgetCell(idx).equals(colName))
return idx;
}
}
and then
Row nameRow = sheet.getRow(0);
myObject.setName(row.getCell(getCellIdx(nameRow, "columnName")));
If you have a bigger set of columns, you may better hash the column names to a Hashmap or similar.
Upvotes: 1