2017-03-31 10 views
0

例として3100のサブネット(192.168.1.0/24)を持つインポートされたExcelシートがあります。私は格納された変数で最初の3オクテットを検索できるようにしたい。私がそれを得ることができれば、彼は最初の3オクテットだけを含むようにスプレッドシートを編集して、私がプログラムの将来に望む結果を得ることができます。事前にどうもありがとうございました。ここで3オクテット(0.0.0)を使用して変数として保存

string octets = ("10.2.30"); 
     var match = false; 

     for (int i = 0; i < xlRange.Rows.Count; i++) 
     { 
      IPAddress excelIP; 

      if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP)) 
      { 

       if (excelIP.ToString().Equals(octets)) 
       { 
        match = true; 
        Console.Write(excelIP.ToString()); 
        Console.WriteLine(" -This id was found"); 
       } 
      } 
     } 
     if (!match) 
     { 
      Console.WriteLine("No Match "); 

     } 

     xlWorkbook.Close(); xlApp = null; 
    } 
+0

10.2.300オクテットが255 –

+0

の上に行くことができないので、有効なIPと一致することはありませんおっと、私は余分な0を追加しました、私はそれを修正しましょう! – gahser1

+1

セルの値がCIDR形式であれば、 'IPAddress.TryParse()'は成功するとは思われません。おそらく 'IPAddress.TryParse(xlWorksheet.Cells [i + 1、1] .Value.Split( '/')[0]、out excel)'を実行する必要があります。次に、3オクテットの文字列が4オクテットのIP文字列と決して一致しないので、 'excelIP.ToString()。StartsWith(octets)'を使いたいでしょう。もちろん、 "192.168.100.1"が "192.168.10"で始まるところで問題が発生する可能性があります。 – itsme86

答えて

0

はこれを行う方法のカップルです:

  1. は、末尾のピリオドを使用してoctets文字列を変更し、excelIP文字列の先頭にそれを一致させます。
  2. 文字列をListに分割し、そのリストの要素をexcelIPの同じ数の要素とオクテットのリストに分割して比較します。

どちらの場合でも、式を使用するにはSystem.Linqへの参照を追加する必要があります。 #1のために

using System; 
using System.Linq; 
using System.Net; 

// add a trailing '.' so we can match the start of the excelIP 
string octets = ("10.2.30."); 
var match = false; 

for (int i = 0; i < xlRange.Rows.Count; i++) 
{ 
    // Get the IP address portion of the CIDR string 
    var cellString = xlWorksheet.Cells[i + 1, 1].Value.Split('/')[0]; 
    IPAddress excelIP; 

    // If cellString starts with the octets string and it's a valid IP address... 
    if (cellString.StartsWith(octets) && IPAddress.TryParse(cellString, out excelIP)) 
    { 
     match = true; 
     Console.Write(excelIP.ToString()); 
     Console.WriteLine(" -This id was found"); 
    } 
} 

#2の場合:

var match = false; 
string octets = ("10.2.30"); 

string[] octetsToMatch = octets.Split('.'); 

for (int i = 0; i < xlRange.Rows.Count; i++) 
{ 
    // Get the IP address portion of the CIDR string 
    var cellString = xlWorksheet.Cells[i + 1, 1].Value.Split('/')[0]; 
    IPAddress excelIP; 

    if (IPAddress.TryParse(cellString, out excelIP)) 
    { 
     // Compare the first octets of the IP address with our octetsToMatch 
     if (octetsToMatch.SequenceEqual(cellString.Split('.').Take(octetsToMatch.Length))) 
     { 
      match = true; 
      Console.Write(excelIP.ToString()); 
      Console.WriteLine(" -This id was found"); 
     } 
    } 
} 
+0

2番目のオプションでは、 "SequenceEqual"は文字列[]からその引数を取りません。 – gahser1

+0

はい、 'SequenceEqual'は' IEnumerable'をとり、 'Take'は' IEnumerable'を返します。私の例では、 'octetsToMatch'と' cellString.Split'の両方が 'string []'型であるため、すべてがOKであるはずです。私はあなたが確認できる小さなサンプルコードで質問を更新しました。あなたはどんなエラーを出していますか? –

+0

Console.Writeline(excelIP.ToString())に到達したときの最初のオプション。 excelIPは割り当てられていない変数の使用です。 – gahser1

関連する問題