将变量与excel电子表格中的值匹配

时间:2017-03-24 15:32:16

标签: c# excel ip subnet

我有一个代码,我输入一个IP地址,我希望它从我附加的电子表格中找到它的关闭匹配子网,它具有一系列子网。

基本上,将输入的值找到电子表格中单元格中最接近的值并将其打印到控制台。这是我的代码,我注释了几行,所以我可以测试电子表格是否正确链接。 excel表中只有2列。 A列和B列。细胞一直向下走几千行。

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;

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;

            /* int rowCount = xlRange.Rows.Count;
          int colCount = xlRange.Columns.Count;
          string[] headers = new string[colCount + 1];
          for (cCnt = 1; cCnt <= colCount; cCnt++)
          {
              headers[cCnt] = (Convert.ToString((xlRange.Cells[1, cCnt] as Excel.Range).Value2));

          }

          for (rCnt = 2; rCnt <= rowCount; rCnt++)
          {
              for (cCnt = 1; cCnt <= colCount; cCnt++)
              {

                  str = (Convert.ToString((xlRange.Cells[rCnt, cCnt] as Excel.Range).Value2));
                  Console.Write(headers[cCnt]);
                  Console.Write(" ");
                  Console.WriteLine(str);

              }

              Console.WriteLine("----------------");


          }
      }





 }



      }

  */

        }
    }
}

0 个答案:

没有答案