如何将精确的字符串值或最接近的匹配项与 excel 列单元格匹配
How to match exact string value or closest match with excel column cell
我有一个代码,我可以在其中输入 IP 地址,然后它会循环并在 excel 列中搜索与该 IP 最接近的匹配项。它只是循环每个 IP,我如何在它匹配该 IP 的地方放置一个参数?
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\subnets.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
for (int i = 0; i < xlWorksheet.Rows.Count; i++)
{
IPAddress excelIP = IPAddress.Parse("8.8.8.8");
if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP))
{
Console.Write(excelIP.ToString());
Console.WriteLine(" -This id was found");
}
}
}
将您找到的与正在搜索的进行比较(您也可以将 excelIP
的声明移出循环 - 您只需声明一次)。我还创建了一个标志,以防您需要根据退出循环后是否找到您正在寻找的 IP 采取一些行动:
bool foundIP = false;
IPAddress excelIP;
for (int i = 0; i < xlWorksheet.Rows.Count; i++)
{
if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP))
{
// Compare the IP address we found with the one we're looking for
if (excelIP.Equals(addr))
{
foundIP = true;
break; // Exit the for loop since we found it
}
}
}
if (foundIP)
{
Console.WriteLine("Found the IP address!");
// If you need to do something with the IP, you can use either excelIP
// or addr, since they are both the same at this point
}
else
{
Console.WriteLine("IP address was not found.");
}
我有一个代码,我可以在其中输入 IP 地址,然后它会循环并在 excel 列中搜索与该 IP 最接近的匹配项。它只是循环每个 IP,我如何在它匹配该 IP 的地方放置一个参数?
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\subnets.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
for (int i = 0; i < xlWorksheet.Rows.Count; i++)
{
IPAddress excelIP = IPAddress.Parse("8.8.8.8");
if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP))
{
Console.Write(excelIP.ToString());
Console.WriteLine(" -This id was found");
}
}
}
将您找到的与正在搜索的进行比较(您也可以将 excelIP
的声明移出循环 - 您只需声明一次)。我还创建了一个标志,以防您需要根据退出循环后是否找到您正在寻找的 IP 采取一些行动:
bool foundIP = false;
IPAddress excelIP;
for (int i = 0; i < xlWorksheet.Rows.Count; i++)
{
if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP))
{
// Compare the IP address we found with the one we're looking for
if (excelIP.Equals(addr))
{
foundIP = true;
break; // Exit the for loop since we found it
}
}
}
if (foundIP)
{
Console.WriteLine("Found the IP address!");
// If you need to do something with the IP, you can use either excelIP
// or addr, since they are both the same at this point
}
else
{
Console.WriteLine("IP address was not found.");
}