【问题标题】:Cell value obtained with Apache POI from Excel Spreadsheet prints same value as Java String but IF statement does not say they are the same?使用 Apache POI 从 Excel 电子表格获得的单元格值打印与 Java 字符串相同的值,但 IF 语句没有说它们相同?
【发布时间】:2020-06-07 11:21:06
【问题描述】:

我正在从 Excel 电子表格中读取单元格。我正在读取每一行中的第一个单元格,并将它们与我传递给函数的 String 对象进行比较。该函数正确地遍历所有行并访问每行中的第一个单元格,但是当将它们与 String 对象参数进行比较时,IF 语句不会执行,尽管从单元格获取的值和 String 参数都打印了相同的内容。我使用了 '==' 运算符以及 .equals() 但似乎没有什么可以让 IF 语句执行。

public static ArrayList<Integer> returnCurrency(String currency, String fileName) throws IOException
{

FileInputStream excelFile = new FileInputStream(newFile(fileName));

Workbook workbook = new XSSFWorkbook(excelFile);

Sheet sheet = workbook.getSheetAt(0);

Iterator<Row> iterator = sheet.iterator();

ArrayList<Double> currencyRateArray = new ArrayList<Double>();

for( int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++)
{
    Row row = sheet.getRow(rowNum);
    Cell tempCell = row.getCell(0);

System.out.printf("temp cell has value of:%s\n",tempCell.getStringCellValue());

System.out.printf("currency variable is: %s\n", currency);
if(currency.equals(tempCellString))
{
    System.out.println("Found the currency\n");
}
}
}

当我将参数字符串作为“USD”传递时,两个打印语句都会打印“USD”,但它永远不会打印“找到货币”,因为该 IF 语句没有执行。任何帮助将不胜感激:)

【问题讨论】:

    标签: java string comparison cell


    【解决方案1】:

    找到答案 - 数据库中的单元格的值是“USD”而不是“USD”。

    【讨论】:

      猜你喜欢
      • 2017-08-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多