我的excel文件有4行2列
具有第3列第1列作为空白值的连续值,如下所示
phone plan
------------------
85366667777 PLATINUM
85367890000 GOLD
85362524232 SILVER
null SILVER
85362522436 DIAMOND
public List<List<String>> readExcelFile(String fileName) {
try {
FileInputStream fileInputStream = new FileInputStream(fileName);
POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
HSSFSheet hssfSheet = workBook.getSheetAt(0);
Iterator<Row> rowIterator = hssfSheet.rowIterator();
boolean headerRow = true;
while (rowIterator.hasNext()) {
if (headerRow == false) {
String Rows = util.loadfile("config/ConfigValue.properties","NoOfRowsToSkip");
int i = Integer.parseInt(Rows);
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
if(hssfRow.getRowNum()==0||hssfRow.getRowNum()<=i){
continue; //just skip the rows if row number is 0 TO i
}
Iterator<Cell> iterator = hssfRow.cellIterator();
List cellTempList = new ArrayList();
while (iterator.hasNext()) {
HSSFCell hssfCell = (HSSFCell) iterator.next();
if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC)
{DecimalFormat df = new DecimalFormat("#");
String cellValue=df.format(hssfCell.getNumericCellValue());
cellTempList.add(cellValue.toString());
}
else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_STRING)
{
cellTempList.add(hssfCell.toString());
}
}
cellDataList.add(cellTempList);
} else {
headerRow = false;
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("Other Exception : " + e.getStackTrace());
}
System.out.println(cellDataList);
return cellDataList;
}
}
输出:
[[85366667777, PLATINUM], [85367890000, GOLD], [85362524232, SILVER], [SILVER], [85362522436, DIAMOND]]
当其对应的单元格中没有值时,为什么不打印
[null,Silver]
或[0,SILVER]
? 最佳答案
if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){
DecimalFormat df = new DecimalFormat("#");
String cellValue=df.format(hssfCell.getNumericCellValue());
cellTempList.add(cellValue.toString());
} else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_STRING){
cellTempList.add(hssfCell.toString());
} else {
cellTempList.add(null); // or cellTempList.add("0");
}
您的空单元格既不是字符串也不是数字,因此不会向您的数组添加任何内容。尝试检查CELL_TYPE_BLANK。