CNAS取数仪器端升级
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 5 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 TXTDAL
  11. {
  12. /// <summary>
  13. /// 获取表名表结构
  14. /// </summary>
  15. public static DataTable ReadTxtFileColumn(string strFilePath,string strTxtFileMode)
  16. {
  17. DataTable dt = new DataTable();
  18. try
  19. {
  20. if (strTxtFileMode == "0" || strTxtFileMode == "1") //采用0号规则或1号规则读取数据
  21. {
  22. //根据路径打开文件,并获取文件第一行数据,如果第一行数据是列名,则返回这些列名,如果不是,则返回F1,F2...
  23. List<string> arryList = new List<string>();
  24. if (File.Exists(strFilePath))
  25. {
  26. FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  27. StreamReader _StreamReaderKey = new StreamReader(fileStream, GetType(strFilePath));
  28. string strLine = "";
  29. /*这里只读取第一行数据*/
  30. strLine = _StreamReaderKey.ReadLine();
  31. arryList.Add(strLine);
  32. fileStream.Close();
  33. }
  34. if (arryList.Count >= 0)
  35. {
  36. string[] temp = arryList[0].Split('\t');
  37. if (strTxtFileMode == "0") //此时是文本模式1,此时第一行是数据列信息
  38. {
  39. for (int i = 0; i < temp.Length; i++)
  40. {
  41. dt.Columns.Add($"F{i + 1}");
  42. }
  43. }
  44. else if (strTxtFileMode == "1")//此时是文本模式2,此时第一行不是数据列信息,直接就是数据
  45. {
  46. foreach (string item in temp)
  47. {
  48. dt.Columns.Add(item);
  49. }
  50. }
  51. }
  52. }
  53. if (strTxtFileMode == "2") //此时是2号规则读取文件数据
  54. {
  55. dt = ReadFileSecond.ReadTableStructure();
  56. }
  57. if (strTxtFileMode == "3")
  58. {
  59. dt = ReadFileThird.ReadTableStructure();
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. AppLog.Error(ex.Message);
  65. }
  66. return dt;
  67. }
  68. public static DataTable ReadTxtFileData(string strFilePath, string strTxtFileMode)
  69. {
  70. DataTable dt = new DataTable();
  71. try
  72. {
  73. if (strTxtFileMode == "0" || strTxtFileMode == "1") //采用0号规则或1号规则读取数据
  74. {
  75. //根据路径打开文件,并获取文件第一行数据,如果第一行数据是列名,则返回这些列名,如果不是,则返回F1,F2...
  76. List<string> arryList = new List<string>();
  77. if (File.Exists(strFilePath))
  78. {
  79. FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  80. StreamReader _StreamReaderKey = new StreamReader(fileStream, GetType(strFilePath));
  81. string strLine = "";
  82. while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine())))
  83. {
  84. arryList.Add(strLine);
  85. }
  86. fileStream.Close();
  87. }
  88. if (arryList.Count >= 0)
  89. {
  90. //1 .构建表结构
  91. string[] temp = arryList[0].Split('\t');
  92. if (strTxtFileMode == "0") //此时是文本模式1,此时第一行是数据列信息
  93. {
  94. for (int i = 0; i < temp.Length; i++)
  95. {
  96. dt.Columns.Add($"F{++i}");
  97. }
  98. }
  99. else if (strTxtFileMode == "1")//此时是文本模式2,此时第一行不是数据列信息,直接就是数据
  100. {
  101. foreach (string item in temp)
  102. {
  103. dt.Columns.Add(item);
  104. }
  105. }
  106. //插入表数据
  107. int index = 0;
  108. for (int j = 0; j < arryList.Count; j++)
  109. {
  110. if (strTxtFileMode == "1" && j == 0)
  111. continue;
  112. index = 0;
  113. DataRow drNew = dt.NewRow();
  114. temp = arryList[j].Split('\t');
  115. foreach (var item in temp)
  116. {
  117. drNew[index++] = item;
  118. }
  119. dt.Rows.Add(drNew);
  120. }
  121. }
  122. }
  123. if (strTxtFileMode == "2") //此时是2号规则读取文件数据
  124. {
  125. dt = ReadFileSecond.ReadTableData(strFilePath);
  126. }
  127. if (strTxtFileMode == "3")
  128. {
  129. dt = ReadFileThird.ReadTableData(strFilePath);
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. AppLog.Error(ex.Message);
  135. }
  136. return dt;
  137. }
  138. #region 得到文本的编码格式,避免因解析问题造成的乱码
  139. public static System.Text.Encoding GetType(string FILE_NAME)
  140. {
  141. FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
  142. Encoding r = GetType(fs);
  143. fs.Close();
  144. return r;
  145. }
  146. public static System.Text.Encoding GetType(FileStream fs)
  147. {
  148. byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
  149. byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
  150. byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
  151. Encoding reVal = Encoding.Default;
  152. BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
  153. int i;
  154. int.TryParse(fs.Length.ToString(), out i);
  155. byte[] ss = r.ReadBytes(i);
  156. if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
  157. {
  158. reVal = Encoding.UTF8;
  159. }
  160. else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
  161. {
  162. reVal = Encoding.BigEndianUnicode;
  163. }
  164. else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
  165. {
  166. reVal = Encoding.Unicode;
  167. }
  168. r.Close();
  169. return reVal;
  170. }
  171. private static bool IsUTF8Bytes(byte[] data)
  172. {
  173. int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
  174. byte curByte; //当前分析的字节.
  175. for (int i = 0; i < data.Length; i++)
  176. {
  177. curByte = data[i];
  178. if (charByteCounter == 1)
  179. {
  180. if (curByte >= 0x80)
  181. {
  182. //判断当前
  183. while (((curByte <<= 1) & 0x80) != 0)
  184. {
  185. charByteCounter++;
  186. }
  187. //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
  188. if (charByteCounter == 1 || charByteCounter > 6)
  189. {
  190. return false;
  191. }
  192. }
  193. }
  194. else
  195. {
  196. //若是UTF-8 此时第一位必须为1
  197. if ((curByte & 0xC0) != 0x80)
  198. {
  199. return false;
  200. }
  201. charByteCounter--;
  202. }
  203. }
  204. if (charByteCounter > 1)
  205. {
  206. throw new Exception("非预期的byte格式");
  207. }
  208. return true;
  209. }
  210. #endregion
  211. }
  212. }