C#采集解析log文件及CSV文件数据到DataTable

 

 1         /// <summary>
 2         /// XRD.log 读取
 3         /// </summary>
 4         /// <param name="fileName">文件名</param>
 5         /// <returns>返回List集合</returns>
 6         public static List<Dictionary<string, string>> GetXRDTestData(string fileName)
 7         {
 8             try
 9             {
10                 var dictlist = new List<Dictionary<string, string>>();
11                 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
12                 StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
13                 string strLine = sr.ReadLine();
14                 string strLin = sr.ReadToEnd();
15                 int n = 14;
16                 string[] arr = strLine.Split(new char[] { ',' }, options: StringSplitOptions.RemoveEmptyEntries);      //取出列
17                 string[] content = strLin.Split(new char[] { ',' }, options: StringSplitOptions.RemoveEmptyEntries); //取出内容
18                 content = content.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
19                 for (int i = 0; i < content.Length; i++)                                                             //去掉空格
20                 {
21                     content[i] = content[i].Trim();
22                 }
23                 DataTable dt = new DataTable();
24                 for (int i = 0; i < arr.Length; i++)
25                 {
26                     dt.Columns.Add(arr[i].Trim());
27                 }
28                 if (content.Length > 0)
29                 {
30                     for (int i = 0; i < content.Length / n; i++)
31                     {
32 
33                         DataRow dr = dt.NewRow();
34                         dr[0] = content[i * n];
35                         dr[1] = content[i * n + 1];
36                         dr[2] = content[i * n + 2];
37                         dr[3] = content[i * n + 3];
38                         dr[4] = content[i * n + 4];
39                         dr[5] = content[i * n + 5];
40                         dr[6] = content[i * n + 6];
41                         dr[7] = content[i * n + 7];
42                         dr[8] = content[i * n + 8];
43                         dr[9] = content[i * n + 9];
44                         dr[10] = content[i * n + 10];
45                         dr[11] = content[i * n + 11];
46                         dr[12] = content[i * n + 12];
47                         dr[13] = content[i * n + 13];
48                         dt.Rows.Add(dr);
49                     }
50                 }
51                 string[] WaferIDs = dt.AsEnumerable().ToList().Select(x => x.Field<string>("WaferID")).ToArray();
52                 WaferIDs = DelRepeatData(WaferIDs);
53                 DataTable newdt = dt.Clone();
54                 // Dictionary<string, string> dict = new Dictionary<string, string>();
55                 string lotID, waferID;
56                 for (int i = 0; i < WaferIDs.Length; i++)
57                 {
58                     newdt.Clear();
59                     Dictionary<string, string> dict = new Dictionary<string, string>();
60                     var dataRows = from datarow in dt.AsEnumerable()
61                                    where datarow.Field<string>("WaferID") == WaferIDs[i]
62                                    select datarow;
63                     foreach (var item in dataRows)
64                     {
65                         newdt.ImportRow(item);
66                     }
67                     lotID = WaferIDs[i];
68                     waferID = lotID;
69                     dict.Add("LOTID", lotID);
70                     dict.Add("WAFER_ID", waferID);
71                     string[] arrFWHM1 = newdt.AsEnumerable().Select(d => d.Field<string>("102FWHM1")).ToArray();
72                     string[] arrFWHM2 = newdt.AsEnumerable().Select(d => d.Field<string>("002FWHM1")).ToArray();
73                     string[] arrX1 = newdt.AsEnumerable().Select(d => d.Field<string>("X1")).ToArray();
74                     string[] arrSum = newdt.AsEnumerable().Select(d => d.Field<string>("sum[T1+T2]")).ToArray();
75                     string[] arrX3 = newdt.AsEnumerable().Select(d => d.Field<string>("X3")).ToArray();
76                     string strFWHM1 = string.Join(",", arrFWHM1);
77                     string strFWHM2 = string.Join(",", arrFWHM2);
78                     string strX1 = string.Join(",", arrX1);
79                     string strSum = string.Join(",", arrSum);
80                     string strX3 = string.Join(",", arrX3);
81                     dict.Add("102FWHM1", strFWHM1);
82                     dict.Add("002FWHM1", strFWHM2);
83                     dict.Add("X1", strX1);
84                     dict.Add("sum[T1+T2]", strSum);
85                     dict.Add("X3", strX3);
86                     dictlist.Add(dict);
87                 }
88                 sr.Close();
89                 fs.Dispose();
90                 return dictlist;
91 
92             }
93             catch (Exception ex)
94             {
95                 UtilLog.LogError(string.Format("XRD文件解析出错了,详情如下:{0}", ex.Message));
96                 return new List<Dictionary<string, string>>();
97             }
98         }

CSV文件解析

 1         /// <summary>
 2         /// 将CSV文件中内容读取到DataTable中
 3         /// </summary>
 4         /// <param name="path">CSV文件路径</param>
 5         /// <param name="hasTitle">是否将CSV文件的第一行读取为DataTable的列名</param>
 6         /// <returns></returns>
 7         public static DataTable ReadFromCSV(string path, bool hasTitle = false)
 8         {
 9             DataTable dt = new DataTable();           //要输出的数据表
10             FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
11             StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);//文件读入流
12             bool bFirst = true;                       //指示是否第一次读取数据
13 
14             //逐行读取
15             string line;
16             while ((line = sr.ReadLine()) != null)
17             {
18                 string[] elements = line.Split(',');
19 
20                 //第一次读取数据时,要创建数据列
21                 if (bFirst)
22                 {
23                     for (int i = 0; i < elements.Length; i++)
24                     {
25                         dt.Columns.Add();
26                     }
27                     bFirst = false;
28                 }
29 
30                 //有标题行时,第一行当做标题行处理
31                 if (hasTitle)
32                 {
33                     for (int i = 0; i < dt.Columns.Count && i < elements.Length; i++)
34                     {
35                         dt.Columns[i].ColumnName = elements[i];
36                     }
37                     hasTitle = false;
38                 }
39                 else //读取一行数据
40                 {
41                     if (elements.Length == dt.Columns.Count)
42                     {
43                         dt.Rows.Add(elements);
44                     }
45                     else
46                     {
47                         //throw new Exception("CSV格式错误:表格各行列数不一致");
48                     }
49                 }
50             }
51             sr.Close();
52             fs.Close();
53             return dt;
54         }

 

posted @ 2022-02-24 16:35  DotNet码农  阅读(489)  评论(0编辑  收藏  举报
/*粒子线条,鼠标移动会以鼠标为中心吸附的特效*/