CNAS取数仪器端升级
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

262 řádky
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. if (LstFileFullName == null)
  78. {
  79. return excels;
  80. }
  81. //读取数据
  82. foreach (var item in LstFileFullName)
  83. {
  84. try
  85. {
  86. Dictionary<string, DataTable> datas = dataBaseService.GetInstrumentData(
  87. new ExcelOpenParams
  88. {
  89. StrPath = item,
  90. ExcelFileVersion = ExcelFormat.ExcelFileVersion,
  91. autoSql = ExcelFormat.AutoSql
  92. });
  93. foreach (var key in datas.Keys)
  94. {
  95. if (false == excels.ContainsKey(key))
  96. {
  97. excels.Add(key, datas[key]);
  98. }
  99. else
  100. {
  101. foreach (DataRow dr in datas[key].Rows)
  102. {
  103. DataRow drNew = excels[key].NewRow();
  104. drNew.ItemArray = dr.ItemArray;
  105. excels[key].Rows.Add(drNew);
  106. }
  107. }
  108. }
  109. }
  110. catch
  111. {
  112. }
  113. }
  114. return excels;
  115. }
  116. public override DataTable GetInstrumentDataByDate()
  117. {
  118. DataTable dtCompine = new DataTable();
  119. try
  120. {
  121. //读取数据
  122. List<DataTable> dataTables = new List<DataTable>();
  123. foreach (var item in LstFileFullName)
  124. {
  125. DataTable dt = dataBaseService.GetInstrumentDataByDate(
  126. new ExcelOpenParams
  127. {
  128. StrPath = item,
  129. ExcelFileVersion = ExcelFormat.ExcelFileVersion,
  130. autoSql = ExcelFormat.AutoSql
  131. },
  132. new ConditionParams
  133. {
  134. TableName = strTableName,
  135. DateColumn = strDateColumn,
  136. DateValue = strDate
  137. });
  138. dataTables.Add(dt);
  139. }
  140. //合并数据
  141. if (dataTables.Count > 0)
  142. {
  143. dtCompine = dataTables[0].Clone();
  144. foreach (DataTable dt1 in dataTables)
  145. {
  146. foreach (DataRow dr in dt1.Rows)
  147. {
  148. DataRow drNew = dtCompine.NewRow();
  149. foreach (DataColumn dc in dtCompine.Columns)
  150. {
  151. try
  152. {
  153. if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower())
  154. {
  155. DateTime dt = DateTime.Now;
  156. if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt))
  157. {
  158. drNew[dc.ColumnName] = dr[dc.ColumnName].ToString();
  159. }
  160. else
  161. {
  162. DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), ExcelFormat.ExcelFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true));
  163. if (dtTime.ToString("HH:mm:ss") == "00:00:00")
  164. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd");
  165. else
  166. drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss");
  167. }
  168. }
  169. else
  170. {
  171. drNew[dc.ColumnName] = dr[dc.ColumnName];
  172. }
  173. }
  174. catch
  175. {
  176. }
  177. }
  178. dtCompine.Rows.Add(drNew);
  179. }
  180. }
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. AppLog.Info("读取数据时发生异常");
  186. AppLog.Error(ex.Message);
  187. }
  188. AppLog.Info("读取数据完成,准备返回");
  189. return dtCompine;
  190. }
  191. public override DataTable GetInstrumentDataStruct()
  192. {
  193. Dictionary<string, DataTable> excels = new Dictionary<string, DataTable>();
  194. if (LstFileFullName == null)
  195. {
  196. return null;
  197. }
  198. //读取数据
  199. foreach (var item in LstFileFullName)
  200. {
  201. try
  202. {
  203. Dictionary<string, DataTable> datas = dataBaseService.GetInstrumentData(
  204. new ExcelOpenParams
  205. {
  206. StrPath = item,
  207. ExcelFileVersion = ExcelFormat.ExcelFileVersion,
  208. autoSql = ExcelFormat.AutoSql
  209. });
  210. foreach (var key in datas.Keys)
  211. {
  212. if (false == excels.ContainsKey(key))
  213. {
  214. excels.Add(key, datas[key]);
  215. }
  216. else
  217. {
  218. foreach (DataRow dr in datas[key].Rows)
  219. {
  220. DataRow drNew = excels[key].NewRow();
  221. drNew.ItemArray = dr.ItemArray;
  222. excels[key].Rows.Add(drNew);
  223. }
  224. }
  225. }
  226. }
  227. catch
  228. {
  229. }
  230. }
  231. return (0 < excels.Count) ? excels.First().Value : null;
  232. }
  233. }
  234. }