简体   繁体   English

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

[英]Java POI: How to find an Excel cell with a string value and get its position (row) to use that position to find another cell

I'm looking for a cell in a spreadsheet that has the string 'Total' and then use the row in which that cell is to find the total value in another cell which is always the same cell/column (the 10th cell in a 0 based index). 我正在寻找一个包含字符串'Total'的电子表格中的单元格,然后使用该单元格所在的行来查找另一个单元格中的总值,该单元格始终是相同的单元格/列(0中的第10个单元格)基于索引)。

I have the following code, which has no errors (syntax), but the findCell method is not returning rowNum value: 我有以下代码,没有错误(语法),但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);
    }
}   

This method fix is the solution to your problem: 此方法修复是您的问题的解决方案:

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

Keep in mind that your colnr is still a fixed value. 请记住,您的colnr仍然是固定值。

You have a semicolon after your if statement which means your if won't work: if语句后面有一个分号,表示你的if不起作用:

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

Even if this won't resolve your problem, I think your while statement may not be proper for here; 即使这不能解决你的问题,我认为你的while声明可能不适合这里;

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

As far as I remember, there are other Cell types in POI. 据我记忆,POI中还有其他Cell类型。 Try to put a breakpoint on these lines and check them if they have correct CellType. 尝试在这些行上设置断点并检查它们是否具有正确的CellType。

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

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