2016-10-25 4 views
0

"Customer:"を含むセルを見つけてプログラムが正常に動作するプログラムを作成しました。問題は、セルのすぐ隣のセルの値が必要なことです。レイアウトは次のようになります。現在のセルに隣接するセルの値を見つける方法C#+ Excel

__________ _________ 
|Customer:| | Steve | 
|_________| |________| 
|Customer:| | John | 
|_________| |________| 
|Customer:| | Frank | 
|_________| |________| 

この場合、値 "Steve"、 "John"、および "Frank"が必要です。これどうやってするの?

おかげで、ルーク

マイコード:

public void gatherInfo() 
    { 
     string temp = ""; 

     Excel.Range currentFind = null; 
     Excel.Range firstFind = null; 

foreach (Excel.Worksheet sheet in excelWorkbook.Application.Worksheets) 
     { 
      try 
      { 
       // access cell within sheet 
       Excel.Range excelCell = 
         (Excel.Range)sheet.get_Range("A1", Type.Missing);     

       currentFind = excelCell.Find("Customer:", Type.Missing, 
      Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, 
      Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, 
      Type.Missing, Type.Missing); 

       while (currentFind != null) 
       { 
        // Keep track of the first range you find. 
        if (firstFind == null) 
        { 
         firstFind = currentFind; 
        } 

        // If you didn't move to a new range, you are done. 
        else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1) 
          == firstFind.get_Address(Excel.XlReferenceStyle.xlA1)) 
        { 
         //String value of cell 
         temp = currentFind.Value2.ToString(); 

         break; 
        } 

        currentFind = excelCell.FindNext(currentFind); 

       } 

       //Find adjacent cell value here? 

       holdInformation.Add(temp); 

      } 

      catch 
      { 
       Console.WriteLine("Couldn't get customer name"); 
      } 

答えて

1

私はオフセット機能は、あなたが探しているものだと思います。あなたのコードでは、あなたが行くの行を追加することができます。

firstFind.Offset[0, 1]; 

あなたがExcelから右に1列目をターゲットにすることを意味するために使用すると、現在の行 「1」を標的とすることを意味する「0」範囲

+0

ありがとう!それはうまくいった。唯一のことは...私は単セルがCからHまで広がっていることに気付きました.Cの部分を参照すれば値を読み取ることができますか? –

+0

*列C〜H 同様に: C D E F G H –

+0

C#についてよくわかりません。しかし、おそらく 'Resize'プロパティも機能しますか? – Brian

関連する問題