public static List <FaHuoInfo> ReadFaHuoInfosExcel(MemoryStream ms) { var mapper = new Mapper(ms); mapper.Map <FaHuoInfo>("序号", o => o.CustomerId) .Map(column => true, (column, target) => { if (column.CurrentValue == null) { return(true); } var inx = column.Attribute.Index / 2; var mod = column.Attribute.Index % 2; var isNew = mod == 1; var faHuo = target as FaHuoInfo; if (faHuo == null) { throw new Exception("读取发货任务信息Excel数据出错!"); } FaHuoItem item = isNew ? new FaHuoItem() : faHuo.Items.Last(); var tmpCellVal = column.CurrentValue?.ToString() ?? string.Empty; if (column.HeaderValue.Equals("型号")) { item.XingHao = tmpCellVal; } if (column.HeaderValue.Equals("长度")) { //int.TryParse(tmpCellVal, out int tmpval); decimal.TryParse(tmpCellVal, out decimal tmpval); item.Length = tmpval; } if (isNew) { faHuo.Items.Add(item); } return(true); }); var data = mapper.Take <FaHuoInfo>("发货任务信息"); if (data != null && data.Any()) { return(data.Select(m => m.Value).ToList()); } throw new Exception("获取发货任务信息出错!"); }
public static async Task <List <FaHuoInfo> > ReadFaHuoInfosExcel(string filePath) { var result = await Task.Run(() => { List <FaHuoInfo> viewData = null; using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var mapper = new Mapper(fs); mapper.Map <FaHuoInfo>("序号", o => o.CustomerId) .Map(column => true, (column, target) => { if (column.CurrentValue == null) { return(true); } var inx = column.Attribute.Index / 2; var mod = column.Attribute.Index % 2; var isNew = mod == 1; var faHuo = target as FaHuoInfo; if (faHuo == null) { throw new Exception("读取发货任务信息Excel数据出错!"); } FaHuoItem item = isNew ? new FaHuoItem() : faHuo.Items.Last(); var tmpCellVal = column.CurrentValue?.ToString() ?? string.Empty; if (column.HeaderValue.Equals("型号")) { item.XingHao = tmpCellVal; } if (column.HeaderValue.Equals("长度")) { int.TryParse(tmpCellVal, out int tmpval); item.Length = tmpval; } if (isNew) { faHuo.Items.Add(item); } return(true); }); var data = mapper.Take <FaHuoInfo>("发货任务信息"); if (data != null && data.Any()) { viewData = data.Select(m => m.Value).ToList(); } } return(viewData); }); return(result ?? throw new Exception("获取发货任务信息出错!")); }