You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Office/NPOIExtensions.cs

66 lines
1.9 KiB

using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
namespace Infrastructure.Office
{
public static class NPOIExtensions
{
public static IEnumerable<ISheet> 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<IRow> 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<ICell> 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;
}
}
}