2017-09-01 13 views
0

私の変数 "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") 
} 
+0

したがって、空のセルを見つけることは何ですか?あなたのコードには、何もありません。 – JensS

+0

このようなプロジェクトではinteropを使用しないでください。 'microsoft.ace.oledb'プロバイダーを使用し、それを使用して、データ・リーダーまたはデータ・テーブルを反復するだけです。そのアプローチはあなたのためのすべての問題を解決します。あなたのアプローチは非常に非効率的です。 –

答えて

0

EDIT:質問を読んだときにこれがC#用であるとは思わなかったので、これが該当するかどうかはわかりません。コードはExcel用のVBAにあります。

まず、見ている列の空白でないセルの数を数えます。次に、その列のセルを反復処理し、データが入っているセルがあるたびにその数を減らします。いったんゼロに達すると、それは最後のセルです。以下のコード例:

Dim int_cell_count As Integer 
Dim int_row As Integer: int_row = 0 

int_cell_count = Application.WorksheetFunction.CountA(Range("B:B")) 
Do 
    int_row = int_row + 1 
    If Not IsEmpty(Range("B" & int_row)) Then 
     int_cell_count = int_cell_count - 1 
     ' do stuff with the cell here 
    End If 
Loop Until int_cell_count = 0 

MsgBox "Last Cell was: B" & int_row 
0

私はその後..それを考え出したし、シート全体で最後に使用セルを見つけることRange.Specialのパラメータの型.xlCellTypeLastCellを使用してからだったその何行を見つけるために.Rowを使用行Bに最後に使用されたセルを取得するためにその行番号に「B」列を連結します。新しいコード:

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]; 
      excelApp.Visible = true; 
      excelApp.ScreenUpdating = false; 
      excelApp.DisplayAlerts = false; 
      Excel.Range last = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing); 
      int lastUsedRow = last.Row; 
      string lastCell = "B" + lastUsedRow.ToString(); 
      Excel.Range range = xlWorkSheet.get_Range("B1", lastCell); 

      foreach (Excel.Range item in range.Cells) 
      { 
       string text = (string)item.Text; 
       if (text == "A") 
       { 
        Console.Write("A was found in cell") 
       } 

    } 
+0

まだ、非効率的なアプローチ。さらに、これがサーバー側のアプリケーションである場合、このアプローチは「NO-NO」です –

関連する問題