This commit is contained in:
qianhao.xu
2024-04-15 18:25:01 +08:00
parent afef8ba745
commit c63c5e9418
11 changed files with 65 additions and 17 deletions

View File

@@ -1,4 +1,8 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
@@ -127,7 +131,7 @@ namespace ZR.Admin.WebApi.Controllers
}
[HttpGet("importData")]
[HttpPost("importData")]
[Log(Title = "物料清单批量导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[AllowAnonymous] //不需要授权 就可以访问
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
@@ -145,7 +149,7 @@ namespace ZR.Admin.WebApi.Controllers
}
//2.0 解析 excel
List<WmMaterial> materials = new List<WmMaterial>();
using (var stream = formFile.OpenReadStream())
{
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
@@ -157,27 +161,49 @@ namespace ZR.Admin.WebApi.Controllers
targetFileStream.Write(buffer, 0, bytesRead);
}
stream.Position = 0; //这一句不加就会上面的异常错误
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表
//IWorkbook workbook = new XSSFWorkbook(stream);
//ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表
//for (int i = 0; i <= sheet.LastRowNum; i++)
//{
// IRow row = sheet.GetRow(i);
// if (row != null)
// {
// for (int j = 0; j < row.LastCellNum; j++)
// {
// Console.Write(row.GetCell(j) + "\t");
// }
// Console.WriteLine();
// }
//}
for (int i = 2; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row != null)
{
if(row.GetCell(0)!=null &&row.GetCell(0).ToString()!="")
{
WmMaterial material = new WmMaterial();
material.Partnumber = row.GetCell(0)?.ToString();
material.U8InventoryCode = row.GetCell(1)?.ToString();
material.BlankNum = row.GetCell(2)?.ToString();
material.Unit = row.GetCell(3)?.ToString();
material.ProductName = row.GetCell(4)?.ToString();
material.Color = row.GetCell(5)?.ToString();
material.Specification = row.GetCell(6)?.ToString();
material.Description = row.GetCell(7)?.ToString();
material.Version = row.GetCell(8)?.ToString();
material.Remarks = row.GetCell(9)?.ToString();
material.Search1 = row.GetCell(10)?.ToString();
material.Search2 = row.GetCell(11)?.ToString();
material.Status = 1;
material.Sort = i;
material.ToCreate(HttpContext);
materials.Add(material);
}
}
}
}
(int,int) result= _WmMaterialService.ExcelADD(materials);
return null;
return SUCCESS(result);
}

View File

@@ -22,5 +22,8 @@ namespace ZR.Service.mes.wms.IService
WmGoodsNowProduction GetInfoByPatchCode(string patchCode);
(int,int) ExcelADD(List<WmMaterial> materials);
}
}

View File

@@ -116,5 +116,24 @@ namespace ZR.Service.mes.wms
.Where(it => it.PackageCodeClient == patchCode).First();
}
/// <summary>
/// excel 插入或者更新
/// </summary>
/// <param name="materials"></param>
/// <returns></returns>
public (int, int) ExcelADD(List<WmMaterial> materials)
{
if(materials == null || materials.Count == 0)
{
return (-1,-1);
}
var x=Context.Storageable(materials)
.WhereColumns(it => it.BlankNum).ToStorage();
int insert= x.AsInsertable.ExecuteCommand(); //执行插入
int update= x.AsUpdateable.ExecuteCommand(); //执行更新 
return (insert, update);
}
}
}