ExcelFileReader.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using NPOI.SS.Formula.Functions;
  2. using NPOI.SS.UserModel;
  3. using InABox.Core;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using static NPOI.HSSF.UserModel.HeaderFooter;
  11. using System.Diagnostics.CodeAnalysis;
  12. namespace InABox.Scripting
  13. {
  14. public class ExcelFileReader : ITabularFileReader
  15. {
  16. private IEnumerator<IRow> rows;
  17. public Dictionary<string, int> Columns { get; set; }
  18. [MemberNotNullWhen(false, nameof(_row))]
  19. public bool EndOfData { get; private set; }
  20. private IRow? _row;
  21. public ExcelFileReader(Stream stream)
  22. {
  23. rows = new Spreadsheet(stream).GetSheet(0).RowEnumerator();
  24. Columns = new Dictionary<string, int>();
  25. SkipLine();
  26. }
  27. public IList<string> ReadLineValues()
  28. {
  29. var results = new List<string>();
  30. if (!EndOfData)
  31. {
  32. foreach (var cell in _row.Cells())
  33. {
  34. results.Add(cell.GetValue());
  35. }
  36. SkipLine();
  37. }
  38. return results;
  39. }
  40. public bool ReadHeader()
  41. {
  42. Columns.Clear();
  43. if (EndOfData)
  44. {
  45. return false;
  46. }
  47. int i = 0;
  48. foreach(var cell in _row.Cells())
  49. {
  50. var column = cell.GetValue();
  51. if (!column.IsNullOrWhiteSpace())
  52. {
  53. Columns.Add(column, i);
  54. }
  55. ++i;
  56. }
  57. SkipLine();
  58. return true;
  59. }
  60. public Dictionary<string, object?> ReadLine()
  61. {
  62. if (EndOfData)
  63. {
  64. return new Dictionary<string, object?>();
  65. }
  66. var results = Columns.ToDictionary(
  67. x => x.Key,
  68. x =>
  69. {
  70. object? result;
  71. var cell = _row.GetCell(x.Value);
  72. if(cell is null)
  73. {
  74. result = null;
  75. }
  76. else
  77. {
  78. result = cell.GetCellType() switch
  79. {
  80. CellType.Formula => cell.GetValue(),
  81. CellType.Numeric => cell.GetDoubleValue(),
  82. CellType.Date => cell.GetDateTimeValue(),
  83. CellType.String => cell.GetValue(),
  84. CellType.Boolean => cell.GetBoolValue(),
  85. _ => null,
  86. };
  87. }
  88. return result;
  89. });
  90. SkipLine();
  91. return results;
  92. }
  93. public bool SkipLine()
  94. {
  95. if (!EndOfData)
  96. {
  97. EndOfData = !rows.MoveNext();
  98. if (!EndOfData)
  99. {
  100. _row = rows.Current;
  101. }
  102. }
  103. return EndOfData;
  104. }
  105. }
  106. }