Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串值与导入的excel电子表格中的值匹配_C#_Excel_Ip_Subnet - Fatal编程技术网

C# 将字符串值与导入的excel电子表格中的值匹配

C# 将字符串值与导入的excel电子表格中的值匹配,c#,excel,ip,subnet,C#,Excel,Ip,Subnet,我正在尝试输入一个与excel spreadhseet中的IP匹配的IP地址。它开始在DataRow row=xlWorksheet.Rows[i]中崩溃;线路。我需要它来循环,因为我要处理数千个ip和子网,它需要与最近的ip匹配。我在想,它可能不会收集电子表格中的所有单元格?因为输入的IP地址是一个字符串变量,当我使用我注释掉的代码来显示所有列时,它会得到所有列 System; using System.Net; using Microsoft.Office.Interop.Excel; us

我正在尝试输入一个与excel spreadhseet中的IP匹配的IP地址。它开始在DataRow row=xlWorksheet.Rows[i]中崩溃;线路。我需要它来循环,因为我要处理数千个ip和子网,它需要与最近的ip匹配。我在想,它可能不会收集电子表格中的所有单元格?因为输入的IP地址是一个字符串变量,当我使用我注释掉的代码来显示所有列时,它会得到所有列

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)
        {
            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;

            string columnSubnet = "Network"; // change to the correct header

            Match match = Regex.Match(columnSubnet, @".*[0-3148].*");
            for (int i = 0; i < xlWorksheet.Rows.Count; i++)
            {
                // get the current row
                DataRow row = xlWorksheet.Rows[i];
                // get the ID from the row
                string idValue = row[columnSubnet].ToString();
                // check if the row value is equal to the textbox entry
                bool myMatch = idValue.Equals(addr);
                // if both of the above are true, do this
                if (match.Success && myMatch == true)
                {
                    Console.Write(idValue);
                    Console.WriteLine(" -This id was found");
                }
系统;
Net系统;
使用Microsoft.Office.Interop.Excel;
使用Excel=Microsoft.Office.Interop.Excel;
使用System.Data.OleDb;
使用系统数据;
使用System.Runtime.InteropServices;
使用System.Text.RegularExpressions;
命名空间调查
{
班级计划
{
静态void Main(字符串[]参数)
{
int rCnt=0;
int-cCnt=0;
字符串str;
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\subnets.xlsx”);
Excel._工作表xlWorksheet=xlWorkbook.Sheets[1];
Excel.Range xlRange=xlWorksheet.UsedRange;
Excel.Range currentFind=null;
Excel.Range firstFind=null;
Excel.XlFindLookIn xlValues;
Excel.XlLookAt xlPart;
string columnSubnet=“Network”;//更改为正确的头
Match Match=Regex.Match(列子网,@.*[0-3148].*);
对于(int i=0;i
您的代码中有几个关于对象类型的问题

首先,变量
addr
的类型为
IPAddress
,但随后您尝试将该值与字符串进行比较。字符串对象永远不会等于
IPAddress
对象。您需要将传入的ip
string
解析为
IPAddress
对象本身,如下所示:

IPAddress idValue = IPAddress.Parse(row[columnSubnet].ToString());
其次,关于您声明的问题,
xlsheet.Rows[i]
为您提供了一个
范围
对象,而不是
数据行
对象。您的代码没有显示数据行被保存在任何位置,因此我假设您只是使用它来获取值。您可以通过这种方式直接提取所需的值,而无需检查正则表达式:(假设您知道列索引)

for(int i=0;i
没错,那是有道理的。我只是想,因为addr存储了一个值,所以我可以这样使用它。非常感谢Hindex,意思是如果它在B列中,那么你就可以使用
。Cells[I+1,2]
。行和列索引都从1开始递增。IPAddress idValue=IPAddress.Parse(row[columnSubnet].ToString());我在行中遇到错误。它可能是我放置的位置?这一行[I+1],我在Console.Write(excelIP.toString());line中也有错误。我是否缺少任何参数?请将上述方法与
xlsheet.Cells[r,c].Value.toString()
一起使用,而不是
行[columnSubnet].toString()
r,c下面有红色。我还有几个其他问题,我在上面的评论中发表了。我明白了逻辑,我以前从未导入过excel工作表,所以我对此有点陌生。
for (int i = 0; i < xlWorksheet.Rows.Count; i++)
{   
    IPAddress excelIP;
    if (IPAddress.TryParse(xlWorksheet.Cells[i + 1, <ip column index>].Value.ToString(), out excelIP)) 
    {
        Console.Write(excelIP.toString());
        Console.WriteLine(" -This id was found");
    }
}