|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 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<string> arryList = new List<string>();
- 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;
- }
- }
- }
|