/// <summary> /// 导入Excel到库A,并返回原油的oilInfoID /// </summary> /// <param name="path">excel的路径</param> /// <returns></returns> public static OilInfoEntity importExcel(string fileName) { OilInfoEntity oilA = null; try { DataSet ds = ExcelTool.ExcelToDataSet(fileName); #region "原油信息表的处理" List <string> oilInfoItemCode = OilTableRowBll._OilTableRow.Where(d => d.oilTableTypeID == (int)EnumTableType.Info).Select(d => d.itemCode).Distinct().ToList(); oilA = findOilInfoTalbe(ds, oilInfoItemCode); //首先要找到原油信息表,没有原油信息表 则没必要继续下去 if (oilA.ID < 0) { return(oilA); } #endregion List <string> allItemCode = OilTableRowBll._OilTableRow.Where(d => new int[] { (int)EnumTableType.Whole, (int)EnumTableType.Light, (int)EnumTableType.Narrow, (int)EnumTableType.Wide, (int)EnumTableType.Residue }.Contains(d.oilTableTypeID)).Select(d => d.itemCode).Distinct().ToList(); List <WCol> tableColList = new List <WCol>(); //记录所有窄馏分、宽馏分、渣油 数据列集合 foreach (DataTable table in ds.Tables) //找到原油信息表后,遍历每个表 { tableColList.AddRange(getColListFromTable(table, allItemCode)); } #region "对宽馏分数据处理,如果出现ICP,ECP相同的重复列则合并。" var items = tableColList.Where(d => d.TableType == EnumTableType.Wide).GroupBy(x => new { x.ICP, x.ECP }).Select(d => new { keys = d.Key, Count = d.Count() }); foreach (var i in items) { if (i.Count > 1) { List <WCol> mergeList = tableColList.Where(d => d.TableType == EnumTableType.Wide && d.ICP == i.keys.ICP && d.ECP == i.keys.ECP).ToList(); WCol newCol = new WCol(); newCol.ECP = i.keys.ECP; newCol.ICP = i.keys.ICP; newCol.TableType = EnumTableType.Wide; foreach (var mergeCol in mergeList) { newCol.Cells.AddRange(mergeCol.Cells); tableColList.Remove(mergeCol); } tableColList.Add(newCol); } } #endregion //排序,找出宽馏分的,根据iep排序,渣油也排序 List <WCol> wideData = tableColList.Where(d => d.TableType == EnumTableType.Wide).OrderBy(d => d.MCP).ToList(); WriteToOilA(ref oilA, wideData, EnumTableType.Wide); wideData = tableColList.Where(d => d.TableType == EnumTableType.Whole).ToList(); WriteToOilA(ref oilA, wideData, EnumTableType.Whole); wideData = tableColList.Where(d => d.TableType == EnumTableType.Narrow).ToList(); WriteToOilA(ref oilA, wideData, EnumTableType.Narrow); wideData = tableColList.Where(d => d.TableType == EnumTableType.Residue).OrderBy(d => d.MCP).ToList(); WriteToOilA(ref oilA, wideData, EnumTableType.Residue); wideData = tableColList.Where(d => d.TableType == EnumTableType.Light).ToList(); WriteToOilA(ref oilA, wideData, EnumTableType.Light); } catch (Exception ex) { Log.Error("数据管理,镇海导入Excel到库原始库:" + ex); return(null); } return(oilA); }