using Microsoft.AspNetCore.Mvc; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using ZR.Model.MES.qc; using ZR.Model.MES.qc.DTO; using ZR.Service.mes.qc.IService; namespace ZR.Admin.WebApi.Controllers.mes.qc.FQC { /// /// 生成报表 /// [Route("mes/qc/statistics")] public class QCStatisticsController : BaseController { private readonly IQCStatisticsService qcStatistics; public QCStatisticsController(IQCStatisticsService qcStatistics) { this.qcStatistics = qcStatistics; } /// /// 获取 统计表 首检 /// /// [HttpGet("queryQualityStatisticsTable_first")] public IActionResult GetQualityStatisticsTable_first(DateTime starttime, DateTime endTime, string workorderid, string partnumber, string product_description, string team, int pageNum, int pageSize, int sortType) { (List, int) list = qcStatistics.GetQualityStatisticsTable_first(starttime, endTime, workorderid, partnumber, product_description, team, pageNum, pageSize, sortType); return SUCCESS(list); } /// /// 获取 统计表 二检 /// /// [HttpGet("queryQualityStatisticsTable_again")] public IActionResult GetQualityStatisticsTable_again(DateTime starttime, DateTime endTime, string workorderid, string partnumber, string product_description, string team, int pageNum, int pageSize, int sortType) { (List, int) list = qcStatistics.GetQualityStatisticsTable_again(starttime, endTime, workorderid, partnumber, product_description, team, pageNum, pageSize, sortType); return SUCCESS(list); } /// /// 获取 统计表 三检 /// /// [HttpGet("queryQualityStatisticsTable_final")] public IActionResult GetQualityStatisticsTable_final(DateTime starttime, DateTime endTime, string workorderid, string partnumber, string product_description, string team, int pageNum, int pageSize, int sortType) { (List, int) list = qcStatistics.GetQualityStatisticsTable_final(starttime, endTime, workorderid, partnumber, product_description, team, pageNum, pageSize, sortType); return SUCCESS(list); } /// /// 获取 统计表 总 /// /// [HttpGet("queryQualityStatisticsTable_total")] public IActionResult GetQualityStatisticsTable_total(DateTime starttime, DateTime endTime, string workorderid, string partnumber, string product_description, string team, int pageNum, int pageSize, int sortType) { (List, int) list = qcStatistics.GetQualityStatisticsTable_total(starttime, endTime, workorderid, partnumber, product_description, team, pageNum, pageSize, sortType); return SUCCESS(list); } [HttpGet("delete_statisticsTable")] public IActionResult DeleteStatisticsTable(string workorderid) { int deletenum = qcStatistics.DeleteStatisticsTable(workorderid); return SUCCESS(deletenum); } /// /// 获取 首检 倒车雷达数量 /// /// [HttpGet("getParkingSensorTotal")] public IActionResult GetParkingSensorTotal(DateTime starttime, DateTime endTime, string workorderid, string partnumber, string product_description, string team, int pageNum, int pageSize) { int result = qcStatistics.GetParkingSensorTotal(starttime, endTime, workorderid, partnumber, product_description, team, pageNum, pageSize); return SUCCESS(result); } /// /// 导出对应报表 /// /// 开始时间 /// 结束时间 /// 工单号 /// 零件号 /// 产品描述 /// 班组 /// 分页开始 /// 分页结束 /// 报表类别 1-首检 2-抛光 3-包装 4-全部 /// 是否展示细节,展示细节则根据三行,两行合并规则 /// [HttpGet("downloadStatisticsTableExcel")] public IActionResult DownloadStatisticsTableExcel(DateTime starttime, DateTime endTime, string workorderid, string partnumber, string product_description, string team, int pageNum, int pageSize, int type, bool isShowDetail, int sortType) { try { string fileName = @"导出统计报表-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + Guid.NewGuid() + ".xlsx"; List excelDataList = qcStatistics.DownloadStatisticsTableExcel(starttime, endTime, workorderid, partnumber, product_description, team, pageNum, pageSize, type, sortType); XSSFWorkbook workbook = new XSSFWorkbook(); var sheet = workbook.CreateSheet(); // 单元格样式 ICellStyle style = workbook.CreateCellStyle(); style.Alignment = HorizontalAlignment.Center; style.VerticalAlignment = VerticalAlignment.Center; // 标题列表 string[] titleDict = { "工单号", "零件号", "颜色", "描述", "生产投入数", "班次", "合格数", "合格率", "抛光总数", "打磨总数", "报废总数", "开始时间", "结束时间" }; string[] titleGroupDict = { "油漆", "设备", "毛坯", "程序", "班组操作" }; int[] titleGroupIndex = { 14, 20, 27, 33, 38 }; string[] titleDetailDict = { "备注", "缩孔", "针孔", "失光","色差","点子","其他", "水斑", "脏点", "变形","油珠","设备故障","撞伤","其他", "毛刺", "缩印", "擦伤","砂印","流痕","开裂", "流挂", "色漆 缺漆", "清漆缺漆","桔皮","其他", "下件擦伤", "清漆漆块", "色漆漆块","发花","脱落","喷漏",}; // 标题列位于第几行 int startTitle = 0; if (isShowDetail) { // 调整第一行分组 startTitle = 1; var groupTitle = sheet.CreateRow(0); var titleCell1 = groupTitle.CreateCell(0); titleCell1.SetCellValue("工单信息"); titleCell1.CellStyle = style; for (int i = 0; i < titleGroupDict.Length; i++) { var cell = groupTitle.CreateCell(titleGroupIndex[i]); cell.SetCellValue(titleGroupDict[i]); cell.CellStyle = style; } // 标题列表添加详情 titleDict = titleDict.Concat(titleDetailDict).ToArray(); // 合并标题列 CellRangeAddress titleRange1 = new(0, 0, 0, titleGroupIndex[0] - 1); sheet.AddMergedRegion(titleRange1); CellRangeAddress titleRange2 = new(0, 0, titleGroupIndex[0], titleGroupIndex[1] - 1); sheet.AddMergedRegion(titleRange2); CellRangeAddress titleRange3 = new(0, 0, titleGroupIndex[1], titleGroupIndex[2] - 1); sheet.AddMergedRegion(titleRange3); CellRangeAddress titleRange4 = new(0, 0, titleGroupIndex[2], titleGroupIndex[3] - 1); sheet.AddMergedRegion(titleRange4); CellRangeAddress titleRange5 = new(0, 0, titleGroupIndex[3], titleGroupIndex[4] - 1); sheet.AddMergedRegion(titleRange5); CellRangeAddress titleRange6 = new(0, 0, titleGroupIndex[4], titleGroupIndex[4] + 5); sheet.AddMergedRegion(titleRange6); } var rowTitle = sheet.CreateRow(startTitle); for (int i = 0; i < titleDict.Length; i++) { var cell = rowTitle.CreateCell(i); cell.SetCellValue(titleDict[i]); cell.CellStyle = style; } string _lastWorkOrderId = ""; // 数据开始行 int rowIndex = startTitle + 1; for (int i = 0; i < excelDataList.Count; i++) { var item = excelDataList[i]; if (!isShowDetail && _lastWorkOrderId == item.WorkorderId) { continue; } var row = sheet.CreateRow(rowIndex); var cell1 = row.CreateCell(0); cell1.SetCellValue(item.WorkorderId); cell1.CellStyle = style; var cell2 = row.CreateCell(1); cell2.SetCellValue(item.FinishedPartNumber); cell2.CellStyle = style; var cell3 = row.CreateCell(2); cell3.SetCellValue(item.Color); cell3.CellStyle = style; // 描述 var cell4 = row.CreateCell(3); cell4.SetCellValue(item.ProductDescription.ToString()); cell4.CellStyle = style; // ===== var cell5 = row.CreateCell(4); cell5.SetCellValue(item.RequireNumber.ToString()); cell5.CellStyle = style; var cell6 = row.CreateCell(5); cell6.SetCellValue(item.Team); cell6.CellStyle = style; var cell7 = row.CreateCell(6); cell7.SetCellValue(item.QualifiedNumber.ToString()); cell7.CellStyle = style; var cell8 = row.CreateCell(7); cell8.SetCellValue(((int)item.QualifiedRate).ToString() + "%"); cell8.CellStyle = style; var cell9 = row.CreateCell(8); cell9.SetCellValue(item.PaoguangTotal.ToString()); cell9.CellStyle = style; var cell10 = row.CreateCell(9); cell10.SetCellValue(item.DamoTotal.ToString()); cell10.CellStyle = style; var cell11 = row.CreateCell(10); cell11.SetCellValue(item.BaofeiTotal.ToString()); cell11.CellStyle = style; var cell12 = row.CreateCell(11); cell12.SetCellValue(item.StartTime.ToString()); cell12.CellStyle = style; var cell13 = row.CreateCell(12); cell13.SetCellValue(item.EndTime.ToString()); cell13.CellStyle = style; if (isShowDetail) { // 备注 var cell14 = row.CreateCell(13); cell14.SetCellValue(item.Remark.ToString()); cell14.CellStyle = style; // 油漆 var cell15 = row.CreateCell(14); cell15.SetCellValue(item.PaintSuokong.ToString() == "0" ? "" : item.PaintSuokong.ToString()); cell15.CellStyle = style; var cell16 = row.CreateCell(15); cell16.SetCellValue(item.PaintZhengkong.ToString() == "0" ? "" : item.PaintZhengkong.ToString()); cell16.CellStyle = style; var cell17 = row.CreateCell(16); cell17.SetCellValue(item.PaintShiguang.ToString() == "0" ? "" : item.PaintShiguang.ToString()); cell17.CellStyle = style; var cell18 = row.CreateCell(17); cell18.SetCellValue(item.PaintSecha.ToString() == "0" ? "" : item.PaintSecha.ToString()); cell18.CellStyle = style; var cell19 = row.CreateCell(18); cell19.SetCellValue(item.PaintDianzi.ToString() == "0" ? "" : item.PaintDianzi.ToString()); cell19.CellStyle = style; var cell20 = row.CreateCell(19); cell20.SetCellValue(item.PaintOther.ToString() == "0" ? "" : item.PaintOther.ToString()); cell20.CellStyle = style; // 设备 var cell21 = row.CreateCell(20); cell21.SetCellValue(item.DeviceShuiban.ToString() == "0" ? "" : item.DeviceShuiban.ToString()); cell21.CellStyle = style; var cell22 = row.CreateCell(21); cell22.SetCellValue(item.DeviceZandian.ToString() == "0" ? "" : item.DeviceZandian.ToString()); cell22.CellStyle = style; var cell23 = row.CreateCell(22); cell23.SetCellValue(item.DeviceBianxing.ToString() == "0" ? "" : item.DeviceBianxing.ToString()); cell23.CellStyle = style; var cell24 = row.CreateCell(23); cell24.SetCellValue(item.DeviceYouzhu.ToString() == "0" ? "" : item.DeviceYouzhu.ToString()); cell24.CellStyle = style; var cell25 = row.CreateCell(24); cell25.SetCellValue(item.DeviceTuoluo.ToString() == "0" ? "" : item.DeviceTuoluo.ToString()); cell25.CellStyle = style; var cell26 = row.CreateCell(25); cell26.SetCellValue(item.DeviceZhuangshang.ToString() == "0" ? "" : item.DeviceZhuangshang.ToString()); cell26.CellStyle = style; var cell27 = row.CreateCell(26); cell27.SetCellValue(item.DeviceOther.ToString() == "0" ? "" : item.DeviceOther.ToString()); cell27.CellStyle = style; // 毛坯 var cell28 = row.CreateCell(27); cell28.SetCellValue(item.BlankMaoci.ToString() == "0" ? "" : item.BlankMaoci.ToString()); cell28.CellStyle = style; var cell29 = row.CreateCell(28); cell29.SetCellValue(item.BlankSuoyin.ToString() == "0" ? "" : item.BlankSuoyin.ToString()); cell29.CellStyle = style; var cell30 = row.CreateCell(29); cell30.SetCellValue(item.BlankCanshuang.ToString() == "0" ? "" : item.BlankCanshuang.ToString()); cell30.CellStyle = style; var cell31 = row.CreateCell(30); cell31.SetCellValue(item.BlankShaying.ToString() == "0" ? "" : item.BlankShaying.ToString()); cell31.CellStyle = style; var cell32 = row.CreateCell(31); cell32.SetCellValue(item.BlankZangdian.ToString() == "0" ? "" : item.BlankZangdian.ToString()); cell32.CellStyle = style; var cell33 = row.CreateCell(32); cell33.SetCellValue(item.BlankDamo.ToString() == "0" ? "" : item.BlankDamo.ToString()); cell33.CellStyle = style; // 程序 var cell34 = row.CreateCell(33); cell34.SetCellValue(item.ProgramLiuguang.ToString() == "0" ? "" : item.ProgramLiuguang.ToString()); cell34.CellStyle = style; var cell35 = row.CreateCell(34); cell35.SetCellValue(item.ProgramSeqiqueqi.ToString() == "0" ? "" : item.ProgramSeqiqueqi.ToString()); cell35.CellStyle = style; var cell36 = row.CreateCell(35); cell36.SetCellValue(item.ProgramQingqiqueqi.ToString() == "0" ? "" : item.ProgramQingqiqueqi.ToString()); cell36.CellStyle = style; var cell37 = row.CreateCell(36); cell37.SetCellValue(item.ProgramJupi.ToString() == "0" ? "" : item.ProgramJupi.ToString()); cell37.CellStyle = style; var cell38 = row.CreateCell(37); cell38.SetCellValue(item.ProgramOther.ToString() == "0" ? "" : item.ProgramOther.ToString()); cell38.CellStyle = style; // 班组操作 var cell39 = row.CreateCell(38); cell39.SetCellValue(item.TeamTuoluocanshuang.ToString() == "0" ? "" : item.TeamTuoluocanshuang.ToString()); cell39.CellStyle = style; var cell40 = row.CreateCell(39); cell40.SetCellValue(item.TeamQingqiqikuai.ToString() == "0" ? "" : item.TeamQingqiqikuai.ToString()); cell40.CellStyle = style; var cell41 = row.CreateCell(40); cell41.SetCellValue(item.TeamSeqiqikuai.ToString() == "0" ? "" : item.TeamSeqiqikuai.ToString()); cell41.CellStyle = style; var cell42 = row.CreateCell(41); cell42.SetCellValue(item.TeamFahua.ToString() == "0" ? "" : item.TeamFahua.ToString()); cell42.CellStyle = style; var cell43 = row.CreateCell(42); cell43.SetCellValue(item.TeamLiangbang.ToString() == "0" ? "" : item.TeamLiangbang.ToString()); cell43.CellStyle = style; var cell44 = row.CreateCell(43); cell44.SetCellValue(item.TeamPenglou.ToString() == "0" ? "" : item.TeamPenglou.ToString()); cell44.CellStyle = style; // 单元格合并 // 合并行数 int checkIndex = rowIndex + 1; if (type == 3) { for (int rangeCol = 0; rangeCol < 13; rangeCol++) { if (rowIndex % 2 == 0) { CellRangeAddress cellRangeAddress = new(rowIndex, rowIndex + 1, rangeCol, rangeCol); sheet.AddMergedRegion(cellRangeAddress); } } } else { for (int rangeCol = 0; rangeCol < 13; rangeCol++) { if (checkIndex % 3 == 0) { CellRangeAddress cellRangeAddress = new(rowIndex, rowIndex + 2, rangeCol, rangeCol); sheet.AddMergedRegion(cellRangeAddress); } } } } _lastWorkOrderId = item.WorkorderId; rowIndex++; } // 导出 IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment)); string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "QualityStatisticsExport", fileName); using (FileStream fs = new(fullPath, FileMode.Create, FileAccess.Write)) { workbook.Write(fs); } workbook.Dispose(); return ExportExcel(fullPath, fileName); } catch (Exception e) { return ToResponse(new ApiResult(500, e.Message, "Excel导出异常")); } } } }