导入导出
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user