简体   繁体   English

java apache poi(第2部分)

[英]java apache poi (part 2)

cont. 续。 on java apache poi (part 1) 关于java apache poi(第1部分)

  • Code

     ... 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); } ... 
  • From above code, my output is like below: 从上面的代码,我的输出如下:
    ID ID
    1 1
    2 2
    3 3

  • My expected output should like this: 我的预期输出应该是这样的:
    1 1
    2 2
    3 3

  • My question is how to remove the first row from excel (ID) from the above code? 我的问题是如何从上面的代码中删除excel(ID)的第一行?

To skip the first row: 要跳过第一行:

while(rowIterator.hasNext()){

    row = (XSSFRow)rowIterator.next();

    if(row.getRowNum()==0) {
        continue;
    }

    List<String> record = new ArrayList<String>();
    Iterator<Cell> cellIterator = row.cellIterator();
    ...
    readFile();
}

Add rowIterator.next(); 添加rowIterator.next(); above the While loop in the program which ignore the first row.So simple.Hope this helps you. 在忽略第一行的程序中的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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM