【发布时间】:2013-11-30 05:28:22
【问题描述】:
我正在从 Excel 文件中获取数据并将它们存储在数组列表中。除空白单元格外,所有单元格均已正确读取。我想跟踪空白单元格。
例如,我有一个包含 7 列的 Excel 文件,其中 7 列中的 5 个填充了数据,2 个单元格为空,因此将它们存储在 arraylist 中后,我得到的列表大小为 12 而不是 14。
我假设在迭代过程中只考虑填充数据的单元格并忽略空白单元格。
这是我的代码
ArrayList arrayList=new ArrayList();
int rownum=0;
try{
FileInputStream fis=new FileInputStream(new File("D:\\emsAttendancesheet_26-Nov-2013.xls"));
HSSFWorkbook hssfWorkbook=new HSSFWorkbook(fis);
HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(0);
Iterator<Row> rowIterator=hssfSheet.iterator();
while(rowIterator.hasNext()){
Row row=rowIterator.next();
Iterator<Cell> cellIterator=row.iterator();
while(cellIterator.hasNext()){
Cell cell=cellIterator.next();
if(cell.getCellType()==Cell.CELL_TYPE_STRING){
System.out.println(cell.getStringCellValue());
arrayList.add(cell.getStringCellValue());
}
else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
arrayList.add(cell.getNumericCellValue());
System.out.println(cell.getNumericCellValue());
}
}
}
System.out.println("Size of List::"+arrayList.size());
}catch(Exception ex){
System.out.println("Exception in Exel creation"+ex.getMessage());
}
我的 Exel 文件如下所示:
Date EmpId EmpName Status In-Time Out-Time Comment
14-Nov-2013 112 Akas Absent illness
这里 In-Time 和 Out-time 是空白的,所以它没有读取空单元格。我需要考虑空单元格。任何人都可以建议代码吗?
【问题讨论】: