NpoiHelper.cs 3.56 KB
using HHECS.BllModel;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Models;
using NPOI.XSSF.UserModel;

namespace HHECS.Tool.Utils
{
    internal static class NpoiHelper
    {
        public static BllResult<List<Equipment>> GetEquipmentData(string filePath)
        {
            try
            {
                using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                var hssfworkbook = new XSSFWorkbook(file);
                var equipmentSheet = hssfworkbook.GetSheetAt(0);
                var equipmentPropSheet = hssfworkbook.GetSheetAt(1);
                var equipments = new List<Equipment>();
                //设备
                for (int i = 1; i <= equipmentSheet.LastRowNum; i++)
                {
                    var row = equipmentSheet.GetRow(i);
                    if (row == null)
                    {
                        continue;
                    }
                    equipments.Add(new Equipment
                    {
                        Id = (int)row.GetCell(0).NumericCellValue,
                        Code = row.GetCell(1).StringCellValue,
                        Name = row.GetCell(2).StringCellValue,
                        EquipmentTypeId = (int)row.GetCell(3).NumericCellValue,
                        IP = row.GetCell(4).StringCellValue,
                        DestinationArea = row.GetCell(5).NumericCellValue.ToString(),
                        Description = row.GetCell(6).StringCellValue,
                        Disable = false,
                        ConnectName = row.GetCell(7).StringCellValue,
                        ProjectCode = row.GetCell(8).StringCellValue,
                        FactoryCode = row.GetCell(9).StringCellValue,
                    });
                }

                //设备属性
                for (int i = 1; i <= equipmentPropSheet.LastRowNum; i++)
                {
                    var row = equipmentPropSheet.GetRow(i);
                    if (row == null)
                    {
                        continue;
                    }
                    _ = Enum.TryParse<EquipmentPropType>(row.GetCell(4).StringCellValue, out var propType);
                    _ = Enum.TryParse<EquipmentDataType>(row.GetCell(5).StringCellValue, out var dataType);
                    var equipmentProp = new EquipmentProp
                    {
                        EquipmentId = (int)row.GetCell(0).NumericCellValue,
                        Code = row.GetCell(1).StringCellValue,
                        Name = row.GetCell(2).StringCellValue,
                        Address = row.GetCell(3).StringCellValue,
                        PropType = propType,
                        DataType = dataType,
                        MonitorCompareValue = row.GetCell(6)?.BooleanCellValue.ToString() ?? string.Empty,
                        MonitorFailure = row.GetCell(7)?.StringCellValue ?? string.Empty,
                    };
                    var equipment = equipments.Where(x => x.Id == equipmentProp.EquipmentId).FirstOrDefault();
                    if (equipment == null)
                    {
                        return BllResultFactory.Error<List<Equipment>>($"设备属性{equipmentProp.Code}未绑定设备!文件路径:“{filePath}”,行号:{i}");
                    }
                    equipment.EquipmentProps.Add(equipmentProp);
                }

                return BllResultFactory.Success(equipments);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<Equipment>>(ex.Message);
            }
        }
    }
}