一個查詢excel記錄的ip並ping的小程序


因為現場的設備ip配置都是固定的,有時候想測試網絡怎么樣要挨個ping特麻煩,就弄了個方法來ping,文件路徑暫時都是寫死的

.net core 的 一個控制台程序

  1 static void Main(string[] args)
  2         {
  3             Console.WriteLine("Hello World!");
  4             DataSet ds =  ToDataTable("F:\\***\\***\\IP地址分配.xlsx");
  5             DataTable dt = ds.Tables[0];
  6             string butrong = string.Empty;
  7             for (int i = 0; i < dt.Rows.Count; i++)
  8             {
  9                 string ip = dt.Rows[i][3].ToString().Trim().Replace("", ".");
 10                 if (!string.IsNullOrEmpty(ip))
 11                 {
 12                     string rtn = (PingIp(ip) ? "trun" : "false");
 13                     if (rtn == "false")
 14                     {
 15                         butrong += dt.Rows[i][2].ToString();
 16                         butrong += ip;
 17                         butrong += "||";
 18                     }
 19                     Console.WriteLine("pingIP" + ip+" : "+ rtn);
 20                 }
 21                 
 22             }
 23             Console.WriteLine("不通的ip有:"+ butrong);
 24         }
 25         /// <summary>
 26         /// ping ip,測試能否ping通
 27         /// </summary>
 28         /// <param name="strIP">IP地址</param>
 29         /// <returns></returns>
 30         private static bool PingIp(string strIP)
 31         {
 32             bool bRet = false;
 33             try
 34             {
 35                 Ping pingSend = new Ping();
 36                 PingReply reply = pingSend.Send(strIP, 1000);
 37                 if (reply.Status == IPStatus.Success)
 38                     bRet = true;
 39             }
 40             catch (Exception)
 41             {
 42                 bRet = false;
 43             }
 44             return bRet;
 45         }
 46 
 47 
 48 
 49         /// <summary>  
 50         /// 讀取Excel文件到DataSet中  
 51         /// </summary>  
 52         /// <param name="filePath">文件路徑</param>  
 53         /// <returns></returns>  
 54         public static DataSet ToDataTable(string filePath)
 55         {
 56             string connStr = "";
 57             string fileType = System.IO.Path.GetExtension("F:\\***\\***\\IP地址分配.xlsx");
 58             if (string.IsNullOrEmpty(fileType)) return null;
 59 
 60             if (fileType == ".xls")
 61                 connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
 62             else
 63                 connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
 64             string sql_F = "Select * FROM [{0}]";
 65 
 66             OleDbConnection conn = null;
 67             OleDbDataAdapter da = null;
 68             DataTable dtSheetName = null;
 69 
 70             DataSet ds = new DataSet();
 71             try
 72             {
 73                 // 初始化連接,並打開  
 74                 conn = new OleDbConnection(connStr);
 75                 conn.Open();
 76 
 77                 // 獲取數據源的表定義元數據                         
 78                 string SheetName = "";
 79                 dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
 80 
 81                 // 初始化適配器  
 82                 da = new OleDbDataAdapter();
 83                 for (int i = 0; i < dtSheetName.Rows.Count; i++)
 84                 {
 85                     SheetName = (string)dtSheetName.Rows[i]["TABLE_NAME"];
 86 
 87                     if (SheetName.Contains("$") && !SheetName.Replace("'", "").EndsWith("$"))
 88                     {
 89                         continue;
 90                     }
 91 
 92                     da.SelectCommand = new OleDbCommand(String.Format(sql_F, SheetName), conn);
 93                     DataSet dsItem = new DataSet();
 94                     da.Fill(dsItem,i.ToString());//這里找個第二個參數還沒弄明白是干嘛用的,網上抄的
 95 
 96                     ds.Tables.Add(dsItem.Tables[0].Copy());
 97                 }
 98             }
 99             catch (Exception ex)
100             {
101             }
102             finally
103             {
104                 // 關閉連接  
105                 if (conn.State == ConnectionState.Open)
106                 {
107                     conn.Close();
108                     da.Dispose();
109                     conn.Dispose();
110                 }
111             }
112             return ds;
113         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM