CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.3KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchrousModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. namespace CnasSynchronusDAL
  10. {
  11. public class TXTDAL
  12. {
  13. /// <summary>
  14. /// 获取表名表结构
  15. /// </summary>
  16. public static DataTable ReadTxtFileColumn(TXTOpenParams t)
  17. {
  18. DataTable dt = new DataTable();
  19. try
  20. {
  21. switch (t.TxtFileMode)
  22. {
  23. case "1":
  24. ReadFileCommon01.SplitType = t.SplitType;
  25. ReadFileCommon01.StartLineIndex = t.StartLineIndex;
  26. ReadFileCommon01.IfCustomCreateDateColumn = t.IfCustomCreateDateColumn;
  27. ReadFileCommon01.Suffix = t.Suffix;
  28. ReadFileCommon01.FileNameFormat = t.FileNameFormat;
  29. dt = ReadFileCommon01.ReadTableStructure(t.StrPath);
  30. break;
  31. case "2":
  32. dt = ReadFileSecond.ReadTableStructure();
  33. break;
  34. case "3":
  35. dt = ReadFileThirdth.ReadTableStructure();
  36. break;
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. AppLog.Error(ex.Message);
  42. }
  43. return dt;
  44. }
  45. public static DataTable ReadTxtFileData(TXTOpenParams t)
  46. {
  47. DataTable dt = new DataTable();
  48. try
  49. {
  50. switch (t.TxtFileMode)
  51. {
  52. case "1":
  53. ReadFileCommon01.SplitType = t.SplitType;
  54. ReadFileCommon01.StartLineIndex = t.StartLineIndex;
  55. ReadFileCommon01.IfCustomCreateDateColumn = t.IfCustomCreateDateColumn;
  56. ReadFileCommon01.Suffix = t.Suffix;
  57. ReadFileCommon01.FileNameFormat = t.FileNameFormat;
  58. dt = ReadFileCommon01.ReadTableData(t.StrPath);
  59. break;
  60. case "2":
  61. dt = ReadFileSecond.ReadTableData(t.StrPath);
  62. break;
  63. case "3":
  64. dt = ReadFileThirdth.ReadTableData(t.StrPath);
  65. break;
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. AppLog.Error(ex.Message);
  71. }
  72. return dt;
  73. }
  74. #region 得到文本的编码格式,避免因解析问题造成的乱码
  75. public static System.Text.Encoding GetType(string FILE_NAME)
  76. {
  77. FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
  78. Encoding r = GetType(fs);
  79. fs.Close();
  80. return r;
  81. }
  82. public static System.Text.Encoding GetType(FileStream fs)
  83. {
  84. byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
  85. byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
  86. byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
  87. Encoding reVal = Encoding.Default;
  88. BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
  89. int i;
  90. int.TryParse(fs.Length.ToString(), out i);
  91. byte[] ss = r.ReadBytes(i);
  92. if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
  93. {
  94. reVal = Encoding.UTF8;
  95. }
  96. else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
  97. {
  98. reVal = Encoding.BigEndianUnicode;
  99. }
  100. else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
  101. {
  102. reVal = Encoding.Unicode;
  103. }
  104. r.Close();
  105. return reVal;
  106. }
  107. private static bool IsUTF8Bytes(byte[] data)
  108. {
  109. int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
  110. byte curByte; //当前分析的字节.
  111. for (int i = 0; i < data.Length; i++)
  112. {
  113. curByte = data[i];
  114. if (charByteCounter == 1)
  115. {
  116. if (curByte >= 0x80)
  117. {
  118. //判断当前
  119. while (((curByte <<= 1) & 0x80) != 0)
  120. {
  121. charByteCounter++;
  122. }
  123. //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
  124. if (charByteCounter == 1 || charByteCounter > 6)
  125. {
  126. return false;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. //若是UTF-8 此时第一位必须为1
  133. if ((curByte & 0xC0) != 0x80)
  134. {
  135. return false;
  136. }
  137. charByteCounter--;
  138. }
  139. }
  140. if (charByteCounter > 1)
  141. {
  142. throw new Exception("非预期的byte格式");
  143. }
  144. return true;
  145. }
  146. #endregion
  147. }
  148. }