C# 将字符串值与excel电子表格中具有相同或接近相同值的单元格相匹配

C# 将字符串值与excel电子表格中具有相同或接近相同值的单元格相匹配,c#,excel,ip,match,C#,Excel,Ip,Match,我正在尝试将输入的ip地址与excel电子表格中的单元格进行匹配。我输入IP,然后一个变量在excel电子表格中搜索最接近的匹配项。我的代码的问题是excelIP变量,不管是什么,它只存储电子表格中的第一个单元格值,因此永远不存在匹配项 using System; using System.Net; using Microsoft.Office.Interop.Excel; using Excel = Microsoft.Office.Interop.Excel; using System.Da

我正在尝试将输入的ip地址与excel电子表格中的单元格进行匹配。我输入IP,然后一个变量在excel电子表格中搜索最接近的匹配项。我的代码的问题是excelIP变量,不管是什么,它只存储电子表格中的第一个单元格值,因此永远不存在匹配项

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;
                    }



                }
            }

        }

    }
 }
使用系统;
Net系统;
使用Microsoft.Office.Interop.Excel;
使用Excel=Microsoft.Office.Interop.Excel;
使用System.Data.OleDb;
使用系统数据;
使用System.Runtime.InteropServices;
使用System.Text.RegularExpressions;
命名空间调查
{
班级计划
{
静态void Main(字符串[]参数)
{
IPAddress addr=IPAddress.Parse(“8.8.8.8”);
IPHostEntry条目=Dns.GetHostEntry(addr);
控制台写入线(“IP地址:+addr”);
Console.WriteLine(“主机名:”+entry.HostName);
Excel.Application xlApp=新的Excel.Application();
Excel.Workbook xlWorkbook=xlApp.Workbooks.Open(@“C:\Users\Subnets1.xlsx”);
Excel._工作表xlWorksheet=xlWorkbook.Sheets[1];
Excel.Range xlRange=xlWorksheet.UsedRange;
对于(int i=0;i
您需要对代码进行两个小更改

  • 移除
    中断在else上。这将导致程序退出
  • 将语句
    更改为使用
    xlRange
    而不是
    xlsheet
    ,否则也会中断。正确的声明如下:
  • for(int i=0;i

    试试看,让我知道你的意见

    编辑:刚刚注意到,您可能只希望有一个结果匹配或不匹配,因此您需要有:

            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(addr))
                    {
                        match = true;
                        Console.Write(excelIP.ToString());
                        Console.WriteLine(" -This id was found");
                    }
                }
            }
            if (!match)
            {
                Console.WriteLine("No Match ");
            }
    
    var匹配=false;
    对于(int i=0;i

    在这种情况下,布尔匹配被声明为false,只有在找到匹配时才会更改为true。使用该变量,如果(int i=0;i