BOM 修正

This commit is contained in:
gcw_MV9p2JJN 2025-11-18 14:34:26 +08:00
parent 1741c0a2ac
commit 8a0f6571d7
16 changed files with 664 additions and 3 deletions

View File

@ -17,7 +17,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AlibabaCloud.SDK.Dingtalk" Version="2.1.18" />
<PackageReference Include="NPOI" Version="2.7.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DOAN.Model\DOAN.Model.csproj" />

View File

@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="NETCore.Encrypt" Version="2.1.1" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NPOI" Version="2.7.5" />
</ItemGroup>
</Project>

View File

@ -210,10 +210,14 @@ namespace Infrastructure.Controllers
protected (string, string) DownloadImportTemplate(string fileName)
{
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
// string path= "./Assets/ImportTemplate";
string sFileName = $"{fileName}.xlsx";
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", sFileName);
return (sFileName, fullPath);
}
}
}

Binary file not shown.

View File

@ -13,6 +13,7 @@ using MDM.Services.IMaterialService;
using MDM.Model.Material.Dto;
using MDM.Model.Material;
using MDM.Repository;
using DOAN.Model.MES.base_.Dto;
//创建时间2025-11-15
namespace MDM.Controllers.Material
@ -112,6 +113,22 @@ namespace MDM.Controllers.Material
}
//TODO 获取母件
[HttpPost("get_monter_inv")]
public IActionResult GetMonterInvList([FromBody] MaterialBomQueryDto parm)
{
var response = _MaterialBomService.GetMonterInvList(parm);
return SUCCESS(response);
}
//TODO 获取子件
[HttpPost("get_son_inv")]
public IActionResult GetSonInvList([FromBody] MaterialBomQueryDto parm)
{
var response = _MaterialBomService.GetSonInvList(parm);
return SUCCESS(response);
}
}

View File

