111
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.IdentityModel.Tokens;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Model.MES.wms;
|
||||
@@ -136,74 +137,82 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
[AllowAnonymous] //不需要授权 就可以访问
|
||||
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile, bool updateSupport)
|
||||
{
|
||||
|
||||
//1.0 读取excel 文件 保存在指定位置
|
||||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||
string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName;
|
||||
string target = Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial", sFileName);
|
||||
if (!Directory.Exists(Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial")))
|
||||
try
|
||||
{
|
||||
// 如果目录不存在就创建
|
||||
Directory.CreateDirectory(Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial"));
|
||||
|
||||
}
|
||||
//2.0 解析 excel
|
||||
|
||||
List<WmMaterial> materials = new List<WmMaterial>();
|
||||
using (var stream = formFile.OpenReadStream())
|
||||
{
|
||||
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
|
||||
//1.0 读取excel 文件 保存在指定位置
|
||||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||
string sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + formFile.FileName;
|
||||
string target = Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial", sFileName);
|
||||
if (!Directory.Exists(Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial")))
|
||||
{
|
||||
targetFileStream.Write(buffer, 0, bytesRead);
|
||||
// 如果目录不存在就创建
|
||||
Directory.CreateDirectory(Path.Combine(webHostEnvironment.WebRootPath, "wmmaterial"));
|
||||
|
||||
}
|
||||
//2.0 解析 excel
|
||||
|
||||
stream.Position = 0; //这一句不加就会上面的异常错误
|
||||
IWorkbook workbook = new XSSFWorkbook(stream);
|
||||
ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表
|
||||
|
||||
|
||||
|
||||
|
||||
for (int i = 2; i <= sheet.LastRowNum; i++)
|
||||
List<WmMaterial> materials = new List<WmMaterial>();
|
||||
using (var stream = formFile.OpenReadStream())
|
||||
{
|
||||
IRow row = sheet.GetRow(i);
|
||||
if (row != null)
|
||||
{
|
||||
if(row.GetCell(0)!=null &&row.GetCell(0).ToString()!="")
|
||||
{
|
||||
WmMaterial material = new WmMaterial();
|
||||
FileStream targetFileStream = new FileStream(target, FileMode.Create, FileAccess.Write);
|
||||
|
||||
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);
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
targetFileStream.Write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
stream.Position = 0; //这一句不加就会上面的异常错误
|
||||
IWorkbook workbook = new XSSFWorkbook(stream);
|
||||
ISheet sheet = workbook.GetSheetAt(0); // 读取第一个工作表
|
||||
|
||||
|
||||
|
||||
|
||||
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 SUCCESS(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ToResponse(new ApiResult(210, "文件异常"+ex.Message));
|
||||
}
|
||||
|
||||
(int,int) result= _WmMaterialService.ExcelADD(materials);
|
||||
|
||||
return SUCCESS(result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user