CmdReadKeyValueTable.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using ExcelDataReader;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. namespace etoy
  7. {
  8. class CmdReadKeyValueTable : Command
  9. {
  10. const string SEARCH_PATTERN = "*.kv";
  11. ExcelReaderConfiguration _excelReaderConfiguration;
  12. KeyValueTableCsvParser _parser = new KeyValueTableCsvParser();
  13. public override string Description => "读取KeyValue表";
  14. protected override void OnProcess()
  15. {
  16. SetProgress(0.1f);
  17. _excelReaderConfiguration = new ExcelReaderConfiguration() { AutodetectSeparators = new char[] { ',' } };
  18. var kvs = Directory.GetFiles(Context.Option.CsvOutput, SEARCH_PATTERN, SearchOption.TopDirectoryOnly);
  19. List<KeyValueTable> tables = new List<KeyValueTable>();
  20. SetProgress(0.4f);
  21. foreach (var kv in kvs)
  22. ReadSingle(kv.ToPath(), tables);
  23. SetProgress(0.8f);
  24. Context.Blackboard.KeyValueTables = tables;
  25. Completed();
  26. }
  27. void ReadSingle(string file, List<KeyValueTable> tables)
  28. {
  29. using Stream metadata = new FileStream(file, FileMode.Open, FileAccess.Read);
  30. IExcelDataReader reader = ExcelReaderFactory.CreateCsvReader(metadata, _excelReaderConfiguration);
  31. DataSet dataSet = reader.AsDataSet();
  32. if (dataSet.Tables.Count != 1)
  33. throw new Exception($"KeyValue表 Table数量不为1, {file}");
  34. DataTable dt = dataSet.Tables[0];
  35. if (dt == null)
  36. throw new Exception($"KeyValue表 DataTable等于空, {file}");
  37. KeyValueTable table = _parser.Parse(dt, file);
  38. tables.Add(table);
  39. metadata.Close();
  40. reader.Close();
  41. reader.Dispose();
  42. }
  43. }
  44. }