@ -12,6 +12,9 @@ using Infrastructure.Model;
using MDM.Services.IMaterialService;
using MDM.Model.Material.Dto;
using MDM.Model.Material;
using MDM.Services.Material;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
//创建时间2025-11-15
namespace MDM.Controllers.Material
@ -111,6 +114,54 @@ namespace MDM.Controllers.Material
}
//TODO 导出模板
[HttpGet("importTemplate")]
[Log(Title = "物料类型模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ImportTemplateExcel()
{
string path = "./Assets/ImportTemplate";
string sFileName = $"MaterialMODEL.xlsx";
string fullPath = Path.Combine(path, sFileName);
(string, string) result = (sFileName, fullPath);
return ExportExcel(result.Item2, result.Item1);
}
//TODO 导入excel
[HttpPost("importData")]
[Log(Title = "物料类型导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[AllowAnonymous]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
if (formFile == null)
{
return SUCCESS(null);
}
int response = _MaterialListService.ImportData(formFile, HttpContext.GetName());
return SUCCESS(response);
}
//TODO 导出excel
[HttpGet("exportData")]
[Log(Title = "物料类型导出", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ExportData()
{
var excelBytes = _MaterialListService.ExportData();
string fileName = $"PlanAchievementRate_{DateTime.Now.Date:yyyyMMdd}.xlsx";
return File(
excelBytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
fileName
);
}
}

View File

@ -12,6 +12,11 @@ using Infrastructure.Model;
using MDM.Services.IMaterialService;
using MDM.Model.Material.Dto;
using MDM.Model.Material;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using NPOI.HPSF;
using DOAN.Model.Mobile.Dto;
//创建时间2025-11-15
namespace MDM.Controllers.Material
@ -111,6 +116,54 @@ namespace MDM.Controllers.Material
}
//TODO 导出模板
[HttpGet("importTemplate")]
[Log(Title = "物料类型模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ImportTemplateExcel()
{
string path= "./Assets/ImportTemplate";
string sFileName = $"MaterialMODEL.xlsx";
string fullPath = Path.Combine(path, sFileName);
(string, string) result = (sFileName,fullPath);
return ExportExcel(result.Item2, result.Item1);
}
//TODO 导入excel
[HttpPost("importData")]
[Log(Title = "物料类型导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
[AllowAnonymous]
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
{
if (formFile == null)
{
return SUCCESS(null);
}
int response = _MaterialTypeService.ImportData(formFile, HttpContext.GetName());
return SUCCESS(response);
}
//TODO 导出excel
[HttpGet("exportData")]
[Log(Title = "物料类型导出", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ExportData()
{
var excelBytes = _MaterialTypeService.ExportData();
string fileName = $"PlanAchievementRate_{DateTime.Now.Date:yyyyMMdd}.xlsx";
return File(
excelBytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
fileName
);
}
}

View File

@ -10,6 +10,14 @@
<None Remove="Services\Process\IService\IProcessControlStrategyDictService.cs~RFd534036.TMP" />
</ItemGroup>
<ItemGroup>
<Resource Include="Assets\ImportTemplate\MaterialMODEL.xlsx">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
</ItemGroup>

View File

@ -2,11 +2,14 @@ using System.ComponentModel.DataAnnotations;
namespace MDM.Model.Material.Dto
{
/// <summary>
/// 查询对象
/// </summary>
public class MaterialBomQueryDto : PagerInfo
{
public string InvCode { get; set; }
public string SubInvCode { get; set; }
}
/// <summary>

View File

@ -3,6 +3,7 @@ using MDM.Model;
using MDM.Model.Material;
using MDM.Model.Material.Dto;
using MDM.Service;
using Microsoft.AspNetCore.Http;
namespace MDM.Services.IMaterialService
{
@ -13,11 +14,22 @@ namespace MDM.Services.IMaterialService
{
PagedInfo<MaterialBomDto> GetList(MaterialBomQueryDto parm);
PagedInfo<MaterialBomDto> GetMonterInvList(MaterialBomQueryDto parm);
List<MaterialBom> GetSonInvList(MaterialBomQueryDto parm);
MaterialBom GetInfo(string Id);
MaterialBom AddMaterialBom(MaterialBom parm);
int UpdateMaterialBom(MaterialBom parm);
int ImportData(IFormFile formFile, string name);
byte[] ExportData();
}
}

View File

@ -2,6 +2,7 @@ using MDM.Model;
using MDM.Model.Material;
using MDM.Model.Material.Dto;
using MDM.Service;
using Microsoft.AspNetCore.Http;
namespace MDM.Services.IMaterialService
{
@ -17,6 +18,10 @@ namespace MDM.Services.IMaterialService
MaterialList AddMaterialList(MaterialList parm);
int UpdateMaterialList(MaterialList parm);
int ImportData(IFormFile formFile, string name);
byte[] ExportData();
}
}

View File

@ -2,6 +2,7 @@ using MDM.Model;
using MDM.Model.Material;
using MDM.Model.Material.Dto;
using MDM.Service;
using Microsoft.AspNetCore.Http;
namespace MDM.Services.IMaterialService
{
@ -18,5 +19,10 @@ namespace MDM.Services.IMaterialService
int UpdateMaterialType(MaterialType parm);
int ImportData(IFormFile formFile, string name);
byte[] ExportData();
}
}

View File

@ -1,5 +1,7 @@
using Infrastructure;
using Infrastructure.Attribute;
using MDM.Model;
using MDM.Model.Material;
@ -7,6 +9,9 @@ using MDM.Model.Material.Dto;
using MDM.Repository;
using MDM.Service;
using MDM.Services.IMaterialService;
using Microsoft.AspNetCore.Http;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Linq.Expressions;
@ -25,7 +30,13 @@ namespace MES_Model.Services.Material
/// <returns></returns>
public PagedInfo<MaterialBomDto> GetList(MaterialBomQueryDto parm)
{
var predicate = Expressionable.Create<MaterialBom>();
var predicate = Expressionable.Create<MaterialBom>()
.AndIF(!string.IsNullOrEmpty(parm.InvCode), it => it.InvCode.Contains(parm.InvCode))
.AndIF(!string.IsNullOrEmpty(parm.SubInvCode), it => it.SubInvCode.Contains(parm.SubInvCode))
;
;
var response = Queryable()
.Where(predicate.ToExpression())
@ -33,7 +44,55 @@ namespace MES_Model.Services.Material
return response;
}
/// <summary>
/// 获取母件BOM清单
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<MaterialBomDto> GetMonterInvList(MaterialBomQueryDto parm)
{
var predicate = Expressionable.Create<MaterialBom>()
.AndIF(!string.IsNullOrEmpty(parm.InvCode), it => it.InvCode.Contains(parm.InvCode))
.AndIF(!string.IsNullOrEmpty(parm.SubInvCode), it => it.SubInvCode.Contains(parm.SubInvCode))
;
var query = Context.Queryable<MaterialBom>()
.Where(predicate.ToExpression())
.GroupBy(it => it.InvCode)
.Select(it => new MaterialBomDto()
{
Id = SqlFunc.AggregateMax(it.Id),
InvCode = SqlFunc.AggregateMax(it.InvCode),
InvName = SqlFunc.AggregateMax(it.InvName),
SubInvName = SqlFunc.AggregateMax(it.SubInvName),
Iusequantity = SqlFunc.AggregateMax(it.Iusequantity),
BOMVersion = SqlFunc.AggregateMax(it.BOMVersion),
CreatedBy = SqlFunc.AggregateMax(it.CreatedBy),
CreatedTime = SqlFunc.AggregateMax(it.CreatedTime),
UpdatedBy = SqlFunc.AggregateMax(it.UpdatedBy),
UpdatedTime = SqlFunc.AggregateMax(it.UpdatedTime)
});
var response = query.ToPage<MaterialBomDto, MaterialBomDto>(parm);
return response;
}
/// <summary>
/// 获取子件
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public List<MaterialBom> GetSonInvList(MaterialBomQueryDto parm)
{
return Context.Queryable<MaterialBom>().Where(it => it.InvCode == parm.InvCode).ToList();
}
/// <summary>
/// 获取详情
@ -83,6 +142,127 @@ namespace MES_Model.Services.Material
return Update(model, true);
}
public int ImportData(IFormFile formFile, string name)
{
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheet("物料BOM清单");
List<MaterialBom> materialBoms = new List<MaterialBom>();
// 遍历每一行
for (int row = 1; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空
{
MaterialBom item = new MaterialBom();
item.Id = XueHua;
//母件编码
if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String)
item.InvCode = currentRow.GetCell(0).ToString();
//母件名称
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.InvName = currentRow.GetCell(1).ToString();
//母件名称
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.InvName = currentRow.GetCell(1).ToString();
//子件编码
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.SubInvCode = currentRow.GetCell(1).ToString();
//子件名称
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.SubInvName = currentRow.GetCell(1).ToString();
//使用数量
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.Iusequantity = currentRow.GetCell(1).ToString();
//BOM版本号
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.BOMVersion = currentRow.GetCell(1).ToString();
materialBoms.Add(item);
}
}
if (materialBoms.Count == 0)
{
return 0;
}
UseTran2(() =>
{
// Context.Deleteable<ProPlanAchievementrateVersion2>().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand();
Context.Insertable(materialBoms).ExecuteCommand();
});
return materialBoms.Count;
}
catch (Exception ex)
{
throw new CustomException("excel文件异常请详细核对");
}
}
}
public byte[] ExportData()
{
string templatePath = Path.Combine(
Directory.GetCurrentDirectory(),
"wwwroot",
"ImportTemplate",
"MaterialMODEL.xlsx"
);
if (!global::System.IO.File.Exists(templatePath))
{
throw new CustomException("Excel 模板文件不存在");
}
List<MaterialType> dataList = Context.Queryable<MaterialType>()
.Where(it => it.Status == 1)
.ToList();
using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = WorkbookFactory.Create(fileStream);
ISheet sheet = workbook.GetSheet("物料类型");
int startRowIndex = 1;
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i);
// 为每个需要数字的单元格设置值,并应用 numberCellStyle
row.CreateCell(0).SetCellValue(item.Name);
// 以下为字符串类型字段,不需要数字格式
row.CreateCell(1).SetCellValue(item.Code);
row.CreateCell(2).SetCellValue(item.ParentCode);
row.CreateCell(3).SetCellValue((double)item.Status);
}
// 改为写入 MemoryStream 然后转成 byte[]
var memoryStream = new MemoryStream();
workbook.Write(memoryStream);
// memoryStream.Position = 0;
return memoryStream.ToArray(); // ✅ 直接返回 byte 数组
}
}
}
}

