导入导出

This commit is contained in:
2026-01-08 10:50:03 +08:00
parent 0de782eaa9
commit 2125ef8e9e
2 changed files with 155 additions and 162 deletions

View File

@@ -14,6 +14,7 @@ using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
//创建时间2025-12-25
@@ -132,152 +133,8 @@ namespace DOAN.Admin.WebApi.Controllers.BZFM
[ActionPermissionFilter(Permission = "mmmaterial:import")]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
List<MmMaterialExcelDto> materialList = new();
DateTime dateValue = DateTime.MinValue;
using (var stream = formFile.OpenReadStream())
{
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;
}
}
}
#endregion
}
catch (Exception ex)
{
//return -1;
}
material = stream.Query<MmMaterialExcelDto>(startCell: "A2").ToList();
}
List<MmMaterialExcelDto> material = new();
material = stream.Query<MmMaterialExcelDto>(startCell: "A2").ToList();
return SUCCESS(_MmMaterialService.Importmaterial(material));
}

View File

@@ -1,6 +1,7 @@
using DOAN.Common;
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.Repository;
@@ -8,6 +9,10 @@ using DOAN.Service.BZFM.IBZFMService;
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Http;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace DOAN.Service.BZFM
{
@@ -140,27 +145,158 @@ namespace DOAN.Service.BZFM
/// <returns></returns>
public ImportResultDto Importmaterial(List<MmMaterialExcelDto> material)
{
// normalize and set defaults, do not overwrite provided values
material.ForEach(x =>
int result = 0;
List<ProWorkorder> materialList = new();
DateTime dateValue = DateTime.MinValue;
// 获取 产品代号
List<MmMaterialExcelDto> ProductCodeList = Context
.Queryable<MmMaterialExcelDto>()
.Where(it => it.Type == "type")
.ToList();
using (var stream = formFile.OpenReadStream())
{
if (x.CreatedTime == null)
try
{
x.CreatedTime = DateTime.Now;
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;
}
}
}
#endregion
}
if (string.IsNullOrWhiteSpace(x.Status))
catch (Exception ex)
{
x.Status = "启用";
//return -1;
}
if (!string.IsNullOrWhiteSpace(x.MaterialCode))
{
x.MaterialCode = x.MaterialCode.Trim();
}
if (!string.IsNullOrWhiteSpace(x.MaterialName))
{
x.MaterialName = x.MaterialName.Trim();
}
x.Description = x.Description.IsEmpty() ? "数据导入" : x.Description;
});
}
var x = Context
.Storageable(material)
.SplitInsert(it => !it.Any())