续。 上的Java的Apache POI(部分1)
码
... while(rowIterator.hasNext()){ List<String> record = new ArrayList<String>(); row = (XSSFRow)rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()){ cell = cellIterator.next(); cell.setCellType(Cell.CELL_TYPE_STRING); switch(cell.getCellType()){ case Cell.CELL_TYPE_STRING: record.add(cell.getStringCellValue()); break; case Cell.CELL_TYPE_NUMERIC: record.add(Double.toString (cell.getNumericCellValue())); break; } } readFile(); } public void readFile(){ String ID = record.get(0); System.out.println(ID); } ...
从上面的代码中,我的输出是象下面这样:
ID
1
2
3
我预期的输出应该是这样的:
1
2
3
我的问题是如何从Excel(ID)从上面的代码中删除的第一行?
要跳过第一行:
while(rowIterator.hasNext()){
row = (XSSFRow)rowIterator.next();
if(row.getRowNum()==0) {
continue;
}
List<String> record = new ArrayList<String>();
Iterator<Cell> cellIterator = row.cellIterator();
...
readFile();
}
加入rowIterator.next(); 在节目中虽然环路忽略第一个row.So simple.Hope上面这可以帮助你。
**rowIterator.next();**
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
System.out.println("");
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}