View File

@ -1,3 +1,4 @@
using Infrastructure;
using Infrastructure.Attribute;
using MDM.Model;
using MDM.Model.Material;
@ -5,6 +6,9 @@ using MDM.Model.Material.Dto;
using MDM.Repository;
using MDM.Service;
using MDM.Services.IMaterialService;
using Microsoft.AspNetCore.Http;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
@ -92,5 +96,191 @@ namespace MDM.Services.Material
return Update(model, true);
}
public int ImportData(IFormFile formFile, string name)
{
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheet("物料列表");
List<MaterialList> materialLists = new List<MaterialList>();
// 遍历每一行
for (int row = 1; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空
{
MaterialList item = new MaterialList();
item.Id = XueHua;
//物料类型code
if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String)
item.FkMaterialTypeCode = currentRow.GetCell(0).ToString();
//物料名称
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.Name = currentRow.GetCell(1).ToString();
//物料编码
if (currentRow.GetCell(2) != null && currentRow.GetCell(2).CellType == CellType.String)
item.Code = currentRow.GetCell(2).ToString();
//物料代码
if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.String)
item.Addcode = currentRow.GetCell(3).ToString();
//客户编码
if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.String)
item.CustomerCode = currentRow.GetCell(4).ToString();
//颜色
if (currentRow.GetCell(5) != null && currentRow.GetCell(5).CellType == CellType.String)
item.Color = currentRow.GetCell(5).ToString();
//规格型号
if (currentRow.GetCell(6) != null && currentRow.GetCell(6).CellType == CellType.String)
item.Specification = currentRow.GetCell(6).ToString();
//单位
if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.String)
item.Unit = currentRow.GetCell(7).ToString();
//描述
if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.String)
item.Description = currentRow.GetCell(7).ToString();
//供应商代码
if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.String)
item.SupplierCode = currentRow.GetCell(7).ToString();
//保质期单位
if (currentRow.GetCell(8) != null && currentRow.GetCell(8).CellType == CellType.String)
item.ExpirationUnit = currentRow.GetCell(8).ToString();
//保质期单位
if (currentRow.GetCell(8) != null && currentRow.GetCell(8).CellType == CellType.String)
item.ExpirationUnit = currentRow.GetCell(8).ToString();
//保质期(天)
if (currentRow.GetCell(9) != null && currentRow.GetCell(9).CellType == CellType.Numeric)
item.ExpirationDate =Convert.ToInt16(currentRow.GetCell(9).ToString());
//保质期预警天数
if (currentRow.GetCell(10) != null && currentRow.GetCell(10).CellType == CellType.Numeric)
item.ShelfLifeWarningDays = Convert.ToInt16(currentRow.GetCell(10).ToString());
//是否有效期管理
if (currentRow.GetCell(11) != null && currentRow.GetCell(11).CellType == CellType.Numeric)
item.IsShelfLife = Convert.ToInt16(currentRow.GetCell(11).ToString());
//启用日期
//if (currentRow.GetCell(12) != null && currentRow.GetCell(12).CellType == CellType.Formula)
// item.StartTime = Convert.ToInt16(currentRow.GetCell(12).ToString());
//对应条码
if (currentRow.GetCell(12) != null && currentRow.GetCell(12).CellType == CellType.String)
item.BarCode = currentRow.GetCell(12).ToString();
materialLists.Add(item);
}
}
if (materialLists.Count == 0)
{
return 0;
}
UseTran2(() =>
{
// Context.Deleteable<ProPlanAchievementrateVersion2>().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand();
Context.Insertable(materialLists).ExecuteCommand();
});
return materialLists.Count;
}
catch (Exception ex)
{
throw new CustomException("excel文件异常请详细核对");
}
}
}
public byte[] ExportData()
{
string templatePath = Path.Combine(
Directory.GetCurrentDirectory(),
"Assets",
"ImportTemplate",
"MaterialMODEL.xlsx"
);
if (!global::System.IO.File.Exists(templatePath))
{
throw new CustomException("Excel 模板文件不存在");
}
List<MaterialList> dataList = Context.Queryable<MaterialList>()
.ToList();
using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = WorkbookFactory.Create(fileStream);
ISheet sheet = workbook.GetSheet("物料台账");
int startRowIndex = 1;
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i);
row.CreateCell(0).SetCellValue(item.FkMaterialTypeCode);
row.CreateCell(1).SetCellValue(item.Name);
row.CreateCell(2).SetCellValue(item.Code);
row.CreateCell(3).SetCellValue(item.CustomerCode);
row.CreateCell(4).SetCellValue(item.Color);
row.CreateCell(5).SetCellValue(item.Specification);
row.CreateCell(6).SetCellValue(item.Unit);
row.CreateCell(7).SetCellValue(item.Description);
row.CreateCell(8).SetCellValue(item.SupplierCode);
row.CreateCell(9).SetCellValue(item.ExpirationUnit);
row.CreateCell(10).SetCellValue((double)item.ExpirationDate);
row.CreateCell(11).SetCellValue((double)item.ShelfLifeWarningDays);
row.CreateCell(12).SetCellValue((double)item.IsShelfLife);
row.CreateCell(13).SetCellValue(item.BarCode);
}
// 改为写入 MemoryStream 然后转成 byte[]
var memoryStream = new MemoryStream();
workbook.Write(memoryStream);
// memoryStream.Position = 0;
return memoryStream.ToArray(); // ✅ 直接返回 byte 数组
}
}
}
}

