Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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
JavaPOI:如何找到一个带有字符串值的Excel单元格,并获取其位置(行)以使用该位置查找另一个单元格_Java_Excel_Apache Poi_Poi Hssf - Fatal编程技术网

JavaPOI:如何找到一个带有字符串值的Excel单元格,并获取其位置(行)以使用该位置查找另一个单元格

JavaPOI:如何找到一个带有字符串值的Excel单元格,并获取其位置(行)以使用该位置查找另一个单元格,java,excel,apache-poi,poi-hssf,Java,Excel,Apache Poi,Poi Hssf,我在电子表格中查找字符串为“Total”的单元格,然后使用该单元格所在的行在另一个始终相同的单元格/列(基于0的索引中的第10个单元格)中查找总值 我有以下代码,没有错误(语法),但findCell方法没有返回rowNum值: public static void main(String[] args) throws IOException{ String fileName = "C:\\file-path\\report.xls"; String ce

我在电子表格中查找字符串为“Total”的单元格,然后使用该单元格所在的行在另一个始终相同的单元格/列(基于0的索引中的第10个单元格)中查找总值

我有以下代码,没有错误(语法),但findCell方法没有返回rowNum值:

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

        String fileName = "C:\\file-path\\report.xls";
        String cellContent = "Total";
        int rownr=0, colnr = 10;

        InputStream input = new FileInputStream(fileName);

        HSSFWorkbook wb = new HSSFWorkbook(input);
        HSSFSheet sheet = wb.getSheetAt(0);

        rownr = findRow(sheet, cellContent);

        output(sheet, rownr, colnr);

        finish();
    }

    private static void output(HSSFSheet sheet, int rownr, int colnr) {
        /*
         * This method displays the total value of the month
         */

        HSSFRow row = sheet.getRow(rownr);
        HSSFCell cell = row.getCell(colnr);

                System.out.println("Your total is: " + cell);           
    }

    private static int findRow(HSSFSheet sheet, String cellContent){
        /*
         *  This is the method to find the row number
         */

        int rowNum = 0; 

        for(Row row : sheet) {
            for(Cell cell : row) {

                while(cell.getCellType() == Cell.CELL_TYPE_STRING){

                    if(cell.getRichStringCellValue().getString () == cellContent);{

                            rowNum = row.getRowNum();
                            return rowNum;  
                    }
                }
            }
        }               
        return rowNum;
    }

    private static void finish() {

        System.exit(0);
    }
}   

if
语句后面有一个分号,表示
if
不起作用:

if(cell.getRichStringCellValue().getString () == cellContent);{
即使这不能解决您的问题,我认为您的
while
声明可能不适合这里

while(cell.getCellType() == Cell.CELL_TYPE_STRING)

就我所记得的,POI中还有其他类型的细胞。尝试在这些行上设置断点,并检查它们是否具有正确的单元格类型。

此方法解决了您的问题:

private static int findRow(HSSFSheet sheet, String cellContent) {
    for (Row row : sheet) {
        for (Cell cell : row) {
            if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
                    return row.getRowNum();  
                }
            }
        }
    }               
    return 0;
}

请记住,您的
colnr
仍然是一个固定值。

非常感谢。你的建议是对的,然而声明有点错误。我删除了它,并根据@msi的建议将其更改为if语句。非常感谢@msi。建议的代码解决了这个问题,它工作得很好。我对编程比较陌生,可以问你:1。为什么更改while语句和2。为什么for循环的返回值为=0。再次感谢!当您使用
循环时,您假设“total cell”左侧的所有单元格都用字符串填充。例如,像这样的单元格:| | 1 | Total |将无法通过while找到,因为
while
将在值为1时中断。返回0,以防找不到“total cell”。要比较两个字符串,应使用equals方法<代码>=
用于比较基本类型,而不是对象。getString()返回一个对象。如果您想了解有关字符串的更多信息,请阅读有关字符串池和intern方法的内容。当使用
=
进行比较时,您可以在这里找到正确的案例。我还使用了trim方法,因为用户输入喜欢在字符串末尾包含一些额外的空格。感谢您花时间解释我。我真的很感激。从现在起,我将遵循您的建议。使用派生单元格查找索引:cell.getColumnIndex,cell.getRowIndex