141 lines
5.6 KiB
C#
141 lines
5.6 KiB
C#
using MiniExcelLibs;
|
||
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
|
||
using System;
|
||
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;
|
||
public const string DATA_FILE_PATH = @"\\192.168.1.160\plm系统文档\线材选型";//@"\\192.168.1.160\plm系统文档\线材选型";
|
||
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. 1000W(220V),
|
||
/// 10.1000W带刹车(220V),
|
||
/// 11.1500W(220V),
|
||
/// 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 = "1000W(220V)", ItemValue = "1000W(220V)"},
|
||
// new(){ ItemName = "1000W带刹车(220V)", ItemValue = "1000W带刹车(220V)"},
|
||
// new(){ ItemName = "1500W(220V)", ItemValue = "1500W(220V)"},
|
||
// 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;
|
||
}
|
||
}
|
||
|
||
public void CloseStream()
|
||
{
|
||
dataFileStream.Close();
|
||
}
|
||
}
|
||
}
|