using System; using System.Collections.Generic; using System.Data; using System.IO; using System.IO.MemoryMappedFiles; using System.Text; using WorkData.Entity; #region 文件说明 /*----------------------------------------------------- * 版权所有 (c) 2024 jdfcd * CLR版本 4.0.30319.42000 * 命名空间 DQ.Construction.NewLook.Utility * * 创建者:pnpe * 电子邮件:pnpe@qq.com * 创建时间:2024/12/31 10:38:45 */ #endregion 文件说明 namespace WellWorkDataUI { public class ResultDataInfo { public int Code { get; set; } public string Msg { get; set; } public object Data { get; set; } } public class LasHelper { //读取las public static ResultDataInfo LasFileToDataTable(ref DataTable table, string filename, string charset, int rowindex) { ResultDataInfo result = new ResultDataInfo(); List columnNames = new List(); try { long length = new FileInfo(filename).Length; using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(filename, FileMode.Open, Guid.NewGuid().ToString())) using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, length)) using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(charset))) { int lineindex = 0; bool isReadHeader = false; bool isReadBody = false; string strLine = string.Empty; while (!reader.EndOfStream) { string str = reader.ReadLine(); if (string.IsNullOrWhiteSpace(str)) { continue; } else if (str.StartsWith("~C") || str.StartsWith("~Curve")) { isReadHeader = true; continue; } else if (!isReadHeader) { continue; } else if (str.StartsWith("~A") || str.StartsWith("~Ascii")) { isReadBody = true; table.Columns.Clear(); if (columnNames.Count == 0) { result.Code = 0; result.Msg = $"文件{filename}列读取失败,已跳过文件,{Environment.NewLine}"; return result; } //说明表头写完了 for (int i = 0; i < columnNames.Count; i++) { table.Columns.Add(columnNames[i]); } continue; } if (isReadHeader && !isReadBody) { //判断表头,对于小数点处理和冒号处理 string colName = str.Replace(":", string.Empty); colName = colName.Split('.')[0]; if (colName.StartsWith("#")) { continue; } colName = colName.Trim().ToUpper(); if (!columnNames.Contains(colName)) { columnNames.Add(colName); } else { //存在重复列名 table.Columns.Clear(); result.Code = 0; result.Msg = $"文件{filename}存在重复列名{colName},已跳过文件,{Environment.NewLine}"; return result; } continue; } strLine = str; string trimString = string.Join(",", strLine.Split(Constants.StringSplit.EmptySeparator, StringSplitOptions.RemoveEmptyEntries)); string[] array = trimString.Split(Constants.StringSplit.Separator, StringSplitOptions.None); if (array.Length != columnNames.Count) { continue; } if (array.Length > 0) { table.Rows.Add(array); } lineindex++; } } } catch (Exception e) { result.Code = 0; result.Msg = e.Message; Console.WriteLine(e.ToString()); } result.Code = 1; return result; } /// /// las文件转List /// public static IEnumerable LasFileToList(string jh, string filename, string charset) { if (!File.Exists(filename)) { yield break; } using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(filename, FileMode.Open, Guid.NewGuid().ToString())) using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, new FileInfo(filename).Length)) using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(charset))) { List columnNames = new List(); bool isReadHeader = false; bool isReadBody = false; while (!reader.EndOfStream) { string str = reader.ReadLine(); if (string.IsNullOrWhiteSpace(str)) { continue; } if (str.StartsWith("~C") || str.StartsWith("~Curve")) { isReadHeader = true; continue; } else if (!isReadHeader) { continue; } else if (str.StartsWith("~A") || str.StartsWith("~Ascii")) { isReadBody = true; if (columnNames.Count == 0) { throw new Exception($"文件{filename} 列名为空!"); } continue; } if (isReadHeader && !isReadBody) { string colName = str.Replace(":", string.Empty); colName = colName.Split('.')[0]; if (colName.StartsWith("#")) { continue; } colName = colName.Trim().ToUpper(); if (!columnNames.Contains(colName)) { columnNames.Add(colName); } else { throw new Exception($"文件{filename} 存在重复列名:{colName}"); } continue; } // 数据体 string trimString = string.Join(",", str.Split(Constants.StringSplit.EmptySeparator, StringSplitOptions.RemoveEmptyEntries)); string[] array = trimString.Split(Constants.StringSplit.Separator, StringSplitOptions.None); if (array.Length != columnNames.Count) { continue; } if (!double.TryParse(array[0], out double depth)) { continue; } var dic = new Dictionary(); for (int i = 1; i < array.Length; i++) { dic[columnNames[i]] = array[i]; } yield return new WellCurve { JH = jh, DEPTH = depth, ColumnValues = dic, }; } } } } }