私の変数 "col"は、B列のデータを取得します。B列に含まれている特定のデータを検索したいのですが、無限ループして永遠に検索する必要はありません。どのように1つのエクセルカラムを反復処理できますか?データを持つ最後のセルで処理を停止しますか?
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open("X:\\Private\\DATA\\PROJECT DATA\\Database\\TETRA\\Master Captured List - TETRA.xlsx");
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)excelWorkbook.Sheets[sheetSpaces];
Excel.Range col = (Excel.Range)xlWorkSheet.Columns["B:B", Type.Missing];
excelApp.Visible = true;
excelApp.ScreenUpdating = false;
excelApp.DisplayAlerts = false;
foreach (Excel.Range item in col.Cells)
{
string text = (string)item.Text;
if (text == "A")
{
Console.Write("A was found in cell")
}
Console.Write("This text will loop infinitely because it's iterating over every cell in Column B, not just the one with data. I want to stop this so I can do other things here and it's not just iterating forever")
}
したがって、空のセルを見つけることは何ですか?あなたのコードには、何もありません。 – JensS
このようなプロジェクトではinteropを使用しないでください。 'microsoft.ace.oledb'プロバイダーを使用し、それを使用して、データ・リーダーまたはデータ・テーブルを反復するだけです。そのアプローチはあなたのためのすべての問題を解決します。あなたのアプローチは非常に非効率的です。 –