diff --git a/DOAN.Admin.WebApi/Controllers/MES/Material/productionMaterial/MmMaterialController.cs b/DOAN.Admin.WebApi/Controllers/MES/Material/productionMaterial/MmMaterialController.cs index e039791..519b47e 100644 --- a/DOAN.Admin.WebApi/Controllers/MES/Material/productionMaterial/MmMaterialController.cs +++ b/DOAN.Admin.WebApi/Controllers/MES/Material/productionMaterial/MmMaterialController.cs @@ -2,14 +2,19 @@ using DOAN.Admin.WebApi.Filters; using DOAN.Model; using DOAN.Model.BZFM; using DOAN.Model.BZFM.Dto; +using DOAN.Model.MES.product; using DOAN.Model.System; using DOAN.Model.System.Dto; using DOAN.Service.BZFM; using DOAN.Service.BZFM.IBZFMService; using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; -using System.Linq; +using NPOI.SS.Formula.Functions; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using System.Collections.Generic; +using System.Globalization; +using System.Linq; //创建时间:2025-12-25 namespace DOAN.Admin.WebApi.Controllers.BZFM @@ -127,10 +132,160 @@ namespace DOAN.Admin.WebApi.Controllers.BZFM [ActionPermissionFilter(Permission = "mmmaterial:import")] public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile) { - List material = new(); + //List material = new(); + DateTime dateValue = DateTime.MinValue; using (var stream = formFile.OpenReadStream()) { - material = stream.Query(startCell: "A2").ToList(); + try + { + IWorkbook workbook = new XSSFWorkbook(stream); + ISheet sheet = workbook.GetSheetAt(0); + // 处理第2行 获取日期 + + IRow secondRow = sheet.GetRow(1); + NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0); + + // 将单元格的数字值转换为DateTime + dateValue = cell.DateCellValue.Value; + #region 读取excel + + // 遍历每一行 + for (int row = 4; row <= sheet.LastRowNum; row++) + { + IRow currentRow = sheet.GetRow(row); + if (currentRow != null) // 确保行不为空 + { + MmMaterial material = new MmMaterial(); + + //01 物料标号 + NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(1); + material.Type = currentCell_01?.ToString(); + if (currentCell_01 == null || string.IsNullOrEmpty(material.Type)) + { + continue; + } + + //02物料编码 + NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(2); + + material.MaterialCode = currentCell_02?.ToString(); + if ( + currentCell_02 == null + || string.IsNullOrEmpty(material.MaterialCode) + ) + { + continue; + } + + //03物料名称 + NPOI.SS.UserModel.ICell currentCell_03 = currentRow.GetCell(3); + + material.MaterialName = currentCell_03?.ToString(); + if ( + currentCell_03 == null + || string.IsNullOrEmpty(material.MaterialName) + ) + { + continue; + } + + //04规格 + NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(4); + + material.Specification = currentCell_04?.ToString(); + if (currentCell_04 == null || string.IsNullOrEmpty(material.Specification)) + { + continue; + } + + //05 类型编码 + NPOI.SS.UserModel.ICell currentCell_05 = currentRow.GetCell(5); + + material.CategoryCode = currentCell_05?.ToString(); + if ( + currentCell_05 == null + || string.IsNullOrEmpty(material.CategoryCode) + ) + { + continue; + } + + //06 类型名称 + NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(6); + + material.CategoryName = currentCell_06?.ToString(); + if ( + currentCell_06 == null + || string.IsNullOrEmpty(material.CategoryName) + ) + { + continue; + } + + //07 单位 + NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(7); + + material.Unit = currentCell_07?.ToString(); + if ( + currentCell_07 == null + || string.IsNullOrEmpty(material.Unit) + ) + { + continue; + } + + //08 供应商编码 + NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(8); + + material.SupplierCode = currentCell_08?.ToString(); + if (currentCell_08 == null || string.IsNullOrEmpty(material.SupplierCode)) + { + continue; + } + + //09 供应商名称 + NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(9); + + material.SupplierName = currentCell_09?.ToString(); + if ( + currentCell_09 == null + || string.IsNullOrEmpty(material.SupplierName) + ) + { + continue; + } + + // + + //10 状态 + NPOI.SS.UserModel.ICell currentCell_10 = currentRow.GetCell(10); + + material.Status = currentCell_10?.ToString(); + + if (currentCell_10 == null || string.IsNullOrEmpty(material.Status)) + { + continue; + } + + //10指示日期 + NPOI.SS.UserModel.ICell cell17 = currentRow.GetCell(10); + + // 将单元格的数字值转换为DateTime + material.CreatedTime = cell17.DateCellValue.Value; + + //装箱容积 + /*NPOI.SS.UserModel.ICell currentCell_0811 = currentRow.GetCell(6); + workorder.PackageCapacity = (int)currentCell_0811?.NumericCellValue;*/ + } + } + + #endregion + } + catch (Exception ex) + { + return -1; + } + //material = stream.Query(startCell: "A2").ToList(); } return SUCCESS(_MmMaterialService.Importmaterial(material));