I am trying to match an inputted ip address with a cell in a spreadsheet in excel. I input the IP, the and a variable searches the excel spreadsheet for the closest match. The issue with my code, is the excelIP variable no matter what only ever stores the first cell value from my spreadsheet so there is never a match.
using System;
using System.Net;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
using System.Data;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Investigations
{
class Program
{
static void Main(string[] args)
{
IPAddress addr = IPAddress.Parse("8.8.8.8");
IPHostEntry entry = Dns.GetHostEntry(addr);
Console.WriteLine("IP Address: " + addr);
Console.WriteLine("Host Name: " + entry.HostName);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\Subnets1.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
for(int i = 0; i < xlWorksheet.Rows.Count; i++)
{
IPAddress excelIP;
if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP))
{
if (excelIP.ToString().Equals(addr))
{
Console.Write(excelIP.ToString());
Console.WriteLine(" -This id was found");
}
else
{
Console.WriteLine("No Match ");
break;
}
}
}
}
}
}