我正在从 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 语句不执行。任何帮助将不胜感激:)
最佳答案
找到答案 - 数据库中的单元格值为“USD”而不是“USD”。
关于java - 使用 Apache POI 从 Excel 电子表格获取的单元格值打印与 Java 字符串相同的值,但 IF 语句没有说它们是相同的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60365580/