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

查看:86
本文介绍了为什么我无法使用代码将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文件如下:

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

我希望按顺序存储列B中的TRUE FALSE TRUE,以便我可以将它们使用get()进行比较,例如get(0)将以ABC_1的状态显示为TRUE的状态将ABC_2的状态设置为FALSE,依此类推.

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.

谢谢.

推荐答案

您正在执行 if 检查错误的方向/方式.

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

使用

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

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

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是否包含/开始于ABC_-是

Does ABC_1 contain/startsWith ABC_ - Yes

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

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