using ExcelDataReader; using System; using System.Collections.Generic; using System.Data; using System.IO; namespace etoy { class CmdReadKeyValueTable : Command { const string SEARCH_PATTERN = "*.kv"; ExcelReaderConfiguration _excelReaderConfiguration; KeyValueTableCsvParser _parser = new KeyValueTableCsvParser(); public override string Description => "读取KeyValue表"; protected override void OnProcess() { SetProgress(0.1f); _excelReaderConfiguration = new ExcelReaderConfiguration() { AutodetectSeparators = new char[] { ',' } }; var kvs = Directory.GetFiles(Context.Option.CsvOutput, SEARCH_PATTERN, SearchOption.TopDirectoryOnly); List tables = new List(); SetProgress(0.4f); foreach (var kv in kvs) ReadSingle(kv.ToPath(), tables); SetProgress(0.8f); Context.Blackboard.KeyValueTables = tables; Completed(); } void ReadSingle(string file, List tables) { using Stream metadata = new FileStream(file, FileMode.Open, FileAccess.Read); IExcelDataReader reader = ExcelReaderFactory.CreateCsvReader(metadata, _excelReaderConfiguration); DataSet dataSet = reader.AsDataSet(); if (dataSet.Tables.Count != 1) throw new Exception($"KeyValue表 Table数量不为1, {file}"); DataTable dt = dataSet.Tables[0]; if (dt == null) throw new Exception($"KeyValue表 DataTable等于空, {file}"); KeyValueTable table = _parser.Parse(dt, file); tables.Add(table); metadata.Close(); reader.Close(); reader.Dispose(); } } }