简体   繁体   English

如何遍历一列以查找具有特定值的单元格,然后遍历那些行?

[英]How can I iterate over a column to find cells with a specific value and then iterate over those rows?

I want to search through a column (column A) and find all the "Good" files, then I want to create a list of all the data in Column B, the "Corresponding Info About File" that corresponds with the Good files from Column A. Therefore, excluding all the bad files and their corresponding info in the final list 我想搜索一列(A列)并找到所有“ Good”文件,然后我想在B列中创建所有数据的列表,其中“与文件有关的信息”与该列中的Good文件相对应A.因此,在最终列表中排除所有不良文件及其对应的信息

在此处输入图片说明

My code so far returns all the Good items in Column A, but returns ALL the items in Column B, rather than just the ones that correspond with Column A. 到目前为止,我的代码返回了A列中的所有Good项目,但返回了B列中的所有项目,而不仅仅是与A列相对应的项目。

          Excel.Worksheet xlWorkSheet = (Excel.Worksheet)excelWorkbook.Sheets[sheetSpaces];
        Excel.Range col = (Excel.Range)xlWorkSheet.Columns["A:A", Type.Missing];
        Excel.Range colB = (Excel.Range)xlWorkSheet.Columns["B:B", Type.Missing];

        foreach (Excel.Range item in col.Cells)
        {
            string text = (string)item.Text;
            if (text == "Good")
            {                 
                Console.WriteLine(text);

                foreach (Excel.Range itemSub in colB.Cells)
                {
                    string textSub = (string)itemSub.Text;
                }
            }
        }
    }

You need to add an if-statement to your loop: 您需要在循环中添加一个if语句:

if(item.Value().ToString().Contains("Good")){
// do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM