【问题标题】:Matching a string value with a cell with the same or close to same value in an excel spreadsheet将字符串值与excel电子表格中具有相同或接近相同值的单元格匹配
【发布时间】:2017-08-21 21:44:35
【问题描述】:

我正在尝试将输入的 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;
                    }



                }
            }

        }

    }
 }

【问题讨论】:

标签: c# excel ip match


【解决方案1】:

您需要对代码进行 2 处小改动

  1. 删除else上的break;。这将导致程序退出
  2. for statement 更改为使用xlRange 而不是xlWorksheet,否则也会中断。正确的语句如下:

for (int i = 0; i &lt; xlRange.Rows.Count; i++)

试一试,让我知道你的 cmets

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

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

在这种情况下,布尔匹配被声明为 false,并且只有在找到匹配时才更改为 true。使用该变量,如果找到或未找到该值,我们就只能向控制台写入一次

【讨论】:

  • for (int i = 0; i
  • @gahser1 修正了答案。现在就试一试
  • 我在第一个 if 语句中得到一个未处理的异常。
  • 你有更多关于错误的信息吗?请检查您之间是否有空单元格
  • 工作表最后的空单元格,有没有我可以添加截图?当我运行循环时,它会打印该列中的每个子网 IP,直到最后,如果有帮助,它也会崩溃。
猜你喜欢
  • 2017-08-17
  • 2017-08-20
  • 2016-12-09
  • 2020-06-07
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多