【问题标题】:How to match exact string value or closest match with excel column cell如何匹配精确的字符串值或与excel列单元格最接近的匹配
【发布时间】:2017-08-20 11:49:36
【问题描述】:

我有一个代码,我可以在其中输入 IP 地址,然后它将循环并在该 IP 的 excel 列中搜索最接近的匹配项。它只是循环每个 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");                        
                }
            }    
        }

【问题讨论】:

    标签: c# excel ip subnet


    【解决方案1】:

    比较你找到的那个和你正在搜索的那个(你也可以把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.");
    }
    

    【讨论】:

    • 它目前只是挂在代码的第一部分之后,你帮助的那个开始。我认为这需要大量的编译工作?
    • 挂在哪一行?
    • 当我使程序崩溃时,它会在 if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, 1].Value.ToString(), out excelIP)) 处给出一个处理异常我还需要打印出excel IP,最接近的匹配。
    • 嗯,有什么例外,它说什么?那部分是您的原始代码。还有你所说的最接近的匹配是什么意思?
    • 我有一系列 IP 子网,因此与工作表中该 IP 地址最接近的匹配项,它将打印出该子网。我知道这会很慢,因为我们正在处理大量数据。我目前正在再次运行它,并添加了 excelIP 的打印,现在它只是挂起。现在它已经崩溃了,无法对空引用执行运行时绑定
    猜你喜欢
    • 1970-01-01
    • 2017-03-29
    • 2017-08-21
    • 1970-01-01
    • 2022-11-26
    • 2021-10-18
    相关资源
    最近更新 更多