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

141 lines
5.6 KiB
C#
Raw Normal View History

2024-11-13 13:39:06 +08:00
using MiniExcelLibs;
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
using System;
2024-11-13 13:39:06 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Sinvo.EplanHpD.Plugin.WPFUI.Utils
{
public class MotorExcelHelper
{
public static MotorExcelHelper Instance = new();
private Stream dataFileStream;
2024-11-16 08:43:06 +08:00
public const string DATA_FILE_PATH = @"\\192.168.1.160\plm系统文档\线材选型";//@"\\192.168.1.160\plm系统文档\线材选型";
2024-11-13 13:39:06 +08:00
private const string DATA_FILE_NAME = "线材选型数据表.xlsx";
private const string DATA_FILE_NAME_BOM = "BOM表.xlsx";
private const string TEMPLATE_FILE_NAME = "下单定制线模板.xlsx";
public void ReadDataToStream()
{
var file = Path.Combine(DATA_FILE_PATH, DATA_FILE_NAME);
if (File.Exists(file))
{
var bytes = File.ReadAllBytes(file);
dataFileStream = new MemoryStream(bytes);
}
}
public List<LectotypeModel> GetCableDatas()
{
if (dataFileStream == null) return null;
//var filePath = "D:\\Desktop\\Data\\三菱伺服HK-KT线材选型BOM表_按程序格式整理后.xlsx";
var data = MiniExcel.Query<LectotypeModel>(dataFileStream, "HK-KT").ToList();
//MiniExcel.Query()
return data;
}
/// <summary>
/// 1. 100W,
/// 2. 100W带刹车,
/// 3. 200W,
/// 4. 200W带刹车,
/// 5. 400W低惯量,
/// 6. 400W低惯量带刹车,
/// 7. 750W低惯量,
/// 8. 750W低惯量带刹车,
/// 9. 1000W220V,
/// 10.1000W带刹车220V,
/// 11.1500W220V,
/// 12.1500带刹车220V,
/// </summary>
public List<ConfigItemModel> GetMotorPowers()
{
//return new List<ConfigItemModel>
//{
// new(){ ItemName = "100W", ItemValue = "100W"},
// new(){ ItemName = "100W带刹车", ItemValue = "100W带刹车"},
// new(){ ItemName = "200W", ItemValue = "200W"},
// new(){ ItemName = "200W带刹车", ItemValue = "200W带刹车"},
// new(){ ItemName = "400W低惯量", ItemValue = "400W低惯量"},
// new(){ ItemName = "400W低惯量带刹车", ItemValue = "400W低惯量带刹车"},
// new(){ ItemName = "750W低惯量", ItemValue = "750W低惯量"},
// new(){ ItemName = "750W低惯量带刹车", ItemValue = "750W低惯量带刹车"},
// new(){ ItemName = "1000W220V", ItemValue = "1000W220V"},
// new(){ ItemName = "1000W带刹车220V", ItemValue = "1000W带刹车220V"},
// new(){ ItemName = "1500W220V", ItemValue = "1500W220V"},
// new(){ ItemName = "1500带刹车220V", ItemValue = "1500带刹车220V"},
//};
var data = MiniExcel.Query(dataFileStream, sheetName: "电机驱动器", startCell: "A2", useHeaderRow: true).OrderBy(it => it.).Select(it => new ConfigItemModel
{
ItemName = it.,
ItemValue = it.,
ItemFlag = it.
}).ToList();
return data.Distinct(new LambdaComparer<ConfigItemModel>(
(a, b) => a.ItemName == b.ItemName && a.ItemValue == b.ItemValue && b.ItemFlag == a.ItemFlag,
obj => obj.ToString().GetHashCode()
)).ToList();
}
public List<LineBomModel> GetBomList(string drawNo)
{
//var filePath = "D:\\Desktop\\Data\\BOM表.xlsx";
var filePath = Path.Combine(DATA_FILE_PATH, DATA_FILE_NAME_BOM);
var data = MiniExcel.Query<LineBomModel>(filePath).Where(it => it.DrawingNo == drawNo).ToList();
//MiniExcel.Query<>()
return data;
}
public void SaveBomListToExcel(string targetPath, List<LineBomItemModel> bomList, string cableModelNo)
{
MiniExcel.SaveAs(
targetPath,
bomList
);
}
public bool SaveLinesToExcel(string targetPath, List<LectotypeLineModel> lines)
{
try
{
var templatePath = Path.Combine(DATA_FILE_PATH, TEMPLATE_FILE_NAME);
var data = lines.Select(line => new
{
line.ItemSeqNo,
line.SeqNo,
line.MechanicalNo,
line.MechanicalName,
line.CableModelNo,
line.CableType,
line.AxisNo,
line.DrawingNo,
EncoderLineLength = line.EncoderLineLength == 0 ? "" : line.EncoderLineLength.ToString(),
PowerLineLength = line.PowerLineLength == 0 ? "" : line.PowerLineLength.ToString(),
}).ToList();
MiniExcel.SaveAsByTemplate(
$"{targetPath}\\线材下单_{DateTime.Now:yyyy_MM_dd}.xlsx",
templatePath,
new
{
data
},
new MiniExcelLibs.OpenXml.OpenXmlConfiguration
{
FastMode = true
}
);
return true;
}
catch (System.Exception)
{
throw;
}
}
2024-11-13 13:39:06 +08:00
public void CloseStream()
{
dataFileStream.Close();
}
}
}