如果字符串包含在get getContents(JXL)中

时间:2018-11-26 16:30:01

标签: java excel jxl

更新:可能是由我自己发布的可能解决方案

我需要我的程序才能读取单元格。如果它包含一个特定的字符串,假设名称为“ Jacob”,那么它需要执行稍后在代码中定义的某个静态函数。

我的IF语句将嵌套在while循环中。

int currentRow = 1;
Cell cell;
Cell ncell;

while (!(cell = sheet.getCell(URL_COLUMN, currentRow)).getType().equals(CellType.EMPTY)) {

    String url = cell.getContents();
    System.out.println("Checking URL: " + url);

    ncell = sheet.getCell(NAME_COLUMN, currentRow);
    ncell.getContents();
    ncell.toString();

         if (ncell.contains("Jacob")) {
             String FAV = JacobFavFood(url); // A static function will be defined later for this
             System.out.println("Jacob's favorite food is " + name);
             Label cellWithFAV = new Label(FAV_COLUMN, currentRow, FAV);
             sheet.addCell(cellWithFAV);
         }


            currentRow++;

    }
workbook.write();
workbook.close();

更新:可能是由我自己发布的可能解决方案

1 个答案:

答案 0 :(得分:0)

int currentRow = 1;
Cell cell;
Cell ncell;

while (!(cell = sheet.getCell(URL_COLUMN, currentRow)).getType().equals(CellType.EMPTY)) {

    String url = cell.getContents();
    String NAME = ncell.getcontents();
    System.out.println("Checking URL: " + url);
    ncell = sheet.getCell(NAME_COLUMN, currentRow);

         if (NAME.contains("Jacob")) {
             String FAV = JacobFavFood(url); // A static function will be defined later for this
             System.out.println("Jacob's favorite food is " + name);
             Label cellWithFAV = new Label(FAV_COLUMN, currentRow, FAV);
             sheet.addCell(cellWithFAV);
         }


            currentRow++;

    }
workbook.write();
workbook.close();

我忘记了可以像使用url一样定义字符串。这确实有效,并且能够调用静态函数。我仍然想知道为什么.toString()无法正常工作。