12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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<KeyValueTable> tables = new List<KeyValueTable>();
- 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<KeyValueTable> 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();
- }
- }
- }
|