using FileDataUpload.Common; using FileDataUpload.Entity; using MiniExcelLibs; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace FileDataUpload.FileOperate { public sealed class FileHelper { private int paramBeginRowNumber = 6; private readonly JsonChange jsonChange = JsonChange.Instance; private readonly LogHelper logHelper = LogHelper.Instance; private static readonly Lazy lazy = new Lazy(() => new FileHelper()); public static FileHelper Instance { get { return lazy.Value; } } private FileHelper() { } [Obsolete] public Dictionary> ReadExcelFile_New(string filePath) { try { Dictionary> keyValues = new Dictionary>(); var sheetNames = MiniExcel.GetSheetNames(filePath); foreach (var sheetName in sheetNames) { List deviceParams = new List(); DeviceParam parameterInfo = new DeviceParam(); List info = MiniExcel.Query(filePath, sheetName: sheetName).ToList(); if (info != null && info.Count() > paramBeginRowNumber) { for(int i=8; i<16; i++) { DeviceParam deviceParam = new DeviceParam(); deviceParam.id = System.Guid.NewGuid().ToString("N"); ParamData param = new ParamData(); param.deviceCode = "EVtest"; param.propertyCode = info[i].C; param.dataType = GetDataType(info[16].F == null ? "Text" : info[16].F); param.propertyValue = info[i].E; param.time = GetTimeStamp(); deviceParam.data = param; deviceParams.Add(deviceParam); } DeviceParam deviceParam_17 = new DeviceParam(); deviceParam_17.id = System.Guid.NewGuid().ToString("N"); ParamData param_17 = new ParamData(); param_17.deviceCode = "EVtest"; param_17.propertyCode = info[17].C; param_17.dataType = 5; List propertyValues = new List(); //tag9 监测明细 for (int i = 17; i < info.Count();) { if (string.IsNullOrEmpty((string)info[i].D)) { i = i + 6; continue; } else { PropertyValue propertyValue = new PropertyValue(); propertyValue.CHECK_ITEM = MidStrEx_New((string)info[i].D, "-", "-"); propertyValue.ITEM_VALUE = info[i].E == null ? " " : string.Format("{0}",info[i].E); propertyValue.TEST_STATUS = info[i + 1].E == null ? " " : string.Format("{0}", info[i + 1].E); propertyValue.STANDARDVALUE = info[i + 2].E == null ? " " : string.Format("{0}", info[i + 2].E); propertyValue.STANDARDMAX = info[i + 3].E == null ? " " : string.Format("{0}", info[i + 3].E); propertyValue.STANDARDMIN = info[i + 4].E == null ? " " : string.Format("{0}", info[i + 4].E); propertyValue.TEST_DETAIL_ID = System.Guid.NewGuid().ToString("N"); //空值处理 propertyValue.DEVICE_NAME = ""; propertyValue.ACCURACYVALUE = ""; propertyValue.UOM = ""; propertyValues.Add(propertyValue); i = i + 6; } } param_17.propertyValue = jsonChange.ModeToJson(propertyValues); param_17.time = GetTimeStamp(); deviceParam_17.data = param_17; deviceParams.Add(deviceParam_17); //推送标志位 DeviceParam deviceParam_16 = new DeviceParam(); deviceParam_16.id = System.Guid.NewGuid().ToString("N"); ParamData param_16 = new ParamData(); param_16.deviceCode = "EVtest"; param_16.propertyCode = info[16].C; param_16.dataType = GetDataType(info[16].F == null ? "Text" : info[16].F); param_16.propertyValue = info[16].E == null ? "":info[16].E; param_16.time = GetTimeStamp(); deviceParam_16.data = param_16; deviceParams.Add(deviceParam_16); } keyValues.Add(sheetName, deviceParams); } return keyValues; } catch (Exception ex) { logHelper.Error("文件解析异常", ex); return null; } } [Obsolete] public List ReadExcelFile(string filePath) { try { List result = new List(); var sheetNames = MiniExcel.GetSheetNames(filePath); foreach (var sheetName in sheetNames) { DeviceParameterInfo parameterInfo = new DeviceParameterInfo(); List info = MiniExcel.Query(filePath, sheetName: sheetName).ToList(); var rows = MiniExcel.Query(filePath, sheetName: sheetName); //避免读取数据为空的工作表 if (info != null && info.Count() > paramBeginRowNumber) { //获取前两行参数:公司名称B、工件名称E、工件编号B、操作人员E parameterInfo.companyName = info[0].B; parameterInfo.partName = info[0].E; parameterInfo.partNumber = info[1].B; parameterInfo.operatingPersonnel = info[2].E; List values = new List(); var deviceParams = info.GetRange(paramBeginRowNumber, info.Count() - paramBeginRowNumber); if (deviceParams == null) continue; foreach (var item in deviceParams) { DeviceParametersValue parametersValue = new DeviceParametersValue(); if (item.A as string == null) continue; parametersValue.paramNumber = item.A as string; parametersValue.paramContent = item.B as string; parametersValue.observedValue = item.C == null ? 0 : item.C; parametersValue.nominalValue = item.D == null ? 0 : item.D; parametersValue.overproofValue = item.E == null ? 0 : item.E; parametersValue.upperDeviation = item.F == null ? 0 : item.F; parametersValue.lowerDeviation = item.G == null ? 0 : item.G; parametersValue.state = item.H as string; values.Add(parametersValue); } parameterInfo.deviceParametersValues = values; } else { continue; } result.Add(parameterInfo); } return result; } catch(Exception ex) { throw new Exception(ex.Message); } } /// /// 获取时间戳 /// /// private string GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalMilliseconds).ToString(); } /// /// 字符串截取 /// /// /// /// /// private string MidStrEx_New(string sourse, string startstr, string endstr) { Regex rg = new Regex("(?<=(" + startstr + "))[.\\s\\S]*?(?=(" + endstr + "))", RegexOptions.Multiline | RegexOptions.Singleline); return rg.Match(sourse).Value; } /// /// 数据类型 /// /// /// private int GetDataType(string str) { int result = 5; switch (str) { case "Int32": result = 0; break; case "Float": result = 1; break; case "Double": result = 2; break; case "Bool": result = 3; break; case "Enum": result = 4; break; case "Text": result = 5; break; case "Time": result = 6; break; case "Array": result = 7; break; case "Struct": result = 8; break; default: result = 5; break; } return result; } } }