为什么我无法使用我的代码从 excel 获取值到列表中? [英] Why I cannot get values from excel into a list using my codes?

查看:22
本文介绍了为什么我无法使用我的代码从 excel 获取值到列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将相关的 Excel 单元格收集到一个列表中以进行序列比较.但是,我无法通过使用以下代码将带有条件的单元格值放入列表中(控制台中没有按预期打印任何内容).

I would like to gather the relevant Excel cells into a list for a sequence comparison. However, I failed to get those cell values with conditions into the list by using below codes (nothing is printed in console as expected).

我尝试使用 startsWith 和其他条件语法,但我不确定这是问题还是我之前做错了什么.

I tried using startsWith and other condition syntax but I am not sure if this is the issue or I did sth wrong in prior.

    HSSFSheet dispcolsheet = workbook2.getSheet(0);
    Iterator<Row> colRowItr = dispcolsheet.rowIterator();
    List<String> colstatuslist = new ArrayList<String>();
    while (colRowItr.hasNext()){
        Row row = colRowItr.next();
        Cell colname = row.getCell(0);
        if ("ABC_".contains(colname.getStringCellValue())) {
            colstatuslist.add(row.getCell(1).getStringCellValue());
            System.out.println(colstatuslist);
        }
    }

我的 xls 文件看起来像:

My xls file looks like:

name   |status
ABC_1  | TRUE
ABC_2  | FALSE
ABC_3  | TRUE
.
.
.

我希望按顺序存储 B 列中的 TRUE FALSE TRUE,以便我可以 get() 将它们用于比较,如 get(0) 将是 ABC_1 的状态为 TRUEget(1) 将是状态ABC_2FALSE 等等.

I expect to store the TRUE FALSE TRUE from column B in sequence so that I could get() them to use for comparison, like get(0) would be the status of ABC_1 as TRUE, get(1) would be the status of ABC_2 as FALSE and so on.

谢谢.

推荐答案

如果检查错误的方向/方式.

You are doing the if check the wrong direction/way.

"ABC_".contains(colname.getStringCellValue())

您正在检查字符串 ABC_ contains(或 has)是否包含单元格值.

You are checking if the string ABC_ contains (or has) the cell value.

ABC_ 是否包含 ABC_1 - 否

你应该把它反转为

colname.getStringCellValue().contains("ABC_") 

or 
colname.getStringCellValue().startsWith("ABC_")

ABC_1 是否包含/startsWith ABC_ - 是

Does ABC_1 contain/startsWith ABC_ - Yes

这篇关于为什么我无法使用我的代码从 excel 获取值到列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