using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using CnasSynchronousCommon; namespace CnasSynchronusDAL { public class ReadFileThirdth : BaseReadFileMode { private static int ColumnCount = 10; public static DataTable ReadTableStructure() { DataTable dt = new DataTable(); dt.Columns.Add("Date"); for (int i = 0; i < ColumnCount; i++) { dt.Columns.Add($"F{i + 1}"); } return dt; } internal static DataTable ReadTableData(string strFilePath) { DataTable dt = ReadTableStructure(); try { List arryList = new List(); if (File.Exists(strFilePath)) { FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader _StreamReaderKey = new StreamReader(fileStream, GetType(strFilePath)); string strLine = ""; while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine()))) { arryList.Add(strLine); } fileStream.Close(); } if (arryList.Count > 0) { for (int i = 0; i < arryList.Count; i++) { if (i == 0) continue; //第一行不是数据行 string strData = arryList[i].Trim(); string[] strRowDatas = strData.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (strRowDatas.Length == 10) { DataRow dr = dt.NewRow(); DateTime dtTime = DateTime.ParseExact(string.Concat(strFilePath.Substring(strFilePath.Length - 21, 21).Take(14)), "yyyyMMddHHmmss", new System.Globalization.CultureInfo("zh-CN", true)); dr["Date"] = dtTime.ToString("yyyy-MM-dd HH:mm:ss"); for (int j = 0; j < strRowDatas.Length; j++) { dr[$"F{j + 1}"] = strRowDatas[j].Trim(); } dt.Rows.Add(dr); } if (strRowDatas.Length == 9) //内容中可能存在数据连接在一起的情况 { DataRow dr = dt.NewRow(); DateTime dtTime = DateTime.ParseExact(string.Concat(strFilePath.Substring(strFilePath.Length - 21, 21).Take(14)), "yyyyMMddHHmmss", new System.Globalization.CultureInfo("zh-CN", true)); dr["Date"] = dtTime.ToString("yyyy-MM-dd HH:mm:ss"); int index = 0; for (int j = 0; j < strRowDatas.Length; j++) { string strCurrentString = strRowDatas[j].Trim(); if (System.Text.RegularExpressions.Regex.IsMatch(strCurrentString, @"[\u4e00-\u9fa5]")) { var strArray = strCurrentString.ToCharArray(); string strString = ""; for (int indexs = 0; indexs < strArray.Length; indexs++) { if (strArray[indexs] >= 0x4e00 && strArray[indexs] <= 0x9fa5) break; strString += strArray[indexs]; } dr[$"F{index + 1}"] = strString; index++; dr[$"F{index + 1}"]=strCurrentString.Replace(strString,"").Trim(); index++; } else { dr[$"F{index + 1}"] = strRowDatas[j].Trim(); index++; } } dt.Rows.Add(dr); } } } } catch (Exception ex) { AppLog.Error(ex.Message); } return dt; } } }