You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.2 KiB
C#

using MiniExcelLibs;
using SlnMesnac.LabelPrint.Log4net;
using SlnMesnac.LabelPrint.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SlnMesnac.LabelPrint.FileOperate
{
public class FileHelper
{
private readonly LogHelper logHelper = LogHelper.Instance;
#region 单例实现
private static readonly Lazy<FileHelper> lazy = new Lazy<FileHelper>(() => new FileHelper());
public static FileHelper Instance
{
get
{
return lazy.Value;
}
}
#endregion
private FileHelper() { }
/// <summary>
/// 读取Excel内容
/// </summary>
/// <param name="filePath"></param>
public List<BaseLabelInfo> ReadExceFile(string filePath)
{
List<BaseLabelInfo> result = null;
try
{
string fileName = GetFileName(filePath);
if (string.IsNullOrEmpty(fileName))
{
return result;
}
var sheetNames = MiniExcel.GetSheetNames(filePath);
if (sheetNames != null)
{
result = new List<BaseLabelInfo>();
foreach (string sheetName in sheetNames)
{
//根据sheet读取文档内容
List<dynamic> info = MiniExcel.Query(filePath, sheetName: sheetName).ToList();
if (info != null)
{
for (int i = 1; i < info.Count; i++)
{
//解析数据
dynamic labelDynamic = info[i];
BaseLabelInfo labelInfo = new BaseLabelInfo();
labelInfo.label = labelDynamic;
labelInfo.productType = fileName;
labelInfo.column_A = labelDynamic.A;
if (fileName == "Pallet")
{
labelInfo.recordTime = DateTime.Now;
result.Add(labelInfo);
continue;
}
labelInfo.column_B = labelDynamic.B;
labelInfo.column_C = labelDynamic.C;
labelInfo.column_D = labelDynamic.D;
if(fileName == "Gas" || fileName == "TF" || fileName == "Drum")
{
labelInfo.recordTime = DateTime.Now;
result.Add(labelInfo);
continue;
}
labelInfo.column_E = labelDynamic.E;
if (fileName == "Unit")
{
if (labelInfo.column_B == null)
{
continue;
}
else
{
labelInfo.recordTime = DateTime.Now;
result.Add(labelInfo);
continue;
}
}
//labelInfo.column_F = labelDynamic.F;
//labelInfo.column_G = labelDynamic.G;
//labelInfo.column_H = labelDynamic.H;
//labelInfo.column_I = labelDynamic.I;
//labelInfo.column_J = labelDynamic.J;
//labelInfo.recordTime = DateTime.Now;
//result.Add(labelInfo);
}
}
}
}
}
catch (Exception ex)
{
logHelper.Error("Excel文件读取异常", ex);
}
return result;
}
/// <summary>
/// 获取产品型号
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private string GetFileName(string filePath)
{
if (filePath.Contains("Gas"))
{
return "Gas";
}else if (filePath.Contains("TF"))
{
return "TF";
}
else if (filePath.Contains("Drum"))
{
return "Drum";
}
else if (filePath.Contains("Unit"))
{
return "Unit";
}
else if (filePath.Contains("Pallet"))
{
return "Pallet";
}
else
{
return string.Empty;
}
}
}
}