java apache poi(第 2 部分) [英] java apache poi (part 2)

查看:34
本文介绍了java apache poi(第 2 部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续.java apache poi(第 1 部分)

  • 代码

    <预><代码>...while(rowIterator.hasNext()){列表<字符串>record = new ArrayList();row = (XSSFRow)rowIterator.next();迭代器<Cell>cellIterator = row.cellIterator();while(cellIterator.hasNext()){cell = cellIterator.next();cell.setCellType(Cell.CELL_TYPE_STRING);开关(cell.getCellType()){案例 Cell.CELL_TYPE_STRING:record.add(cell.getStringCellValue());休息;案例 Cell.CELL_TYPE_NUMERIC:record.add(Double.toString(cell.getNumericCellValue()));休息;}}读取文件();}公共无效读文件(){字符串 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) {继续;}列表<字符串>record = new ArrayList();迭代器<Cell>cellIterator = row.cellIterator();...读取文件();}

cont. on java apache poi (part 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
    1
    2
    3

  • My expected output should like this:
    1
    2
    3

  • My question is how to remove the first row from excel (ID) from the above code?

解决方案

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();
}

这篇关于java apache poi(第 2 部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