Reputation: 53
I am writing java code to read excel file having column name "Key" and "Value". But getting error as unable to cast at this line of code,
Row firstRow =(Row)sheet.getRow(0);
How to resolve this error please help me.
public static void main(String[] args) throws Exception {
FileInputStream fileIn = new FileInputStream("c://param_2003.xls");
POIFSFileSystem fs = new POIFSFileSystem(fileIn);
HSSFWorkbook filename = new HSSFWorkbook(fs);
HSSFSheet sheet = filename.getSheetAt(0);
String column1 = "Key";
String column2 = "Value";
Integer columnNo1 = null;
Integer columnNo2 = null;
List<Cell> cells = new ArrayList<Cell>();
Row firstRow =(Row)sheet.getRow(0);
for(org.apache.poi.ss.usermodel.Cell cell:firstRow){
if (cell.getStringCellValue().equals(column1)){
columnNo1 = cell.getColumnIndex();
}
}
for(org.apache.poi.ss.usermodel.Cell cell:firstRow){
if (cell.getStringCellValue().equals(column2)){
columnNo2 = cell.getColumnIndex();
}
}
System.out.println(columnNo1);
System.out.println(columnNo2);
}
Upvotes: 3
Views: 12914
Reputation: 48376
You've got mis-matched Apache POI jars on your classpath. You need to ensure that all of your Apache POI jars are from the same version, things won't work if you combine old an new jars.
To see what jars you need, take a look at the Apache POI Components page which lists them and their dependencies
To work out what jars you're actually using (which may not be what you think you're using), see the code snippet in the first Apache POI FAQ entry
Once you have a consistent set of POI jars (which as of writing would be based off of 3.10), it should be fine
Upvotes: 4