This commit is contained in:
gcw_MV9p2JJN 2025-10-23 20:12:40 +08:00
parent f99b207efc
commit 3173ea3cb7
3 changed files with 100 additions and 11 deletions

View File

@ -125,7 +125,7 @@ namespace DOAN.Admin.WebApi.Controllers
//TODO 导出模板
[HttpGet("importTemplate")]
[Log(Title = "生产工单导入模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[Log(Title = "计划达成率模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ImportTemplateExcel()
{
@ -150,12 +150,22 @@ namespace DOAN.Admin.WebApi.Controllers
}
//TODO 导出excel 某天
[HttpGet("exportData")]
[Log(Title = "计划达成率导出", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
[AllowAnonymous]
public IActionResult ExportData([FromQuery] DateTime exportDate)
{
var memoryStream = _ProPlanAchievementrateService.ExportData(exportDate);
string fileName = $"PlanAchievementRate_{exportDate:yyyyMMdd}.xlsx";
return File(
memoryStream,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
fileName
);
}
}
}

View File

@ -20,7 +20,7 @@ namespace DOAN.Service.MES.product.IService
PagedInfo<ProPlanAchievementrateDto> GetListByMonth(ProPlanAchievementrateQueryDto2 parm);
ProPlanAchievementrate GetInfo(int Id);
@ -39,5 +39,8 @@ namespace DOAN.Service.MES.product.IService
bool ImportData(IFormFile formFile, string name, DateTime RecordDate);
MemoryStream ExportData(DateTime exportDate);
}
}

View File

@ -19,6 +19,7 @@ using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Globalization;
using NPOI.HSSF.Record;
using JinianNet.JNTemplate;
namespace DOAN.Service.MES.product
{
/// <summary>
@ -130,7 +131,7 @@ namespace DOAN.Service.MES.product
UseTran2(() =>
{
Context.Deleteable<ProPlanAchievementrate>().Where(it => it.RecordDate == parm.GenarateDate);
result= Context.Insertable(finalList).ExecuteCommand();
result = Context.Insertable(finalList).ExecuteCommand();
});
@ -338,7 +339,7 @@ namespace DOAN.Service.MES.product
}
public bool ImportData(IFormFile formFile, string name,DateTime RecordDate)
public bool ImportData(IFormFile formFile, string name, DateTime RecordDate)
{
using (var stream = formFile.OpenReadStream())
{
@ -346,19 +347,19 @@ namespace DOAN.Service.MES.product
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
List<ProPlanAchievementrate> AchievementrateList = new List<ProPlanAchievementrate>();
// 遍历每一行
for (int row = 2; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null&&currentRow.GetCell(0)!=null) // 确保行不为空
if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空
{
ProPlanAchievementrate achievementrate = new ProPlanAchievementrate();
//获取项目号
if(currentRow.GetCell(0) != null&&currentRow.GetCell(0).CellType== CellType.String)
if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String)
achievementrate.Project = currentRow.GetCell(0).ToString();
//A班班长
@ -449,7 +450,7 @@ namespace DOAN.Service.MES.product
}
}
if(AchievementrateList.Count == 0)
if (AchievementrateList.Count == 0)
{
return false;
}
@ -458,6 +459,7 @@ namespace DOAN.Service.MES.product
Context.Deleteable<ProPlanAchievementrate>().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand();
Context.Insertable(AchievementrateList).ExecuteCommand();
});
return true;
}
catch (Exception ex)
@ -473,5 +475,79 @@ namespace DOAN.Service.MES.product
}
public MemoryStream ExportData(DateTime exportDate)
{
string templatePath = Path.Combine(
Directory.GetCurrentDirectory(),
"wwwroot",
"ImportTemplate",
"PlanAchievementRate.xlsx"
);
if (!global::System.IO.File.Exists(templatePath))
{
throw new CustomException("Excel 模板文件不存在"); // 或者你自定义的异常
}
// 查询数据
List<ProPlanAchievementrate> dataList = Context.Queryable<ProPlanAchievementrate>()
.Where(it => it.RecordDate.HasValue && it.RecordDate.Value.Date == exportDate.Date)
.ToList();
// 打开模板
using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = WorkbookFactory.Create(fileStream);
ISheet sheet = workbook.GetSheetAt(0);
int startRowIndex = 2; // 从第3行开始写数据
for (int i = 0; i < dataList.Count; i++)
{
var item = dataList[i];
IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i);
// 填充每一列,根据你原来的逻辑
row.CreateCell(1).SetCellValue(item.Project);
row.CreateCell(2).SetCellValue(item.AgroupMonitor);
row.CreateCell(3).SetCellValue((double)(item.AgroupProductNum ?? 0));
row.CreateCell(4).SetCellValue((double)(item.AgroupStandardYield ?? 0));
row.CreateCell(5).SetCellValue((double)(item.AgroupPlanNum ?? 0));
row.CreateCell(6).SetCellValue((double)item.AgroupCompletionRate);
row.CreateCell(7).SetCellValue(item.BgroupMonitor);
row.CreateCell(8).SetCellValue((double)(item.BgroupProductNum ?? 0));
row.CreateCell(9).SetCellValue((double)(item.BgroupStandardYield ?? 0));
row.CreateCell(10).SetCellValue((double)(item.BgroupPlanNum ?? 0));
row.CreateCell(11).SetCellValue((double)item.BgroupCompletionRate);
row.CreateCell(12).SetCellValue((double)(item.SummaryActualNum ?? 0));
row.CreateCell(13).SetCellValue((double)(item.SummaryPlanNum ?? 0));
row.CreateCell(14).SetCellValue((double)item.SummaryPlanAchievementRate);
row.CreateCell(15).SetCellValue((double)(item.DownQuality ?? 0));
row.CreateCell(16).SetCellValue((double)(item.DownSuppler ?? 0));
row.CreateCell(17).SetCellValue((double)(item.DownDeviceFailure ?? 0));
row.CreateCell(18).SetCellValue((double)(item.DownDeviceDebug ?? 0));
row.CreateCell(19).SetCellValue((double)(item.DownLogisticsWaitMaterial ?? 0));
row.CreateCell(20).SetCellValue((double)(item.DownLackMaterial ?? 0));
row.CreateCell(21).SetCellValue((double)(item.DownInjection ?? 0));
row.CreateCell(22).SetCellValue((double)(item.DownAssembly ?? 0));
row.CreateCell(23).SetCellValue((double)(item.AllLineStopTime ?? 0));
row.CreateCell(24).SetCellValue(item.MainProblemDescription);
row.CreateCell(25).SetCellValue(item.Strategy);
row.CreateCell(26).SetCellValue(item.ResponsiblePerson);
}
// 保存到新的 MemoryStream
var memoryStream = new MemoryStream();
workbook.Write(memoryStream);
memoryStream.Position = 0; // 重置流位置
return memoryStream;
}
}
}
}