Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ApachePOI从excel文件中的特定位置获取结果_Java_Excel_Apache_Apache Poi - Fatal编程技术网

Java 如何使用ApachePOI从excel文件中的特定位置获取结果

Java 如何使用ApachePOI从excel文件中的特定位置获取结果,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我有一个excel文件,其中包含各种信息,还包含一个表结构,该表结构有两个标题单元代码和说明。因此,现在我可以使用POI读取整个excel文件,但我的问题是,我只需要读取excel表中冒号的两个值。但我无法正确执行此操作,这是我迄今为止所做的代码 public class ReadExcelFileToList { public static void main(String[] args) throws IOException { InputStream input = null;

我有一个excel文件,其中包含各种信息,还包含一个表结构,该表结构有两个标题单元代码和说明。因此,现在我可以使用POI读取整个excel文件,但我的问题是,我只需要读取excel表中冒号的两个值。但我无法正确执行此操作,这是我迄今为止所做的代码

public class ReadExcelFileToList {

public static void main(String[] args) throws IOException {

    InputStream input = null;

    try {

        input = new FileInputStream(
                "C:\\Users\\jeet.chatterjee\\Downloads\\unitUploadFormat.xlsx");

        System.out.println("file is found");

        XSSFWorkbook wb = new XSSFWorkbook(input);
        XSSFSheet sheet = wb.getSheetAt(0);
        int rowCount = 0;
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            System.out.println("\n");
            if (rowCount == 3) {
                Iterator cells = row.cellIterator();

                while (cells.hasNext()) {

                    XSSFCell cell = (XSSFCell) cells.next();
                    if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                        System.out.print(cell.getStringCellValue()
                                + "     ");

                    /*
                     * else if (XSSFCell.CELL_TYPE_STRING ==
                     * cell.getCellType())
                     * System.out.print(cell.getStringCellValue() +
                     * "     "); else if (XSSFCell.CELL_TYPE_STRING ==
                     * cell.getCellType())
                     * System.out.print(cell.getStringCellValue() +
                     * "     ");
                     */

                    else
                        System.out.print("Unknown cell type");

                }
                rowCount++;
            }
        }

    } catch (IOException ex) {

        ex.printStackTrace();
    } finally {
        input.close();
    }

}

}

我正在使用sheet.getLastRowNum获得我想要的结果,但我没有得到它。请有人帮助。。这是我的excel文件,我只想获取kg和kg值等等,所有将放入其中的项目都将被提取。

代码中的错误由getLastRowNum的API解释

返回:此工作表中包含的最后一行基于0

所以你想要

if(rowCount == 4){
当然,如果有空行,结果也可能不是您想要的

编辑

为什么这么难呢

使用


就像可怕的袋熊说的,使用您自己的行计数器。

使用行迭代器:行迭代器我已经在使用了,那么您得到了什么?请插入第5行的数据,仅使用单位代码和单位列引用\u RFQ\u状态单位代码说明KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG KG建议
XSSFRow row = sheet.getRow(4);
XSSFCell cell = row.getCell(1)
 while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            if(rowCount==3){
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {
                XSSFCell cell = (XSSFCell) cells.next();
                    System.out.print(cell.getStringCellValue()+"\t");
                }
            }
            System.out.println();
            rowCount++;
        }