View File

@ -7,6 +7,13 @@ using MDM.Model.Material.Dto;
using MDM.Service;
using MDM.Model;
using MDM.Repository;
using DOAN.Model.MES.product;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace MDM.Services.Material
{
/// <summary>
@ -86,6 +93,131 @@ namespace MDM.Services.Material
//return response;
return Update(model, true);
}
public int ImportData(IFormFile formFile, string name)
{
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheet("物料类型");
List<MaterialType> materialTypes = new List<MaterialType>();
// 遍历每一行
for (int row = 1; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空
{
MaterialType item = new MaterialType();
//物料类型名称
if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String)
item.Name = currentRow.GetCell(0).ToString();
//物料类别编码
if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String)
item.Code = currentRow.GetCell(1).ToString();
//物料类别父级编码
if (currentRow.GetCell(2) != null && currentRow.GetCell(2).CellType == CellType.String)
item.ParentCode = currentRow.GetCell(2).ToString();
//状态 Status
if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.String)
{
string statusstring = currentRow.GetCell(3).ToString();
if(statusstring == "启用")
{
item.Status = 1;
}
else if (statusstring == "停用")
{
item.Status = 0;
}
}
//备注
if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.String)
item.Remark = currentRow.GetCell(4).ToString();
materialTypes.Add(item);
}
}
if (materialTypes.Count == 0)
{
return 0;
}
UseTran2(() =>
{
// Context.Deleteable<ProPlanAchievementrateVersion2>().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand();
Context.Insertable(materialTypes).ExecuteCommand();
});
return materialTypes.Count;
}
catch (Exception ex)
{
throw new CustomException("excel文件异常请详细核对");
}
}
}
public byte[] ExportData()
{
string templatePath = Path.Combine(
Directory.GetCurrentDirectory(),
"wwwroot",
"ImportTemplate",
"MaterialMODEL.xlsx"
);
if (!global::System.IO.File.Exists(templatePath))
{
throw new CustomException("Excel 模板文件不存在");
}
List<MaterialType> dataList = Context.Queryable<MaterialType>()
.Where(it=>it.Status==1)
.ToList();
using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = WorkbookFactory.Create(fileStream);
ISheet sheet = workbook.GetSheet("物料类型");
int startRowIndex = 1;
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i);
// 为每个需要数字的单元格设置值,并应用 numberCellStyle
row.CreateCell(0).SetCellValue(item.Name);
// 以下为字符串类型字段,不需要数字格式
row.CreateCell(1).SetCellValue(item.Code);
row.CreateCell(2).SetCellValue(item.ParentCode);
row.CreateCell(3).SetCellValue((double)item.Status);
}
// 改为写入 MemoryStream 然后转成 byte[]
var memoryStream = new MemoryStream();
workbook.Write(memoryStream);
// memoryStream.Position = 0;
return memoryStream.ToArray(); // ✅ 直接返回 byte 数组
}
}
}
}