/// <summary> /// 从excel文件导入到入库表 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static List <GetPart> GetgetPartTableFromExcel(string filePath) { var parts = new List <GetPart>(); // List<GetPart> parts = new List<GetPart>(); HSSFWorkbook hssfWorkbook; using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { hssfWorkbook = new HSSFWorkbook(fileStream); } var sheet = hssfWorkbook.GetSheetAt(0); var rows = sheet.GetRowEnumerator(); rows.MoveNext(); using (PartContext pc = new PartContext()) { while (rows.MoveNext()) { HSSFRow row = (HSSFRow)rows.Current; GetPart part = new GetPart(); if (row.FirstCellNum < 0) { continue; } if (row.FirstCellNum == 0) { part.PartNum = row.GetCell(0).ToString().Trim(); part.PartName = row.GetCell(1).ToString().Trim(); part.PartType = row.GetCell(2).ToString().Trim(); part.Unit = row.GetCell(3).ToString().Trim(); //bool b = row.GetCell(4) == null; part.Price = row.GetCell(4) == null?0:(decimal?)row.GetCell(4).NumericCellValue; part.GetNum = (long)row.GetCell(5).NumericCellValue; } else { part.PartNum = ""; part.PartName = row.Cells[0].ToString().Trim(); part.PartType = row.Cells[1].ToString().Trim(); part.Unit = row.Cells[2].ToString().Trim(); part.Price = (decimal?)row.Cells[3].NumericCellValue; part.GetNum = (long)row.Cells[4].NumericCellValue; } part.GetTime = DateTime.Now.ToShortDateString(); parts.Add(part); // pc.GetParts.Add(part); } //pc.SaveChanges(); } MessageBox.Show($"成功导入{parts.Count}条数据"); return(parts); }
/// <summary> /// 从getpart转换到Part /// </summary> /// <param name="getPart">要转换的实体</param> /// <returns></returns> static Part ConvertGetPartToPart(GetPart getPart) { Part part = new Part() { PartNum = getPart.PartNum, PartName = getPart.PartName, PartType = getPart.PartType, Price = getPart.Price, Unit = getPart.Unit, Num = getPart.GetNum }; return(part); }