using NPOI.SS.UserModel; using System; using System.Collections.Generic; namespace Infrastructure.Office { public static class NPOIExtensions { public static IEnumerable GetSheets(this IWorkbook wookbook) { if (wookbook is null) { throw new ArgumentNullException(nameof(wookbook)); } for (int i = 0; i < wookbook.NumberOfSheets; i++) { yield return wookbook.GetSheetAt(i); } } public static IEnumerable GetRows(this ISheet sheet) { if (sheet is null) { throw new ArgumentNullException(nameof(sheet)); } for (int i = 0; i < sheet.LastRowNum; i++) { yield return sheet.GetRow(i); } } public static IEnumerable GetCells(this IRow row) { if (row is null) { throw new ArgumentNullException(nameof(row)); } for (int i = 0; i < row.LastCellNum; i++) { yield return row.GetCell(i); } } public static object GetValue(this ICell cell) { if (cell is null) { throw new ArgumentNullException(nameof(cell)); } object result = cell.CellType switch { CellType.Unknown => cell.ToString().Trim(), CellType.Numeric => cell.NumericCellValue, CellType.String => cell.StringCellValue.Trim(), CellType.Formula => cell.CellFormula, CellType.Blank => null, CellType.Boolean => cell.BooleanCellValue, CellType.Error => cell.ErrorCellValue, _ => cell.ToString().Trim(), }; return result; } } }