using CnasSynchronousCommon; using CnasSynchronusDAL; using CnasSynchronusIDAL; using CnasSynchrousModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; namespace CnasSynchronusClient { public class ExcelInstrumentData : InstrumentData { public string StrLocalPath { get; set; } public string strTableName { get; set; } public string strDateColumn { get; set; } public string strDate { get; set; } public IExcelService dataBaseService { get { return new ExcelDBService(); } } public ExcelFormatConfig ExcelFormat { get; set; } /// /// 如果配置读取的是文件夹,该属性用来记录需要读取所有文件的缓冲路径 /// public List LstFileFullName { get; set; } public ExcelInstrumentData(InstrumentDataSourceInfo dataSourceInfo, object[] vs) { if (dataSourceInfo.Path == null) { return; } this.ExcelFormat = FileOperation.GetFormatConfigData("ExcelFormatConfig.xml"); try { LstFileFullName = new List(); // 此时为读取“文件”模式,指定要读取的具体文件 if (ExcelFormat.ReadExcelFileMode == "0") { RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo); remoteFileCopy.CopyFileFromRemote(".xls"); this.StrLocalPath = FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".xls"; this.strTableName = vs[0].ToString(); this.strDateColumn = vs[1].ToString(); this.strDate = vs[2].ToString(); LstFileFullName.Add(StrLocalPath); } // 此时为读取“文件夹”模式,指定要读取的具体文件 else { DirectoryInfo root = new DirectoryInfo($"{dataSourceInfo.Path}"); foreach (FileInfo f in root.GetFiles()) { string name = f.Name; string fullName = f.FullName; string type = f.Extension; //“今天”产生的数据,并且格式是xls if ((type.ToLower() == ".xls".ToLower() || type.ToLower() == ".xlsx".ToLower()) && name.StartsWith(DateTime.Now.ToString(ExcelFormat.ExcelFileNameFormat))) { RemoteFileCopy remoteFileCopy = new RemoteFileCopy(dataSourceInfo); remoteFileCopy.CopyFileFromRemote(".xls", $"{name}"); LstFileFullName.Add(FileHelper.getBasePath() + "\\Cache\\" + System.Text.RegularExpressions.Regex.Replace(dataSourceInfo.Path + $"{name}", "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + ".xls"); } } this.strTableName = vs[0].ToString(); this.strDateColumn = vs[1].ToString(); this.strDate = vs[2].ToString(); } } catch (Exception ex) { AppLog.Error(ex.Message); } } public override Dictionary GetInstrumentData() { Dictionary excels = new Dictionary(); //读取数据 foreach (var item in LstFileFullName) { try { Dictionary datas = dataBaseService.GetInstrumentData( new ExcelOpenParams { StrPath = item, ExcelFileVersion = ExcelFormat.ExcelFileVersion, autoSql = ExcelFormat.AutoSql }); foreach (var key in datas.Keys) { if (false == excels.ContainsKey(key)) { excels.Add(key, datas[key]); } else { foreach (DataRow dr in datas[key].Rows) { DataRow drNew = excels[key].NewRow(); drNew.ItemArray = dr.ItemArray; excels[key].Rows.Add(drNew); } } } } catch { } } return excels; } public override DataTable GetInstrumentDataByDate() { DataTable dtCompine = new DataTable(); try { //读取数据 List dataTables = new List(); foreach (var item in LstFileFullName) { DataTable dt = dataBaseService.GetInstrumentDataByDate( new ExcelOpenParams { StrPath = item, ExcelFileVersion = ExcelFormat.ExcelFileVersion, autoSql = ExcelFormat.AutoSql }, new ConditionParams { TableName = strTableName, DateColumn = strDateColumn, DateValue = strDate }); dataTables.Add(dt); } //合并数据 if (dataTables.Count > 0) { dtCompine = dataTables[0].Clone(); foreach (DataTable dt1 in dataTables) { foreach (DataRow dr in dt1.Rows) { DataRow drNew = dtCompine.NewRow(); foreach (DataColumn dc in dtCompine.Columns) { try { if (dc.ColumnName.ToString().ToLower() == strDateColumn.ToLower()) { DateTime dt = DateTime.Now; if (DateTime.TryParse(dr[dc.ColumnName].ToString(), out dt)) { drNew[dc.ColumnName] = dr[dc.ColumnName].ToString(); } else { DateTime dtTime = DateTime.ParseExact(dr[dc.ColumnName].ToString(), ExcelFormat.ExcelFileDateColumnFormat, new System.Globalization.CultureInfo("zh-CN", true)); if (dtTime.ToString("HH:mm:ss") == "00:00:00") drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd"); else drNew[dc.ColumnName] = dtTime.ToString("yyyy-MM-dd HH:mm:ss"); } } else { drNew[dc.ColumnName] = dr[dc.ColumnName]; } } catch { } } dtCompine.Rows.Add(drNew); } } } } catch (Exception ex) { AppLog.Info("读取数据时发生异常"); AppLog.Error(ex.Message); } AppLog.Info("读取数据完成,准备返回"); return dtCompine; } public override DataTable GetInstrumentDataStruct() { Dictionary excels = new Dictionary(); //读取数据 foreach (var item in LstFileFullName) { try { Dictionary datas = dataBaseService.GetInstrumentData( new ExcelOpenParams { StrPath = item, ExcelFileVersion = ExcelFormat.ExcelFileVersion, autoSql = ExcelFormat.AutoSql }); foreach (var key in datas.Keys) { if (false == excels.ContainsKey(key)) { excels.Add(key, datas[key]); } else { foreach (DataRow dr in datas[key].Rows) { DataRow drNew = excels[key].NewRow(); drNew.ItemArray = dr.ItemArray; excels[key].Rows.Add(drNew); } } } } catch { } } return (0 < excels.Count) ? excels.First().Value : null; } } }