ExcelFileReader.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 readonly Spreadsheet _sheet;
  17. private IEnumerator<IRow> _rows;
  18. public Dictionary<string, int> Columns { get; set; } = new();
  19. [MemberNotNullWhen(false, nameof(_row))]
  20. public bool EndOfData { get; private set; }
  21. private IRow? _row;
  22. public ExcelFileReader(Stream stream)
  23. {
  24. _sheet = new Spreadsheet(stream);
  25. SelectSheet(0);
  26. }
  27. public void SelectSheet(int sheet)
  28. {
  29. _rows = _sheet.GetSheet(sheet).RowEnumerator();
  30. Columns.Clear();
  31. SkipLine();
  32. }
  33. public IList<string> ReadLineValues()
  34. {
  35. var results = new List<string>();
  36. if (!EndOfData)
  37. {
  38. foreach (var cell in _row.Cells())
  39. {
  40. results.Add(cell.GetValue());
  41. }
  42. SkipLine();
  43. }
  44. return results;
  45. }
  46. public bool ReadHeader()
  47. {
  48. Columns.Clear();
  49. if (EndOfData)
  50. {
  51. return false;
  52. }
  53. int i = 0;
  54. foreach(var cell in _row.Cells())
  55. {
  56. var column = cell.GetValue();
  57. if (!column.IsNullOrWhiteSpace())
  58. {
  59. Columns.Add(column, i);
  60. }
  61. ++i;
  62. }
  63. SkipLine();
  64. return true;
  65. }
  66. public Dictionary<string, object?> ReadLine()
  67. {
  68. if (EndOfData)
  69. {
  70. return new Dictionary<string, object?>();
  71. }
  72. var results = Columns.ToDictionary(
  73. x => x.Key,
  74. x =>
  75. {
  76. object? result;
  77. var cell = _row.GetCell(x.Value);
  78. if(cell is null)
  79. {
  80. result = null;
  81. }
  82. else
  83. {
  84. result = cell.GetCellType() switch
  85. {
  86. CellType.Formula => cell.GetValue(),
  87. CellType.Numeric => cell.GetDoubleValue(),
  88. CellType.Date => cell.GetDateTimeValue(),
  89. CellType.String => cell.GetValue(),
  90. CellType.Boolean => cell.GetBoolValue(),
  91. _ => null,
  92. };
  93. }
  94. return result;
  95. });
  96. SkipLine();
  97. return results;
  98. }
  99. public bool SkipLine()
  100. {
  101. if (!EndOfData)
  102. {
  103. EndOfData = !_rows.MoveNext();
  104. if (!EndOfData)
  105. {
  106. _row = _rows.Current;
  107. }
  108. }
  109. return EndOfData;
  110. }
  111. }
  112. }