CNAS取数仪器端升级
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

4 个月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using CnasSynchronousCommon;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace CnasSynchronusDAL
  9. {
  10. public class ReadFileCommon01 : BaseReadFileMode
  11. {
  12. /// <summary>
  13. /// 分割每个元素的依据。0代表“ ”,1代表“\t”,2代表0和1的集合,3代表逗号
  14. /// </summary>
  15. public static string SplitType { get; set; }
  16. /// <summary>
  17. /// 起始行数(部分txt文件中,表数据的起始位置不是第一行,而是从其他行开始)
  18. /// </summary>
  19. public static int StartLineIndex { get; set; }
  20. /// <summary>
  21. /// 是否自定义创建日期字段
  22. /// </summary>
  23. public static bool IfCustomCreateDateColumn { get; set; }
  24. /// <summary>
  25. /// 后缀名类似于(.txt)
  26. /// </summary>
  27. public static string Suffix { get; set; }
  28. /// <summary>
  29. /// 以日期命名文件名称时的日期格式(yyyyMMdd)
  30. /// </summary>
  31. public static string FileNameFormat { get; set; }
  32. public static DataTable ReadTableStructure(string strFilePath)
  33. {
  34. DataTable dt = new DataTable();
  35. try
  36. {
  37. List<string> arryList = ReadFileMessage(strFilePath);
  38. if (arryList.Count >= StartLineIndex)
  39. {
  40. string[] strRowDatas = arryList[StartLineIndex].Trim().Split(GetSplitStrings(), StringSplitOptions.RemoveEmptyEntries);
  41. if (strRowDatas.Length > 0)
  42. {
  43. for (int i = 0; i < strRowDatas.Length; i++)
  44. {
  45. dt.Columns.Add($"F{i + 1}");
  46. }
  47. if (IfCustomCreateDateColumn)
  48. dt.Columns.Add("日期(系统添加)");
  49. }
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. AppLog.Error(ex.Message);
  55. }
  56. return dt;
  57. }
  58. internal static DataTable ReadTableData(string strFilePath)
  59. {
  60. DataTable dt = new DataTable();
  61. try
  62. {
  63. List<string> arryList = ReadFileMessage(strFilePath);
  64. if (arryList.Count >= StartLineIndex)
  65. {
  66. string[] strRowDatas = arryList[StartLineIndex].Trim().Split(GetSplitStrings(), StringSplitOptions.RemoveEmptyEntries);
  67. if (strRowDatas.Length > 0)
  68. {
  69. for (int i = 0; i < strRowDatas.Length; i++)
  70. {
  71. dt.Columns.Add($"F{i + 1}");
  72. }
  73. if (IfCustomCreateDateColumn)
  74. dt.Columns.Add("日期(系统添加)");
  75. }
  76. }
  77. if (arryList.Count > 0)
  78. {
  79. for (int i = 0; i < arryList.Count; i++)
  80. {
  81. if (i+1<StartLineIndex) continue; //不是数据行
  82. string strData = arryList[i].Trim();
  83. string[] strRowDatas = strData.Split(GetSplitStrings(), StringSplitOptions.RemoveEmptyEntries);
  84. if (strRowDatas.Length >0)
  85. {
  86. DataRow dr = dt.NewRow();
  87. for (int j = 0; j < strRowDatas.Length; j++)
  88. {
  89. dr[$"F{j + 1}"] = strRowDatas[j].Trim();
  90. }
  91. if (IfCustomCreateDateColumn)
  92. {
  93. if (FileNameFormat.Length > 0&&Suffix.Length>0)
  94. {
  95. int subLength = (Suffix.Length - 1) * 2 + 1 + FileNameFormat.Length;
  96. DateTime dtTime = DateTime.ParseExact(string.Concat(strFilePath.Substring(strFilePath.Length - subLength, subLength).Take(FileNameFormat.Length)), FileNameFormat, new System.Globalization.CultureInfo("zh-CN", true));
  97. dr["日期(系统添加)"] = dtTime.ToString(FileNameFormat);
  98. }
  99. }
  100. dt.Rows.Add(dr);
  101. }
  102. }
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. AppLog.Error(ex.Message);
  108. }
  109. return dt;
  110. }
  111. private static List<string> ReadFileMessage(string strFilePath)
  112. {
  113. List<string> arryList = new List<string>();
  114. if (File.Exists(strFilePath))
  115. {
  116. FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  117. StreamReader _StreamReaderKey = new StreamReader(fileStream, GetType(strFilePath));
  118. string strLine = "";
  119. while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine())))
  120. {
  121. arryList.Add(strLine);
  122. }
  123. fileStream.Close();
  124. }
  125. return arryList;
  126. }
  127. private static string[] GetSplitStrings()
  128. {
  129. string[] splitStrings =null;
  130. switch (SplitType)
  131. {
  132. case "0":
  133. splitStrings = new string[] { " "};
  134. break;
  135. case "1":
  136. splitStrings = new string[] { "\t" };
  137. break;
  138. case "2":
  139. splitStrings = new string[] { " ","\t" };
  140. break;
  141. case "3":
  142. splitStrings = new string[] { "," };
  143. break;
  144. }
  145. return splitStrings;
  146. }
  147. }
  148. }