EPLAN_PROD_Plugin/Sinvo.EplanHpD.Plugin.WPFUI/Utils/ExcelHelper.cs

101 lines
3.8 KiB
C#

using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Sinvo.EplanHpD.Plugin.WPFUI.Utils
{
public class ExcelHelper
{
private static IEnumerable<ExcelModel> _terminalMappingCache = [];
/// <summary>
/// 线材端子对照表
/// </summary>
public static IEnumerable<ExcelModel> GetWireTerminalMappingTable(string wireModel, string prefix = "")
{
CheckAndGetCache();
return _terminalMappingCache.Where(item => item.WireModelSpecification == wireModel && (item.Prefix == prefix || prefix == ""));
}
/// <summary>
/// 获取线材信息
/// </summary>
public static IEnumerable<ExcelModel> GetWireInfo(string wireModel, string prefix = "")
{
CheckAndGetCache();
return _terminalMappingCache.Where(item => item.WireModelSpecification == wireModel && (item.Prefix == prefix || item.Prefix == null));
}
public static void CheckAndGetCache(bool isForceReload = false)
{
lock (_terminalMappingCache)
{
if ((_terminalMappingCache == null || !_terminalMappingCache.Any()) || isForceReload)
{
GetAllWireTerminalMappingTable();
}
}
}
private static void GetAllWireTerminalMappingTable()
{
var config = new OpenXmlConfiguration()
{
FillMergedCells = true
};
var path = Path.Combine(Consts.DATA_FILE_PATH, Consts.DATA_FILE_PATH_WIRE_TERMINAL);
var result = MiniExcel.Query<ExcelModel>(path, configuration: config)
//.Where(item => item.WireModelSpecification == wireModel)
.ToList();
_terminalMappingCache = result;
}
/// <summary>
/// 绝缘软套
/// </summary>
public static IEnumerable<InsulationModel> GetInsulationSoftSleeveTable(string wireModel)
{
var path = Path.Combine(Consts.DATA_FILE_PATH, Consts.DATA_FILE_PATH_INSULATION);
var results = MiniExcel.Query<InsulationModel>(path);
//.Where(item => item.Specification == wireModel)
//.ToList();
return results.Where(item => item.Specification == wireModel);
}
//public static List<IDictionary<string, object>> GetWireTerminalMappingTable()
//{
// var config = new OpenXmlConfiguration()
// {
// FillMergedCells = true
// };
// return MiniExcel.Query("D:\\Desktop\\123\\线材端子号码管配对表(配CE端子)2024-10-18.xlsm", useHeaderRow: true, configuration: config)
// //.Where(item => item.WireModelSpecification == wireModel)
// .Cast<IDictionary<string, object>>()
// .ToList();
//}
public static dynamic GetNumberTube(string tubeModel)
{
CheckAndGetCache();
return _terminalMappingCache.Where(item => item.TubeModel == tubeModel)
.Select(item => new
{
item.TubeModel,
item.TubeMaterialCode,
})
.FirstOrDefault();
}
public static void SaveByTemplate(object data, string newFilePath)
{
var config = new OpenXmlConfiguration()
{
IgnoreTemplateParameterMissing = true,
};
var path = Path.Combine(Consts.DATA_FILE_PATH, Consts.TEMPLATE_FILE_NAME);
MiniExcel.SaveAsByTemplate(newFilePath, path, new
{
items = data
}, configuration: config);
}
}
}