CNAS取数仪器端升级
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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