CNAS取数仪器端升级
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

261 líneas
10KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusDAL;
  3. using CnasSynchronusIDAL;
  4. using CnasSynchrousModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. namespace CnasSynchronusClient
  12. {
  13. public class ExcelInstrumentData : InstrumentData
  14. {
  15. public string StrLocalPath { get; set; }
  16. public string strTableName { get; set; }
  17. public string strDateColumn { get; set; }
  18. public string strDate { get; set; }
  19. public IExcelService dataBaseService { get { return new ExcelDBService(); } }
  20. public ExcelFormatConfig ExcelFormat { get; set; }
  21. /// <summary>
  22. /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径
  23. /// </summary>
  24. public List<string> LstFileFullName { get; set; }
  25. public ExcelInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs)
  26. {
  27. if (dataSourceInfo.Path == null)
  28. {
  29. return;
  30. }
  31. this.ExcelFormat = FileOperation.GetFormatConfigData<ExcelFormatConfig>("ExcelFormatConfig.xml");
  32. try
  33. {
  34. LstFileFullName = new List<string>();
  35. // 此时为读取“文件”模式,指定要读取的具体文件
  36. if (ExcelFormat.ReadExcelFileMode == "0")
  37. {
  38. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  39. remoteFileCopy.CopyFileFromRemote(".xls");
  40. this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".xls";
  41. this.strTableName = vs[0].ToString();
  42. this.strDateColumn = vs[1].ToString();
  43. this.strDate = vs[2].ToString();
  44. LstFileFullName.Add(StrLocalPath);
  45. }
  46. // 此时为读取“文件夹”模式,指定要读取的具体文件
  47. else
  48. {
  49. DirectoryInfo root = new DirectoryInfo($"{dataSourceInfo.Path}");
  50. foreach (FileInfo f in root.GetFiles())
  51. {
  52. string name = f.Name;
  53. string fullName = f.FullName;
  54. string type = f.Extension;
  55. //“今天”产生的数据,并且格式是xls
  56. if ((type.ToLower() == ".xls".ToLower() || type.ToLower() == ".xlsx".ToLower()) &&
  57. name.StartsWith(DateTime.Now.ToString(ExcelFormat.ExcelFileNameFormat)))
  58. {
  59. RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo);
  60. remoteFileCopy.CopyFileFromRemote(".xls", $"{name}");
  61. LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + $"{name}", "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".xls");
  62. }
  63. }
  64. this.strTableName = vs[0].ToString();
  65. this.strDateColumn = vs[1].ToString();
  66. this.strDate = vs[2].ToString();
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. AppLog.Error(ex.Message);
  72. }
  73. }
  74. public override Dictionary<string, DataTable> GetInstrumentData()
  75. {
  76. Dictionary<string, DataTable> excels = new Dictionary<string, DataTable>();
  77. //读取数据
  78. foreach (var item in LstFileFullName)
  79. {
  80. try
  81. {
  82. Dictionary<string, DataTable> datas = dataBaseService.GetInstrumentData(
  83. new ExcelOpenParams
  84. {
  85. StrPath = item,
  86. ExcelFileVersion = ExcelFormat.ExcelFileVersion,
  87. autoSql = ExcelFormat.AutoSql
  88. });
  89. foreach (var key in datas.Keys)
  90. {
  91. if (false == excels.ContainsKey(key))
  92. {
  93. excels.Add(key, datas[key]);
  94. }
  95. else
  96. {
  97. foreach (DataRow dr in datas[key].Rows)
  98. {
  99. DataRow drNew = excels[key].NewRow();
  100. drNew.ItemArray = dr.ItemArray;
  101. excels[key].Rows.Add(drNew);
  102. }
  103. }
  104. }
  105. }
  106. catch
  107. {
  108. }
  109. }
  110. return excels;
  111. }
  112. public override DataTable GetInstrumentDataByDate()
  113. {
  114. DataTable dtCompine = new DataTable();
  115. try
  116. {
  117. //读取数据
  118. List<DataTable> dataTables = new List<DataTable>();
  119. foreach (var item in LstFileFullName)
  120. {
  121. DataTable dt = dataBaseService.GetInstrumentDataByDate(
  122. new ExcelOpenParams
  123. {
  124. StrPath = item,
  125. ExcelFileVersion = ExcelFormat.ExcelFileVersion,
  126. autoSql = ExcelFormat.AutoSql
  127. },
  128. new ConditionParams
  129. {
  130. TableName = strTableName,
  131. DateColumn = strDateColumn,
  132. DateValue = strDate
  133. });
  134. dataTables.Add(dt);
  135. }
  136. //合并数据
  137. if (dataTables.Count > 0)
  138. {
  139. dtCompine = dataTables[0].Clone();
  140. foreach (DataTable dt1 in dataTables)
  141. {
  142. foreach (DataRow dr in dt1.Rows)
  143. {
  144. DataRow drNew = dtCompine.NewRow();
  145. foreach (DataColumn dc in dtCompine.Columns)
  146. {
  147. try
  148. {
  149. if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower())
  150. {
  151. DateTime dt = DateTime.Now;
  152. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  153. {
  154. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  155. }
  156. else
  157. {
  158. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), ExcelFormat.ExcelFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  159. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  160. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  161. else
  162. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  163. }
  164. }
  165. else
  166. {
  167. drNew[dc.ColumnName] = dr[dc.ColumnName];
  168. }
  169. }
  170. catch
  171. {
  172. }
  173. }
  174. dtCompine.Rows.Add(drNew);
  175. }
  176. }
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. AppLog.Info("读取数据时发生异常");
  182. AppLog.Error(ex.Message);
  183. }
  184. AppLog.Info("读取数据完成,准备返回");
  185. return dtCompine;
  186. }
  187. public override DataTable GetInstrumentDataStruct()
  188. {
  189. Dictionary<string, DataTable> excels = new Dictionary<string, DataTable>();
  190. //读取数据
  191. foreach (var item in LstFileFullName)
  192. {
  193. try
  194. {
  195. Dictionary<string, DataTable> datas = dataBaseService.GetInstrumentData(
  196. new ExcelOpenParams
  197. {
  198. StrPath = item,
  199. ExcelFileVersion = ExcelFormat.ExcelFileVersion,
  200. autoSql = ExcelFormat.AutoSql
  201. });
  202. foreach (var key in datas.Keys)
  203. {
  204. if (false == excels.ContainsKey(key))
  205. {
  206. excels.Add(key, datas[key]);
  207. }
  208. else
  209. {
  210. foreach (DataRow dr in datas[key].Rows)
  211. {
  212. DataRow drNew = excels[key].NewRow();
  213. drNew.ItemArray = dr.ItemArray;
  214. excels[key].Rows.Add(drNew);
  215. }
  216. }
  217. }
  218. }
  219. catch
  220. {
  221. }
  222. }
  223. return (0 < excels.Count) ? excels.First().Value : null;
  224. }
  225. public override DataTable GetInstrumentDataStruct(string Tablename)
  226. {
  227. throw new NotImplementedException();
  228. }
  229. }
  230. }