I have a code where I enter an Ip address and I want it to find its closing matching subnet from the spreadsheet I have attached that has a whole range of subnets.
Basically, take the value inputted find the closest value in a cell in the spreadsheet and print it to console. Here is my code, I commented out a few lines so I could test out if the spreadsheet was linked properly. There are only 2 columns in the excel sheet. Column A and B. The cells go all the way down a few thousand rows.
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;
namespace Investigations
{
class Program
{
static void Main(string[] args)
{
int rCnt = 0;
int cCnt = 0;
string str;
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\Subnets.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.XlFindLookIn xlValues;
Excel.XlLookAt xlPart;
/* int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string[] headers = new string[colCount + 1];
for (cCnt = 1; cCnt <= colCount; cCnt++)
{
headers[cCnt] = (Convert.ToString((xlRange.Cells[1, cCnt] as Excel.Range).Value2));
}
for (rCnt = 2; rCnt <= rowCount; rCnt++)
{
for (cCnt = 1; cCnt <= colCount; cCnt++)
{
str = (Convert.ToString((xlRange.Cells[rCnt, cCnt] as Excel.Range).Value2));
Console.Write(headers[cCnt]);
Console.Write(" ");
Console.WriteLine(str);
}
Console.WriteLine("----------------");
}
}
}
}
*/
}
}
}