javaapache poi(第 2 部分)
java apache poi (part 2)
续。在
代码
...
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);
}
...
根据上面的代码,我的输出如下:
编号
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 的程序中的 While 循环上方,这对您有帮助。
**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();
}
}
续。在
代码
... 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); } ...
根据上面的代码,我的输出如下:
编号
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 的程序中的 While 循环上方,这对您有帮助。
**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();
}
}