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 _insulations; private static IEnumerable _terminalMappingCache = []; /// /// 线材端子对照表 /// public static IEnumerable GetWireTerminalMappingTable(string wireModel, string prefix = "") { CheckAndGetCache(); return _terminalMappingCache.Where(item => item.WireModelSpecification == wireModel && (item.Prefix == prefix || prefix == "")); } /// /// 获取线材信息 /// public static IEnumerable 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(path, configuration: config) //.Where(item => item.WireModelSpecification == wireModel) .ToList(); _terminalMappingCache = result; } /// /// 绝缘软套 /// public static IEnumerable GetInsulationSoftSleeveTable(string wireModel) { if(_insulations != null) { return _insulations.Where(item => item.Specification == wireModel); } var path = Path.Combine(Consts.DATA_FILE_PATH, Consts.DATA_FILE_PATH_INSULATION); var results = MiniExcel.Query(path); if(_insulations == null) { _insulations = results; } //.Where(item => item.Specification == wireModel) //.ToList(); return results.Where(item => item.Specification == wireModel); } //public static List> 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>() // .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); } } }