/// <summary> /// Считать из строки данные о присоединении трубы /// </summary> /// <param name="row"></param> /// <param name="currentWellData"></param> private void ReadPipeJunctionData(DataRow row, WellData currentWellData) { //номера присоединения может не быть у некоторых колодцев (напр у газовых коверов) //то есть все трубы пересекаются на одной отметке //ориентируемся на наличие данных хотябы в одном столбце string num = row[PIPE_JUNCTION_NUM_COL].ToString(); string material = row[PIPE_MATERIAL_COL].ToString(); double size = -1; double level = double.NegativeInfinity; string sizeStr = row[PIPE_SIZE_COL].ToString(); if (CONTAINS_NUMBERS.IsMatch(sizeStr)) { double.TryParse(sizeStr.Replace(',', '.'), out size); } string levelStr = row[PIPE_JUNCTION_LEVEL_COL].ToString(); if (CONTAINS_NUMBERS.IsMatch(levelStr)) { double.TryParse(levelStr.Replace(',', '.'), out level); } if (!String.IsNullOrEmpty(num) || !String.IsNullOrEmpty(material) || size >= 0 || level >= 0) { string key = !String.IsNullOrEmpty(num) ? num : "0";//в колодце может быть только одно присоединение без номера if (!currentWellData.PipeJunctions.ContainsKey(key)) { currentWellData.PipeJunctions.Add(key, new PipeJunctionData() { Num = num, Material = material, Size = size, JunctionLevel = level }); } } }
public bool ReadDataFromExcel() { foreach (FileInfo fi in WellDataFiles) { FileStream fs = null; try { fs = File.Open(fi.FullName, FileMode.Open, FileAccess.Read); } catch (System.IO.IOException ex) { AccessException(ex); return(false); } if (fs != null) { IExcelDataReader reader = null; switch (fi.Extension) { case ".xls": reader = ExcelReaderFactory.CreateBinaryReader(fs); break; case ".xlsx": reader = ExcelReaderFactory.CreateOpenXmlReader(fs); break; //TODO?: можно добавить еще CSV } if (reader != null) { //Определить все видимые листы HashSet <string> visibleSheets = new HashSet <string>(); for (var i = 0; i < reader.ResultsCount; i++) { // checking visible state if (reader.VisibleState == "visible") { visibleSheets.Add(reader.Name); } reader.NextResult(); } DataSet result = reader.AsDataSet(); string str = reader.VisibleState; reader.Close(); int key = Convert.ToInt32(Path.GetFileNameWithoutExtension(fi.FullName) .Replace("-", "").Replace("_", "").Replace(" ", "")); if (!WellsData.ContainsKey(key)) { Dictionary <string, WellData> thisFileDict = new Dictionary <string, WellData>(); WellsData.Add(key, thisFileDict); foreach (DataTable table in result.Tables) { //если таблица скрыта, то не трогать ее if (!visibleSheets.Contains(table.TableName)) { continue; } int colNum = table.Columns.Count; //int skipped = 0; bool headerSkipped = false; WellData currentWellData = null; foreach (DataRow row in table.Rows) { //Пропустить нужное количество строк с начала таблицы //if (skipped < SKIP_ROWS_COUNT) //{ // skipped++; // continue; //} //считать, что последняя строка шапки таблицы содержит занчения 1, 2, 3, 4...12 if (!headerSkipped) { if (DataRowIsHeaderEnd(row, colNum)) { headerSkipped = true; } continue; } string wellNum = row[WELL_NUM_COL].ToString(); if (!String.IsNullOrEmpty(wellNum)) { if (!thisFileDict.ContainsKey(wellNum)) { string sizeStr = row[SIZE1_COL].ToString(); double size1 = -1; double size2 = -1; double topLevel = double.NegativeInfinity; double bottomLevel = double.NegativeInfinity; double.TryParse(sizeStr, out size1); double.TryParse(row[SIZE2_COL].ToString(), out size2); string topLevelStr = row[WELL_TOP_LEVEL_COL].ToString(); if (CONTAINS_NUMBERS.IsMatch(topLevelStr))//текст должен содержать хотябы одну цифру (иногда там пишут прочерки или что-то такое) { double.TryParse(topLevelStr.Replace(',', '.'), out topLevel); } string bottomLevelStr = row[WELL_BOTTOM_LEVEL_COL].ToString(); if (CONTAINS_NUMBERS.IsMatch(bottomLevelStr)) { double.TryParse(bottomLevelStr.Replace(',', '.'), out bottomLevel); } currentWellData = new WellData() { Num = wellNum, NetworkType = row[NETWORK_TYPE_COL].ToString(), SizeString = sizeStr, Size1 = size1, Size2 = size2, Material = row[WELL_MATERIAL_COL].ToString(), TopLevel = topLevel, BottomLevel = bottomLevel, }; thisFileDict.Add(wellNum, currentWellData); ReadPipeJunctionData(row, currentWellData); } } else if (currentWellData != null) { //добавление нового присоединения к данным колодца ReadPipeJunctionData(row, currentWellData); } } } } } } } return(true); }