using DbfDataReader;
using DevExpress.Spreadsheet;
//using DfdIO.Elements;
//using DQ.Construction.NewLook.CustomControls;
using MiniExcelLibs;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
//using ParameterDQ.Construction.Util;
using SqlSugar;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WellWorkDataUI;
using WellWorkDataUI.CustomControls;
using WorkData;
using WorkData.Entity;
using WorkData.EntityBase;
//using static ParameterDQ.Construction.Util.ParameterDataWriter;
using IRow = NPOI.SS.UserModel.IRow;
using Path = System.IO.Path;
using TableInfo = WorkData.Entity.TableInfo;
namespace DQ.Construction.NewLook.DataManager
{
///
/// ImportManager
///
internal class ImportManager
{
///
/// /// 执行次数,用于井斜分批导入时验证
///
private int times = 0;
/// The preview line count.
///
private const int PreviewLineCount = 200;
public string errorMessage = string.Empty;
private int totalDuplicate = 0;
private string currentFileName;
private IImportDataUI ui;
private IImportDataCfg cfg;
private int importNum = 0;
private string importTips = string.Empty;
///
/// Initializes a new instance of the class.
///
/// window
public ImportManager(IImportDataUI ui)
{
this.ui = ui;
if (ui is IImportDataCfg cfg)
{
this.cfg = cfg;
}
}
#region 属性
///
/// IsDataImported
///
public bool IsDataImported { get; private set; } = false;
///
/// PreviewCache
///
public PreviewCache PreviewCache { get; private set; } = new PreviewCache();
///
/// RecordCount
///
public int RecordCount { get; private set; } = 0;
#endregion
///
/// Gets the start i d.
///
/// An int.
private int GetStartID()
{
return DBHelp.Db.Queryable().Max("id") + 1;
}
///
/// Gets the start i d.
///
/// An int.
private int GetStartXHID(string key)
{
return DBHelp.Db.Queryable().Where("jh=" + key).Max("id") + 1;
}
///
/// 导入数据
///
/// cfg
/// task
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "blabla")]
public async Task ImportTable(IImportDataCfg cfg)
{
this.cfg = cfg ?? this.cfg;
using (SplashHelper.Create())
{
if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckInputData, (table, matches, i, start) => Save(table, matches, i, start, this.SaveDataWellBase, false));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckWellDeflection, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellDeflection, false));
DataHelp.UpdateSearchWellName();
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveDataDrillWellLayered));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckWellCurve, (table, matches, i, start) => Save(table, matches, i, start, this.SaveDataCurve, false));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckWellCurveOrTimeDepth, (table, matches, i, start) => Save(table, matches, i, start, this.SaveDataCurveOrTimeDepth));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveDataWellPerforation));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveDataWellBreakPoint));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWellCapacity, (table, matches, i, start) => SaveWellCapacity(table, matches, i));
KEPDataManager.NotifyChange();
}
//else if (this.tableName == DBHelp.GetTableName())
//{
// await this.CheckAndSave(this.CheckFault, async (table, matches, i, start) => await SaveInterpret(table, matches, i, this.SaveDataFault));
// KEPDataManager.NotifyChange();
//}
//else if (this.tableName == DBHelp.GetTableName())
//{
// await this.CheckAndSave(this.CheckBoundary, async (table, matches, i, start) => await SaveInterpret(table, matches, i, this.SaveDataBoundary));
// KEPDataManager.NotifyChange();
//}
//else if (this.tableName == DBHelp.GetTableName())
//{
// if (!string.IsNullOrWhiteSpace(this.cfg.UpdateNode))
// {
// await this.CheckAndSave(this.CheckFault, async (table, matches, i, start) => await this.SaveDataScatterSingleFault(table, matches, i));
// }
// else
// {
// await this.CheckAndSave(this.CheckFault, async (table, matches, i, start) => await SaveInterpret(table, matches, i, this.SaveDataScatterFault));
// }
// KEPDataManager.NotifyChange();
//}
//else if (this.tableName == DBHelp.GetTableName())
//{
// if (!string.IsNullOrWhiteSpace(this.cfg.UpdateNode))
// {
// await this.CheckAndSave(this.CheckBoundary, async (table, matches, i, start) => await this.SaveDataScatterSingleBoundary(table, matches, i));
// }
// else
// {
// await this.CheckAndSave(this.CheckBoundary, async (table, matches, i, start) => await SaveInterpret(table, matches, i, this.SaveDataScatterBoundary));
// }
// KEPDataManager.NotifyChange();
//}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(this.CheckWorkArea, async (table, matches, i, start) =>
{
await this.SaveWorkArea(table, matches);
});
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(
this.CheckInputDataWithWellName,
(table, matches, i, start) => this.SaveWellEntity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(
this.CheckInputDataWithWellName,
(table, matches, i, start) => this.SaveWellEntity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(
this.CheckInputDataWithWellName,
(table, matches, i, start) => this.SaveWellEntity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(
this.CheckInputDataWithWellName,
(table, matches, i, start) => this.SaveWellEntity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(
this.CheckInputData,
(table, matches, i, start) => Save(table, matches, i, start, this.SaveDataHoriWellFractParm));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(
this.CheckInputData,
(table, matches, i, start) => Save(table, matches, i, start, this.SaveDataHoriWellFractCoord));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(
this.CheckWellExist,
(table, matches, i, start) => this.SaveWellEntity(table, matches, i));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave(
this.CheckInputData,
(table, matches, i, start) => Save(table, matches, i, start, this.SaveSurveyline2d));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSave((tableList, cache) => true, this.SaveCrossplot);
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellLithology));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveCycleLongterm));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveCycleMediumterm));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveCycleShortterm));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellInnerLayer));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellReservesLayer));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellCoring));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellSideWallCoring));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellWaterInjection));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellFluidProducingProfile));
KEPDataManager.NotifyChange();
}
else if (this.tableName == DBHelp.GetTableName())
{
await this.CheckAndSaveAsync(this.CheckInputDataWithWellName, (table, matches, i, start) => Save(table, matches, i, start, this.SaveWellLithologicalAnalysis));
KEPDataManager.NotifyChange();
}
//else if (this.tableType.IsType())
//{
// await SaveReservoirData();
// await SaveReservoirData();
// await SaveReservoirData();
// await SaveReservoirData();
// await SaveReservoirData();
//}
//else if (this.tableType.IsType())
//{
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
// await SaveInterpretTable();
//}
//else if (this.tableType == ReservoirHelp.Type)
//{
// await this.CheckAndSaveAsync(
// this.CheckInputDataWithWellName,
// async (table, matches, i, start) =>
// {
// await Save(table, matches, i, start, this.SaveCcsj);
// await Save(table, matches, i, start, this.SaveCcsj);
// await Save(table, matches, i, start, this.SaveCcsj);
// await Save(table, matches, i, start, this.SaveCcsj);
// await Save(table, matches, i, start, this.SaveCcsj);
// });
//}
}
this.IsDataImported = true;
// 保存InterpretTableXYZ
//async Task SaveInterpretTable()
// where T : InterpretTable, new()
//{
// if (this.tableType == typeof(T))
// {
// await this.CheckAndSave(
// this.CheckMultipleInputData,
// async (table, matches, i, start) => await SaveInterpret(table, matches, i, this.SaveDataInterpretTable));
// KEPDataManager.NotifyChange();
// }
//}
//// 保存储层数据
//async Task SaveReservoirData()
// where T : class, IReservoirData, new()
//{
// if (this.tableType == typeof(T))
// {
// await this.CheckAndSaveAsync(this.CheckWellExist, (table, matches, i, start) => Save(table, matches, i, start, this.SaveCcsj));
// KEPDataManager.NotifyChange();
// }
//}
// 保存通用数据
Task Save(DataTable table, MatchCache matches, int index, int startID, Action action, bool bPrompt = true)
where T : class, new()
{
return SaveGeneral(table, matches, index, startID, action, ref bPrompt);
}
Task SaveGeneral(DataTable table, MatchCache matches, int index, int startID, Action action, ref bool bPrompt)
where T : class, new()
{
if (bPrompt && !this.AlwaysAppend && !this.UpdateAppend && this.GetTableRecordCount() > 0)
{
if (DevExpress.XtraSplashScreen.SplashScreenManager.Default != null)
{
DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
}
var result = new DlgAppend().ShowDialog();
if (result == DialogResult.OK)
{
if (this.cfg is FrmImportData xtraForm)
{
DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(xtraForm, typeof(DlgWait), false, false, DevExpress.XtraSplashScreen.ParentFormState.Unlocked);
}
if (typeof(T) == typeof(InterpretDrillWellLayered))
{
DBHelp.Db.Deleteable().AS(DBHelp.GetTableOriginName()).ExecuteCommand();
}
//else if (typeof(T).IsType())
//{
// DBHelp.Db.Deleteable().AS(DBHelp.GetTableOriginName()).ExecuteCommand();
//}
DBHelp.Db.Deleteable().ExecuteCommand();
action(table, matches, index, startID, false);
bPrompt = false;
}
else if (result == DialogResult.Ignore)
{
//加一个判断,如果是压裂段参数,走追加逻辑
if (typeof(T) == typeof(HoriWellFracturParameter))
{
this.AddDataHoriWellFractParm(table, matches);
}
else
{
action(table, matches, index, startID, true);
}
bPrompt = false;
}
}
else
{
action(table, matches, index, startID, true);
}
return Task.CompletedTask;
}
// 保存地层数据
async Task SaveInterpret(DataTable table, MatchCache matches, int index, Func func)
where T : class, new()
{
List list_del = this.cfg.Storage.DataTableList.Rows.OfType()
.Select(row => row["层位"].ToString()).ToList();
int cnt = DBHelp.Db.Queryable().In("CW", list_del).Count();
if (!this.AlwaysAppend && cnt > 0)
{
if (DevExpress.XtraSplashScreen.SplashScreenManager.Default != null)
{
DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
}
var result = new DlgAppend().ShowDialog();
if (result == DialogResult.OK)
{
if (this.cfg is FrmImportData xtraForm)
{
DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(xtraForm, typeof(DlgWait), false, false, DevExpress.XtraSplashScreen.ParentFormState.Unlocked);
}
DBHelp.Db.Deleteable().Where($"CW in ({string.Join(", ", list_del.Select(o => $"'{o}'"))})").ExecuteCommand();
await func(table, matches, index);
}
else if (result == DialogResult.Ignore)
{
await func(table, matches, index);
}
}
else
{
await func(table, matches, index);
}
}
// 保存产能数据
Task SaveWellCapacity(DataTable table, MatchCache matches, int index)
where T : WellCapacity, new()
{
List list_del = table.Rows.OfType()
.Select(row => row["井号"].ToString()).ToList();
int cnt = DBHelp.Db.Queryable().In("JH", list_del).Count();
if (!this.AlwaysAppend && cnt > 0)
{
if (DevExpress.XtraSplashScreen.SplashScreenManager.Default != null)
{
DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
}
var result = new DlgAppend().ShowDialog();
if (result == DialogResult.OK)
{
if (this.cfg is FrmImportData xtraForm)
{
DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(xtraForm, typeof(DlgWait), false, false, DevExpress.XtraSplashScreen.ParentFormState.Unlocked);
}
DBHelp.Db.Deleteable().Where($"JH in ({string.Join(", ", list_del.Select(o => $"'{o}'"))})").ExecuteCommand();
this.SaveWellCapacity(table, matches, index);
}
else if (result == DialogResult.Ignore)
{
this.SaveWellCapacity(table, matches, index);
}
}
else
{
this.SaveWellCapacity(table, matches, index);
}
return Task.CompletedTask;
}
}
///
/// Checks the and save2.
///
/// The check.
/// The save.
/// A Task.
private async Task CheckAndSaveAsync(Func, MatchCache, Task> check, Func save)
{
try
{
List matches = this.cfg.ListMatches;
this.ui.UpdateProgress("数据准备中");
if (matches.All(m => m.Connections.Count == 0) && this.cfg.UseMatch)
{
this.ShowMessageBox("未配置数据列", "提示");
return;
}
MatchCache cache = new MatchCache(matches);
Type t = EntityHelp.GetEntityTypeByName(this.tableName);
if (typeof(InterpretTable).IsAssignableFrom(t) || this.tableName == DBHelp.GetTableName()
|| this.tableName == DBHelp.GetTableName())
{
this.cfg.TotalImport = 0;
DataTable table = this.cfg.Storage.DataTableList;
this.ui.UpdateProgress(Constants.Splash.DataChecking);
if (!await check(new List() { table }, cache))
{
return;
}
this.ui.UpdateProgress(Constants.Splash.DataSaving);
await save(table, cache, 0, -1);
}
else if (this.tableName == DBHelp.GetTableName())
{
this.cfg.TotalImport = 0;
this.importNum = 0;
List tableList = new List();
if (!this.cfg.ImportFromTable)
{
List readTasks = new List();
for (int i = 0; i < this.listFileNames.Count; i++)
{
string filepath = this.listFileNames[i];
readTasks.Add(Task.Run(() =>
{
DataTable tmpTable = new DataTable();
int rowindex = this.cfg.BeginIndex;
this.LoadDataTable(ref tmpTable, filepath, rowindex, false, false);
DataTable table = this.cfg.IsVertical ? tmpTable : this.Convert2HorizonTable(tmpTable);
table.TableName = Path.GetFileNameWithoutExtension(filepath);
tableList.Add(table);
}));
}
await Task.WhenAll(readTasks);
}
else
{
tableList.Add(this.cfg.Storage.DataTableShow);
}
this.ui.UpdateProgress(Constants.Splash.DataChecking);
if (!await check(tableList, cache))
{
return;
}
this.ui.UpdateProgress(Constants.Splash.DataSaving);
List taskList = new List();
for (int i = 0; i < tableList.Count; i++)
{
taskList.Add(save(tableList[i], cache, i, -1));
}
await Task.WhenAll(taskList);
}
else if (this.tableName == DBHelp.GetTableName())
{
List taskList = new List();
this.cfg.TotalImport = 0;
this.times = 0;
List tableList = new List();
//Stopwatch sw = new Stopwatch();
if (!this.cfg.ImportFromTable)
{
//Console.WriteLine($"``````抽稀规则:{this.cfg.RarefyIndex}``````````");
//totalSw.Start();
int rowindex = this.cfg.BeginIndex;
this.ui.UpdateProgress(Constants.Splash.DataSaving);
ConcurrentBag datas = new ConcurrentBag();
this.cfg.FileCount = this.listFileNames.Count;
int index = 0;
int maxId = DBHelp.NewDb.Queryable().Max("ID");
Parallel.ForEach(this.listFileNames, EntityHelp.Options, file =>
{
try
{
//sw.Restart();
DataTable tmpTable = new DataTable();
maxId = maxId + 1;
// txt文档每个文档一个井号,存储为一个文件
if (file.ToLower().EndsWith(".txt") || file.ToLower().EndsWith(".dev"))
{
this.LoadDataTable(ref tmpTable, file, rowindex, false, false);
DataTable table = this.cfg.IsVertical ? tmpTable : this.Convert2HorizonTable(tmpTable);
this.ui.UpdateProgress(string.Format(Constants.Splash.HandleProgress, index + 1, this.cfg.FileCount));
string fileName = Path.GetFileNameWithoutExtension(file);
table.TableName = fileName;
// 将文件信息存储至数据库
WellDeflection wdd = new WellDeflection
{
ID = maxId,
JH = fileName,
XHID = maxId,
FileName = $"{fileName}.data",
};
datas.Add(wdd);
save(table, cache, index, 1).Wait();
}
// dbf文件需要在读取时就拆分,否则太大也会很慢
else if (file.ToLower().EndsWith(".dbf"))
{
this.LoadDataTable(ref tmpTable, file, rowindex, false, false);
//DataTable table = this.cfg.IsVertical ? tmpTable : this.Convert2HorizonTable(tmpTable);
//this.ui.UpdateProgress(string.Format(Constants.Splash.HandleProgress, index + 1, this.cfg.FileCount));
//string fileName = Path.GetFileNameWithoutExtension(file);
//table.TableName = fileName;
//// 将文件信息存储至数据库
//WellDeflection wdd = new WellDeflection
//{
// JH = fileName,
// FileName = $"{fileName}.data",
//};
//datas.Add(wdd);
}
// 其它文件类型的数据
else
{
this.LoadDataTable(ref tmpTable, file, rowindex, false, false);
DataTable table = this.cfg.IsVertical ? tmpTable : this.Convert2HorizonTable(tmpTable);
// 分解table
if (cache.Keys.Contains("JH"))
{
var matchColumn = cache["JH"];
var tables = table.AsEnumerable().GroupBy(row => row.Field(matchColumn.MatchColumn))
.ToDictionary(g => g.Key, g => g.CopyToDataTable());
List tasks = new List();
bool isFirstRow = true;
foreach (var kvp in tables)
{
if (isFirstRow)
{
isFirstRow = false;
continue;
}
string jh = kvp.Key;
DataTable tb = kvp.Value;
tb.TableName = jh;
// 将文件信息存储至数据库
WellDeflection wdd = new WellDeflection
{
JH = jh,
XHID = maxId,
ID = maxId,
FileName = $"{jh}.data",
};
datas.Add(wdd);
tasks.Add(save(tb, cache, index, 1));
maxId = maxId + 1;
}
if (tasks != null && tasks.Count > 0)
{
Task.WhenAll(tasks).Wait();
}
}
}
//sw.Stop();
//Console.WriteLine($"保存第 [{index + 1}] 个文件耗时:{sw.Elapsed}");
index++;
}
catch (Exception ex)
{
Console.WriteLine($"处理文件 [{file}] 异常:{ex.Message}");
}
});
if (datas.Any())
{
List newDatas = datas.ToList();
DBHelp.NewDb.Storageable(newDatas).WhereColumns(w => w.JH).ExecuteCommand();
DBHelp.NewDb.Ado.ExecuteCommand("UPDATE well_deflection SET xhid = id WHERE xhid is null or xhid = 0");
}
}
else
{
tableList.Add(this.cfg.Storage.DataTableShow);
this.ui.UpdateProgress(Constants.Splash.DataChecking);
// 统一获取起始ID
int startID = this.GetStartID();
if (!await check(tableList, cache))
{
return;
}
this.ui.UpdateProgress(Constants.Splash.DataSaving);
for (int i = 0; i < tableList.Count; i++)
{
taskList.Add(save(tableList[i], cache, i, startID));
startID += tableList[i].Rows.Count;
}
await Task.WhenAll(taskList);
}
}
else
{
this.cfg.TotalImport = 0;
List tableList = new List();
if (!this.cfg.ImportFromTable)
{
for (int i = 0; i < this.listFileNames.Count; i++)
{
string filepath = this.listFileNames[i];
int rowindex = this.cfg.BeginIndex;
DataTable tmpTable = new DataTable();
this.LoadDataTable(ref tmpTable, filepath, rowindex, false, false);
DataTable table = this.cfg.IsVertical ? tmpTable : this.Convert2HorizonTable(tmpTable);
table.TableName = Path.GetFileNameWithoutExtension(filepath);
tableList.Add(table);
}
}
else
{
tableList.Add(this.cfg.Storage.DataTableShow);
}
this.ui.UpdateProgress(Constants.Splash.DataChecking);
List taskList = new List();
//统一获取起始ID
int startID = 1;
if (this.tableName == DBHelp.GetTableName())
{
startID = this.GetStartID();
}
if (!await check(tableList, cache))
{
return;
}
this.ui.UpdateProgress(Constants.Splash.DataSaving);
for (int i = 0; i < tableList.Count; i++)
{
taskList.Add(save(tableList[i], cache, i, startID));
startID += tableList[i].Rows.Count;
}
await Task.WhenAll(taskList);
}
if (this.cfg.TotalImport > 0)
{
if (this.tableName == DBHelp.GetTableName())
{
this.ShowMessageBox(string.Format("导入'测井曲线' {0} 口井!" + this.importTips, this.importNum), "提示");
}
else
{
this.ShowMessageBox(string.Format("导入'{0}' {1} 条数据!", DBHelp.GetTableDescription(this.cfg.TableType), this.cfg.TotalImport), "提示");
}
this.ui.OnSaveFinish(this.cfg);
}
else
{
this.ShowMessageBox($"{DBHelp.GetTableDescription(this.cfg.TableType)},未导入数据!", "提示");
}
}
catch (Exception ex)
{
this.ShowMessageBox(ex.Message, "导入失败");
}
}
///
/// 检测并保存
///
/// 检测函数
/// 保存函数
private async Task CheckAndSave(Func, MatchCache, bool> check, Func save)
{
await this.CheckAndSaveAsync((list, cache) => Task.FromResult(check(list, cache)), save);
}
#region 读取数据
private void LoadWellDeflectionDbfPreviewData(ref DataTable table, string filepath, int rowindex)
{
try
{
table.Columns.Clear();
table.Clear();
string dbfFilePath = filepath;
using (var dbfTable = new DbfTable(filepath, Encoding.GetEncoding("GB2312")))
{
var fields = dbfTable.Columns;
for (int i = 0; i < fields.Count; i++)
{
// 忽略_NullFlags列
if ((int)fields[i].ColumnType == 0x30)
{
continue;
}
table.Columns.Add(fields[i].ColumnName);
}
int beginRow = rowindex + (this.cfg.UseHeaderRow ? 1 : 0);
int index_temp = 0;
var dbfRecord = new DbfDataReader.DbfRecord(dbfTable);
while (dbfTable.Read(dbfRecord))
{
if (index_temp >= beginRow)
{
DataRow dr = table.NewRow();
for (int i = 0; i < dbfRecord.Values.Count; i++)
{
// 忽略_NullFlags列
if ((int)fields[i].ColumnType == 0x30)
{
continue;
}
dr[i] = dbfRecord.Values[i].GetValue();
}
table.Rows.Add(dr);
}
index_temp++;
// 预览时只取前100条数据
if (table.Rows.Count == 100)
{
break;
}
}
this.RecordCount = index_temp;
}
}
catch (Exception ex)
{
this.errorMessage = ex.Message;
Console.WriteLine(ex.ToString());
}
}
private void LoadWellDeflectionDataTableDbf(ref DataTable table, string filepath, int rowindex)
{
try
{
table.Columns.Clear();
table.Clear();
string dbfFilePath = filepath;
//Stopwatch sw = new Stopwatch();
// 存储井斜导入结果
List datas = new List();
int wellCount = DBHelp.NewDb.Queryable().Count();
int maxId = DBHelp.NewDb.Queryable().Max("ID");
int currentWellIndex = 0;
//Console.WriteLine($"井总数:{wellCount}");
if (wellCount > 0)
{
this.cfg.FileCount = wellCount;
this.RecordCount = wellCount;
}
string currentJH = string.Empty;
string jhKey = string.Empty;
int jhIndex = -1;
var matches = this.cfg.ListMatches;
bool isSameWell = true;
MatchCache cache = new MatchCache(matches);
if (cache.ContainsKey("JH"))
{
var jhItem = cache["JH"];
jhKey = jhItem.MatchColumn;
}
// 没有井号,不能匹配数据
if (string.IsNullOrWhiteSpace(jhKey))
{
return;
}
using (var dbfTable = new DbfTable(filepath, Encoding.GetEncoding("GB2312")))
{
var fields = dbfTable.Columns;
for (int i = 0; i < fields.Count; i++)
{
// 忽略_NullFlags列
if ((int)fields[i].ColumnType == 0x30)
{
continue;
}
table.Columns.Add(fields[i].ColumnName);
if (fields[i].ColumnName == jhKey)
{
jhIndex = i;
}
}
int beginRow = rowindex + (this.cfg.UseHeaderRow ? 1 : 0);
int index_temp = 0;
var dbfRecord = new DbfDataReader.DbfRecord(dbfTable);
while (dbfTable.Read(dbfRecord))
{
//sw.Restart();
if (index_temp >= beginRow)
{
DataRow dr = table.NewRow();
for (int i = 0; i < dbfRecord.Values.Count; i++)
{
// 忽略_NullFlags列
if ((int)fields[i].ColumnType == 0x30)
{
continue;
}
dr[i] = dbfRecord.Values[i].GetValue();
if (jhIndex == i)
{
var jhTemp = dr[i].ToString();
// 没有数据,则记录当前井号
if (table.Rows.Count == 0)
{
table.TableName = jhTemp;
currentJH = jhTemp;
isSameWell = true;
}
else
{
// 不是同一口井的数据,则生成当前井的文件数据
if (jhTemp != currentJH)
{
//Console.WriteLine($"不是同一井号的数据:原井号-{currentJH}\t 新井号-{jhTemp}");
isSameWell = false;
currentJH = jhTemp;
}
}
}
}
// 导入的数据是井斜
// 同一口井的数据就添加进表里面
if (isSameWell)
{
// 如果配置了抽稀规则,则按抽稀规则取数据
if (this.cfg.RarefyIndex >= 0)
{
if (index_temp == beginRow || (index_temp % (this.cfg.RarefyIndex + 2) == 0))
{
table.Rows.Add(dr);
}
}
// 没有配置抽稀规则,则直接取数据
else
{
table.Rows.Add(dr);
}
}
// 当前井的数据已经取完
else
{
currentWellIndex++;
// 先把数据保存,之后清空,再重新装入数据
string fileName = table.TableName;
DataTable saveTable = this.cfg.IsVertical ? table : this.Convert2HorizonTable(table);
if (wellCount > 0)
{
this.ui.UpdateProgress(string.Format(Constants.Splash.HandleProgress, currentWellIndex, this.cfg.FileCount));
}
else
{
this.ui.UpdateProgress($"当前导入第 {currentWellIndex} 口井的数据,井号为 【{fileName}】!");
}
this.SaveWellDeflection(saveTable, cache, 0, 0, false);
// 将文件信息存储至数据库
WellDeflection wdd = new WellDeflection
{
ID = maxId + currentWellIndex,
JH = fileName,
XHID = maxId + currentWellIndex,
FileName = $"{fileName}.data",
};
datas.Add(wdd);
isSameWell = true;
table.Rows.Clear();
table.TableName = currentJH;
//currentJH = string.Empty;
//sw.Stop();
if (dr != null)
{
table.Rows.Add(dr);
}
//Console.WriteLine($"导入第 {currentWellIndex} 口井,井号为【{fileName}】的井斜数据,耗时:{sw.Elapsed}");
}
}
index_temp++;
}
// 最后一个表格数据
if (table != null && table.Rows.Count > 0)
{
currentWellIndex++;
// 先把数据保存,之后清空,再重新装入数据
string fileName = table.TableName;
DataTable saveTable = this.cfg.IsVertical ? table : this.Convert2HorizonTable(table);
if (wellCount > 0)
{
this.ui.UpdateProgress(string.Format(Constants.Splash.HandleProgress, currentWellIndex, this.cfg.FileCount));
}
else
{
this.ui.UpdateProgress($"当前导入第 {currentWellIndex} 口井的数据,井号为 【{fileName}】!");
}
this.SaveWellDeflection(saveTable, cache, 0, 0, false);
// 将文件信息存储至数据库
WellDeflection wdd = new WellDeflection
{
JH = fileName,
XHID = currentWellIndex,
FileName = $"{fileName}.data",
};
datas.Add(wdd);
//Console.WriteLine($"导入第 {currentWellIndex} 口井,井号为【{fileName}】的井斜数据,耗时:{sw.Elapsed}");
}
if (datas.Any())
{
DBHelp.NewDb.Storageable(datas).WhereColumns(w => w.JH).ExecuteCommand();
DBHelp.NewDb.Ado.ExecuteCommand("UPDATE well_deflection SET xhid = id WHERE xhid is null or xhid = 0");
}
}
}
catch (Exception ex)
{
this.errorMessage = ex.Message;
Console.WriteLine(ex.ToString());
}
}
///
/// LoadDataTable
///
/// table
/// filepath
/// rowindex
/// reloadTableName
/// previewOnly
public void LoadDataTable(ref DataTable table, string filepath, int rowindex, bool reloadTableName, bool previewOnly)
{
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
if (filename.LastIndexOf(".") == -1)
{
this.LoadDataTableTxt(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
else
{
string filetype = Path.GetExtension(filename).Replace(".", string.Empty).ToUpper();
if (filetype == "DBF")
{
if (this.tableType == typeof(WellDeflection))
{
if (previewOnly)
{
this.LoadWellDeflectionDbfPreviewData(ref table, filepath, rowindex);
}
else
{
this.LoadWellDeflectionDataTableDbf(ref table, filepath, rowindex);
}
}
else
{
this.LoadDataTableDbf(ref table, filepath, rowindex, previewOnly);
}
// this.LoadDataTableDbf(ref table, filepath, rowindex, previewOnly);
}
else if (filetype == "DEV")
{
this.LoadDataTableDev(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
else if (filetype == "LAS")
{
this.LoadDataTableLasNew(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
else if (filetype == "DAT")
{
if (this.tableName == DBHelp.GetTableName())
{
this.LoadDataTableDatByCurve(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
else
{
this.LoadDataTableDat(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
}
else if (filetype == "TXT" || filetype == "CSV" || filetype == "XYZ")
{
if (this.tableName == DBHelp.GetTableName())
{
this.LoadDataTableTxtByCurve(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
else
{
this.LoadDataTableTxt(ref table, filepath, this.GetFileEncoding(filepath), rowindex, previewOnly);
}
}
else if (filetype == "DFD")
{
this.LoadDataTableDfd(ref table, filepath, rowindex, previewOnly);
}
else if (filetype == "XLS" || filetype == "XLSX" || filetype == "MDB")
{
if (reloadTableName)
{
this.LoadOfficeTableName(filepath, previewOnly);
}
else
{
this.ReadOfficeData(ref table, filepath, previewOnly);
}
}
this.RecordCount = table.Rows.Count;
}
}
///
/// 按表名读取数据.
///
/// table
/// previewOnly
public void ReadDataByTableName(ref DataTable table, bool previewOnly)
{
if (this.dbType == "excel" || this.dbType == "access")
{
this.ReadOfficeData(ref table, this.currentFileName, previewOnly);
}
else if (this.dbType == "oracle" || this.dbType == "sqlserver")
{
this.ReadDataBasePreview(ref table, previewOnly);
}
}
///
/// Reads the office data.
///
/// The table.
/// The filepath.
/// If true, preview only.
private void ReadOfficeData(ref DataTable table, string filepath, bool previewOnly)
{
using (SplashHelper.Create())
{
int rowindex = this.cfg.BeginIndex;
string tableName = this.cfg.DbTableName;
if (this.dbType == "excel")
{
this.LoadDataTableXls(ref table, filepath, tableName, rowindex, previewOnly);
}
else if (this.dbType == "access")
{
this.LoadDataTableAccess(ref table, filepath, tableName, rowindex, previewOnly);
}
}
}
///
/// 加载excel的sheet
///
/// 路径
/// 是否预览
private void LoadOfficeTableName(string path, bool previewOnly = false)
{
this.currentFileName = path;
if (this.dbType == "excel")
{
string ext = Path.GetExtension(path);
var lstSheetName = new List();
int activeSheetIndex = 0;
if (ext.ToLower().Equals(".xls"))
{
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
NPOI.SS.UserModel.IWorkbook workbook = WorkbookFactory.Create(stream, true);
int sheetIndex = 0;
for (int i = 0; i < workbook.NumberOfSheets; i++)
{
string sheetName = workbook.GetSheetName(i);
lstSheetName.Add(sheetName);
if (this.cfg.DbTableName == sheetName)
{
activeSheetIndex = sheetIndex;
}
sheetIndex++;
}
}
}
else
{
lstSheetName = MiniExcel.GetSheetNames(path);
for (int i = 0; i < lstSheetName.Count; i++)
{
if (this.cfg.DbTableName == lstSheetName[i])
{
activeSheetIndex = i;
}
}
}
this.ui.OnLoadOfficeTablesFinish(lstSheetName, activeSheetIndex);
}
else if (this.dbType == "access")
{
OleDbConnection conn = null;
try
{
conn = new OleDbConnection(new ConnStringData { FilePath = path }.Access);
conn.Open();
DataTable dt_sheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt_sheet == null)
{
return;
}
var list = new List();
for (int i = 0; i < dt_sheet.Rows.Count; i++)
{
string tabletype = dt_sheet.Rows[i]["TABLE_TYPE"].ToString();
string tableName = dt_sheet.Rows[i]["TABLE_NAME"].ToString();
if (tabletype.ToUpper() == "TABLE")
{
list.Add(tableName);
}
}
conn.Close();
this.ui.OnLoadOfficeTablesFinish(list, 0);
// conn.Dispose();
}
catch (Exception e)
{
this.ShowMessageBox("无法打开此文件", "提示");
conn.Close();
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
return;
}
}
}
///
/// Loads the data table dbf.
///
/// The table.
/// The filepath.
/// The rowindex.
/// If true, preview only.
private void LoadDataTableDbf(ref DataTable table, string filepath, int rowindex, bool previewOnly = false)
{
try
{
table.Columns.Clear();
table.Clear();
string dbfFilePath = filepath;
//using (var stream = File.Open(dbfFilePath, FileMode.Open, FileAccess.Read))
//using (var reader = new DotNetDBF.DBFReader(stream))
//{
// this.RecordCount = reader.RecordCount;
// var fields = reader.Fields;
// for (int i = 0; i < fields.Length; i++)
// {
// // 忽略_NullFlags列
// if ((int)fields[i].DataType == 0x30)
// {
// continue;
// }
// table.Columns.Add(fields[i].Name);
// }
// int beginRow = rowindex + (this.cfg.UseHeaderRow ? 1 : 0);
// int index_temp = 0;
// var dbfRecord = reader.NextRecord();
// while (dbfRecord != null)
// {
// if (index_temp >= beginRow)
// {
// DataRow dr = table.NewRow();
// for (int i = 0; i < dbfRecord.Length; i++)
// {
// // 忽略_NullFlags列
// if ((int)fields[i].DataType == 0x30)
// {
// continue;
// }
// dr[i] = dbfRecord.GetValue(i);
// }
// table.Rows.Add(dr);
// }
// dbfRecord = reader.NextRecord();
// index_temp++;
// }
//}
using (var dbfTable = new DbfTable(filepath, Encoding.GetEncoding("GB2312")))
{
var fields = dbfTable.Columns;
for (int i = 0; i < fields.Count; i++)
{
// 忽略_NullFlags列
if ((int)fields[i].ColumnType == 0x30)
{
continue;
}
table.Columns.Add(fields[i].ColumnName);
}
int beginRow = rowindex + (this.cfg.UseHeaderRow ? 1 : 0);
int index_temp = 0;
var dbfRecord = new DbfRecord(dbfTable);
while (dbfTable.Read(dbfRecord))
{
if (index_temp >= beginRow)
{
DataRow dr = table.NewRow();
for (int i = 0; i < dbfRecord.Values.Count; i++)
{
// 忽略_NullFlags列
if ((int)fields[i].ColumnType == 0x30)
{
continue;
}
dr[i] = dbfRecord.Values[i].GetValue();
}
table.Rows.Add(dr);
}
index_temp++;
}
this.RecordCount = index_temp;
}
}
catch (Exception ex)
{
this.errorMessage = ex.Message;
Console.WriteLine(ex.ToString());
}
}
private void LoadDataTableDfd(ref DataTable table, string filename, int rowindex, bool previewOnly)
{
try
{
FileStream fs = File.OpenRead(filename);
StreamReader reader = new StreamReader(fs);
List> list = new List>();
string str = reader.ReadLine();
while (str != null)
{
List list_row = new List();
string[] array = str.Split(Constants.StringSplit.Separator, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < array.Length; i++)
{
list_row.Add(array[i]);
}
list.Add(list_row);
str = reader.ReadLine();
}
this.RecordCount = list.Count;
if (list.Count > 0)
{
for (int i = 0; i < list[0].Count; i++)
{
string colname = "列" + (i + 1);
if (this.cfg.UseHeaderRow)
{
// 避免列头重名
colname = list[0][i] + $"({colname})";
// colname = list[0][i];
}
table.Columns.Add(colname);
this.cfg.Storage.DataTableShow.Columns.Add(colname);
}
int rowcount_temp = 0;
int beginRow = rowindex + (this.cfg.UseHeaderRow ? 1 : 0);
for (int i = beginRow; i < list.Count; i++)
{
if (previewOnly && rowcount_temp > PreviewLineCount)
{
break;
}
DataRow dr = table.NewRow();
for (int j = 0; j < list[i].Count; j++)
{
// Reporter.WriteMsg("i:" + i+",j:"+j);
dr[j] = list[i][j];
}
table.Rows.Add(dr);
rowcount_temp++;
}
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
}
private void LoadDataTableAccess(ref DataTable table, string path, string tableName, int rowindex, bool previewOnly)
{
try
{
if (string.IsNullOrWhiteSpace(tableName))
{
return;
}
OleDbConnection conn = null;
try
{
conn = new OleDbConnection(new ConnStringData { FilePath = path }.Access);
conn.Open();
OleDbDataAdapter myDa = new OleDbDataAdapter("Select * from " + tableName, conn);
DataTable tmp = new DataTable();
myDa.Fill(tmp);
if (previewOnly)
{
DataHelp.CloneTable(tmp, table, false, count: PreviewLineCount);
}
else
{
table = tmp;
}
this.RecordCount = table.Rows.Count;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
conn?.Close();
// conn.Dispose();
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
}
private void LoadDataTableEmpty(DataTable table, string filename, int rowindex, bool previewOnly)
{
try
{
FileStream fs = File.OpenRead(filename);
StreamReader reader = new StreamReader(fs);
List> list = new List>();
string type = string.Empty;
string str = reader.ReadLine();
if (str == null)
{
return;
}
if (str == "# Type: scattered data")
{
type = "A";
}
else if (str.StartsWith("! "))
{
type = "B";
}
else
{
type = "C";
}
bool next = false;
while (str != null)
{
if (type == "A")
{
if (str.StartsWith("# "))
{
str = reader.ReadLine();
continue;
}
else
{
next = true;
}
}
else if (type == "B")
{
if (str == "@")
{
next = true;
str = reader.ReadLine();
continue;
}
}
else if (type == "C")
{
next = true;
}
if (!next)
{
str = reader.ReadLine();
continue;
}
List list_row = new List();
string[] array = str.Split(Constants.StringSplit.Separator, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < array.Length; i++)
{
list_row.Add(array[i]);
}
list.Add(list_row);
str = reader.ReadLine();
}
this.RecordCount = list.Count;
if (list.Count > 0)
{
for (int i = 0; i < list[0].Count; i++)
{
string colname = "列" + (i + 1);
if (this.cfg.UseHeaderRow)
{
colname = list[0][i];
}
table.Columns.Add(colname);
this.cfg.Storage.DataTableShow.Columns.Add(colname);
}
int rowcount_temp = 0;
for (int i = rowindex; i < list.Count; i++)
{
if (previewOnly && rowcount_temp > PreviewLineCount)
{
break;
}
DataRow dr = table.NewRow();
for (int j = 0; j < list[i].Count; j++)
{
dr[j] = list[i][j];
}
table.Rows.Add(dr);
rowcount_temp++;
}
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
}
private void LoadDataTableOracle(ref DataTable table, int rowindex, bool previewOnly)
{
try
{
string tableName = this.cfg.DbTableName;
Oracle10g dB = new Oracle10g(this.cfg.DbConnectString);
DataTable tmp = dB.GetDataTable("select * from " + tableName);
dB.Dispose();
if (previewOnly)
{
DataHelp.CloneTable(tmp, table, false, count: PreviewLineCount);
}
else
{
table = tmp;
}
this.RecordCount = table.Rows.Count;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Console.WriteLine(ex.Message);
}
}
private void LoadDataTableSqlServer(ref DataTable table, int rowindex, bool previewOnly)
{
try
{
string tableName = this.cfg.DbTableName;
Server2008 dB = new Server2008(this.cfg.DbConnectString);
DataTable tmp = dB.GetDataTable("select * from " + tableName);
dB.Dispose();
if (previewOnly)
{
DataHelp.CloneTable(tmp, table, false, count: PreviewLineCount);
}
else
{
table = tmp;
}
this.RecordCount = table.Rows.Count;
}
catch (Exception ex)
{
this.errorMessage = ex.Message;
Console.WriteLine(ex.Message);
}
}
private void LoadDataTableTxt(ref DataTable table, string filepath, string charset, int rowindex, bool previewOnly = false)
{
this.LoadDataTableTxtWithoutHeader(ref table, filepath, charset, rowindex, previewOnly);
}
///
/// Loads the data table dev.
///
/// The table.
/// The filename.
/// The charset.
/// The rowindex.
/// If true, preview only.
private void LoadDataTableDev(ref DataTable table, string filename, string charset, int rowindex, bool previewOnly = false)
{
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)))
{
PreviewData preview = new PreviewData();
int lineindex = 0;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
else if (str.StartsWith("#"))
{
continue;
}
string trimString = string.Join(",", str.Split(
Constants.StringSplit.EmptySeparator,
StringSplitOptions.RemoveEmptyEntries));
string[] array = trimString.Split(
Constants.StringSplit.Separator,
StringSplitOptions.None);
if (previewOnly && lineindex >= PreviewLineCount)
{
break;
}
if (lineindex < PreviewLineCount)
{
preview.Add(new PreviewLine(array));
}
if (lineindex == 0)
{
for (int i = 0; i < array.Length; i++)
{
this.GenerateColumnByPreview(table, preview, i);
}
}
if (lineindex == rowindex && this.cfg.UseHeaderRow && previewOnly)
{
lineindex++;
continue;
}
if (lineindex >= rowindex)
{
for (int i = 0; i < array.Length; i++)
{
this.GenerateColumnByPreview(table, preview, i);
}
if (array.Length > 0)
{
table.Rows.Add(array);
}
}
lineindex++;
}
this.PreviewCache[filename] = preview;
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
}
///
/// Loads the data table dat.
///
/// The table.
/// The filename.
/// The charset.
/// The rowindex.
/// If true, preview only.
private void LoadDataTableDat(ref DataTable table, string filename, string charset, int rowindex, bool previewOnly = false)
{
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)))
{
PreviewData preview = new PreviewData();
int lineindex = 0;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
string trimString = string.Join(",", str.Split(
Constants.StringSplit.EmptySeparator,
StringSplitOptions.RemoveEmptyEntries));
string[] array = trimString.Split(
Constants.StringSplit.Separator,
StringSplitOptions.None);
if (previewOnly && lineindex >= PreviewLineCount)
{
break;
}
if (lineindex < PreviewLineCount)
{
preview.Add(new PreviewLine(array));
}
if (lineindex == 0)
{
for (int i = 0; i < array.Length; i++)
{
this.GenerateColumnByPreview(table, preview, i);
}
}
if (lineindex == rowindex && this.cfg.UseHeaderRow)
{
lineindex++;
continue;
}
if (lineindex >= rowindex)
{
for (int i = 0; i < array.Length; i++)
{
this.GenerateColumnByPreview(table, preview, i);
}
if (array.Length > 0)
{
table.Rows.Add(array);
}
}
lineindex++;
}
this.PreviewCache[filename] = preview;
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
}
///
/// Loads the data table dat.
///
/// The table.
/// The filename.
/// The charset.
/// The rowindex.
/// If true, preview only.
private List LoadDataTableDatInHeader(ref DataTable table, string filename, string charset, int rowindex, bool previewOnly = false)
{
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)))
{
PreviewData preview = new PreviewData();
int lineindex = 0;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
string trimString = string.Join(",", str.Split(
Constants.StringSplit.EmptySeparator,
StringSplitOptions.RemoveEmptyEntries));
string[] array = trimString.Split(
Constants.StringSplit.Separator,
StringSplitOptions.None);
if (previewOnly && lineindex >= PreviewLineCount)
{
break;
}
if (lineindex == 0)
{
for (int i = 0; i < array.Length; i++)
{
string colName = array[i];
if (!columnNames.Contains(colName))
{
columnNames.Add(colName);
}
}
//判断表头
for (int i = 0; i < columnNames.Count; i++)
{
table.Columns.Add(columnNames[i]);
this.allColumnNames.Add(columnNames[i]);
}
}
else
{
table.Rows.Add(array);
}
lineindex++;
}
this.PreviewCache[filename] = preview;
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
return columnNames;
}
///
/// Loads the data table dat by curve.
///
/// The table.
/// The filename.
/// The charset.
/// The rowindex.
/// If true, preview only.
/// A list of string.
private List LoadDataTableDatByCurve(ref DataTable table, string filename, string charset, int rowindex, bool previewOnly = false)
{
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)))
{
PreviewData preview = new PreviewData();
int lineindex = 0;
bool isReadHeader = false;
bool isReadBody = false;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
str = str.Trim(' ');
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
else if (str.StartsWith("# DEPT"))
{
str = str.Replace("# DEPT", "DEPT");
isReadHeader = true;
}
else if (str.StartsWith("# TVD"))
{
str = str.Replace("# TVD", "TVD");
isReadHeader = true;
}
else if (str.StartsWith("DEPTHT"))
{
str = str.Replace("DEPTHT", "DEPTH");
isReadHeader = true;
this.importTips = $"{filename}的深度列名是DEPTHT,导入时已将列名替换为Depth!";
}
string trimString = string.Join(",", str.Split(
Constants.StringSplit.EmptySeparator,
StringSplitOptions.RemoveEmptyEntries));
string[] array = trimString.Split(
Constants.StringSplit.Separator,
StringSplitOptions.None);
if (isReadHeader && !isReadBody)
{
foreach (var colName in array)
{
if (!columnNames.Contains(colName))
{
columnNames.Add(colName);
}
}
//判断表头
for (int i = 0; i < columnNames.Count; i++)
{
table.Columns.Add(columnNames[i]);
this.allColumnNames.Add(columnNames[i]);
}
isReadBody = true;
//说明表头写完了
continue;
}
if (isReadBody)
{
if (previewOnly && lineindex >= PreviewLineCount)
{
break;
}
if (lineindex < PreviewLineCount)
{
preview.Add(new PreviewLine(array));
}
table.Rows.Add(array);
lineindex++;
}
}
this.PreviewCache[filename] = preview;
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
if (columnNames.Count == 0)
{
columnNames = this.LoadDataTableDatInHeader(ref table, filename, charset, rowindex, previewOnly);
}
return columnNames;
}
///
/// Loads the data table las.
///
/// The table.
/// The filename.
/// The charset.
/// The rowindex.
/// If true, preview only.
private List LoadDataTableLas(ref DataTable table, string filename, string charset, int rowindex, bool previewOnly = false)
{
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)))
{
PreviewData preview = new PreviewData();
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)
{
this.errorMessage += $"文件{filename}列读取失败,已跳过文件,{Environment.NewLine}";
return new List();
}
//说明表头写完了
for (int i = 0; i < columnNames.Count; i++)
{
table.Columns.Add(columnNames[i]);
this.allColumnNames.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();
this.errorMessage += $"文件{filename}存在重复列名{colName},已跳过文件,{Environment.NewLine}";
return new List();
}
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;
}
strLine = string.Empty;
if (previewOnly && lineindex >= PreviewLineCount)
{
break;
}
if (lineindex < PreviewLineCount)
{
preview.Add(new PreviewLine(array));
}
for (int i = 0; i < array.Length; i++)
{
this.GenerateColumnByPreview(table, preview, i);
}
if (array.Length > 0)
{
table.Rows.Add(array);
}
lineindex++;
}
this.PreviewCache[filename] = preview;
}
}
catch (Exception e)
{
this.errorMessage = e.Message;
Console.WriteLine(e.ToString());
}
return columnNames;
}
///
/// 从 ~A 行提取列名
///
/// 存储加载的数据
/// 文件的路径
/// 字符编码
/// 行索引
/// 仅加载预览数据
private List LoadDataTableLasNew(ref DataTable table, string filename, string charset, int rowindex, bool previewOnly = false)
{
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)))
{
PreviewData preview = new PreviewData();
int lineindex = 0;
bool isReadBody = false;
string strLine = string.Empty;
while (!reader.EndOfStream)
{
string str = reader.ReadLine();
if (string.IsNullOrWhiteSpace(str))
{
continue;
}
else if (str.StartsWith("~A", StringComparison.OrdinalIgnoreCase))
{
isReadBody = true;
// 从 ~A 行提取列名
string headerLine = str.Substring(2).Trim(); // 去掉 ~A 前缀
string[] headers = headerLine.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
// 清空已有列名
table.Columns.Clear();
columnNames.Clear();
foreach (var header in headers)
{
// 进一步处理列名,例如去除特殊字符
string colName = header.Trim().ToUpper();
if (!columnNames.Contains(colName))
{
columnNames.Add(colName);
table.Columns.Add(colName);
this.allColumnNames.Add(colName);
}
else
{
// 存在重复列名
this.errorMessage += $"文件{filename}存在重复列名{colName}, 已跳过文件,{Environment.NewLine}";
table.Columns.Clear();
return new List