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 lazy = new Lazy(() => new FileHelper()); public static FileHelper Instance { get { return lazy.Value; } } #endregion private FileHelper() { } /// /// 读取Excel内容 /// /// public List ReadExceFile(string filePath) { List result = null; try { string fileName = GetFileName(filePath); if (string.IsNullOrEmpty(fileName)) { return result; } var sheetNames = MiniExcel.GetSheetNames(filePath); if (sheetNames != null) { result = new List(); foreach (string sheetName in sheetNames) { //根据sheet读取文档内容 List 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; } /// /// 获取产品型号 /// /// /// 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; } } } }