using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using Competition.Common.Util; using CompetitionAPI.Util; namespace CompetitionAPI.Controllers.back { [Route("api/[controller]")] [ApiController] public class DownloadExamController : Controller { private readonly IWebHostEnvironment _webHostEnvironment; Competition.Mysql.BLL.pow_exam exam_bll = new Competition.Mysql.BLL.pow_exam(); Competition.Mysql.BLL.pow_exam_fault exam_fault_bll = new Competition.Mysql.BLL.pow_exam_fault(); Competition.Mysql.BLL.pow_exam_tool exam_tool_bll = new Competition.Mysql.BLL.pow_exam_tool(); Competition.Mysql.BLL.pow_exam_operation_ticket exam_ticket_bll = new Competition.Mysql.BLL.pow_exam_operation_ticket(); public DownloadExamController(IWebHostEnvironment webHostEnvironment) { _webHostEnvironment = webHostEnvironment; } /// /// 下载考试接口 /// /// 考试id /// [Authorize] [HttpGet] [APIFilter] public ActionResult Index(string ExamId) { try { //组织树 var tree_list = ReadTree(); HSSFWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(); ISheet sheet1 = workbook.CreateSheet("考试试卷"); var row1 = sheet1.CreateRow(0);//选择第1行 var cell1_1 = row1.CreateCell(0);//选择第1列 cell1_1.SetCellValue("考试名称"); var cell1_2 = row1.CreateCell(1); cell1_2.SetCellValue("考试时长"); var cell1_3 = row1.CreateCell(2); cell1_3.SetCellValue("台区"); var cell1_4 = row1.CreateCell(3); cell1_4.SetCellValue("线路"); var cell1_5 = row1.CreateCell(4); cell1_5.SetCellValue("工单内容"); var cell1_6 = row1.CreateCell(5); cell1_6.SetCellValue("背景资料"); var cell1_7 = row1.CreateCell(6); cell1_7.SetCellValue("进线电缆型号及长度"); var cell1_8 = row1.CreateCell(7); cell1_8.SetCellValue("出线电缆型号及长度"); ISheet sheet2 = workbook.CreateSheet("巡线考试工具"); var row2 = sheet2.CreateRow(0);//选择第1行 var cell2_1 = row2.CreateCell(0);//选择第1列 cell2_1.SetCellValue("工具名称"); var cell2_2 = row2.CreateCell(1); cell2_2.SetCellValue("类型(工具、材料、着装)"); ISheet sheet3 = workbook.CreateSheet("排故考试工具"); var row3 = sheet3.CreateRow(0);//选择第1行 var cell3_1 = row3.CreateCell(0);//选择第1列 cell3_1.SetCellValue("工具名称"); var cell3_2 = row3.CreateCell(1); cell3_2.SetCellValue("类型(工具、材料、着装)"); ISheet sheet4 = workbook.CreateSheet("考试故障"); var row4 = sheet4.CreateRow(0);//选择第1行 var cell4_1 = row4.CreateCell(0);//选择第1列 cell4_1.SetCellValue("一级设备"); var cell4_2 = row4.CreateCell(1); cell4_2.SetCellValue("二级设备"); var cell4_3 = row4.CreateCell(2); cell4_3.SetCellValue("三级设备"); var cell4_4 = row4.CreateCell(3); cell4_4.SetCellValue("瓷瓶子类型(裙边1、裙边2、裙边3、裙边4)"); var cell4_5 = row4.CreateCell(4); cell4_5.SetCellValue("故障现象"); var cell4_6 = row4.CreateCell(5); cell4_6.SetCellValue("故障等级"); var cell4_7 = row4.CreateCell(6); cell4_7.SetCellValue("是否具体模拟"); var cell4_8 = row4.CreateCell(7); cell4_8.SetCellValue("故障具体位置"); var cell4_9 = row4.CreateCell(8); cell4_9.SetCellValue("查找缺陷分值"); var cell4_10 = row4.CreateCell(9); cell4_10.SetCellValue("故障缺陷分值"); var cell4_11 = row4.CreateCell(10); cell4_11.SetCellValue("处理缺陷分值"); var cell4_12 = row4.CreateCell(11); cell4_12.SetCellValue("故障状态"); var cell4_13 = row4.CreateCell(12); cell4_13.SetCellValue("具体故障,多个故障以|分隔"); var cell4_14 = row4.CreateCell(13); cell4_14.SetCellValue("难度等级 高、中、低"); var cell4_15 = row4.CreateCell(14); cell4_15.SetCellValue("故障点位置 高,中,低"); var cell4_16 = row4.CreateCell(15); cell4_16.SetCellValue("故障点位置角度度数"); var cell4_17 = row4.CreateCell(16); cell4_17.SetCellValue("位置 左、中、右"); var cell4_18 = row4.CreateCell(17); cell4_18.SetCellValue("熔芯正常值"); var cell4_19 = row4.CreateCell(18); cell4_19.SetCellValue("熔芯故障值"); var cell4_20 = row4.CreateCell(19); cell4_20.SetCellValue("方向 东、西、南、北"); var cell4_21 = row4.CreateCell(20); cell4_21.SetCellValue("缺陷详细信息"); var cell4_22 = row4.CreateCell(21); cell4_22.SetCellValue("题干"); var cell4_23 = row4.CreateCell(22); cell4_23.SetCellValue("选项A"); var cell4_24 = row4.CreateCell(23); cell4_24.SetCellValue("选项B"); var cell4_25 = row4.CreateCell(24); cell4_25.SetCellValue("选项C"); var cell4_26 = row4.CreateCell(25); cell4_26.SetCellValue("选项D"); var cell4_27 = row4.CreateCell(26); cell4_27.SetCellValue("选项E"); var cell4_28 = row4.CreateCell(27); cell4_28.SetCellValue("正确答案,多个以|分隔"); var cell4_29 = row4.CreateCell(28); cell4_29.SetCellValue("断路器设置名称"); var cell4_30 = row4.CreateCell(29); cell4_30.SetCellValue("断路器参数名"); var cell4_31 = row4.CreateCell(30); cell4_31.SetCellValue("断路器参数值"); var cell4_32 = row4.CreateCell(31); cell4_32.SetCellValue("电缆名称"); var cell4_33 = row4.CreateCell(32); cell4_33.SetCellValue("电缆起点"); var cell4_34 = row4.CreateCell(33); cell4_34.SetCellValue("电缆终点"); var cell4_35 = row4.CreateCell(34); cell4_35.SetCellValue("型号长度"); ISheet sheet5 = workbook.CreateSheet("考试停电操作票选项"); var row5 = sheet5.CreateRow(0);//选择第1行 var cell5_1 = row5.CreateCell(0); cell5_1.SetCellValue("序号"); var cell5_2 = row5.CreateCell(1); cell5_2.SetCellValue("内容"); ISheet sheet6 = workbook.CreateSheet("考试送电操作票选项"); var row6 = sheet6.CreateRow(0);//选择第1行 var cell6_1 = row6.CreateCell(0); cell6_1.SetCellValue("序号"); var cell6_2 = row6.CreateCell(1); cell6_2.SetCellValue("内容"); var model = new CompetitionAPI.api.back.JsonTurnExcelRequest(); model.Exam = exam_bll.GetAllList(" T1.ExamId='" + ExamId + "' ").FirstOrDefault(); model.ToolList = exam_tool_bll.GetModelList(" ExamId='" + ExamId + "' order by Purpose,ToolType asc "); model.FaultList = exam_fault_bll.GetModelList(" ExamId='" + ExamId + "' "); model.OperationTicketList = exam_ticket_bll.GetModelList(" ExamId='" + ExamId + "' order by Type,SerialNumber asc "); if (model != null) { if (model.Exam != null) { var row = sheet1.CreateRow(1);//选择第1行 var cell1 = row.CreateCell(0); cell1.SetCellValue(model.Exam.ExamName); var cell2 = row.CreateCell(1); cell2.SetCellValue(model.Exam.ExaminationDuration); var cell3 = row.CreateCell(2); cell3.SetCellValue(model.Exam.PlatformAreaName); var cell4 = row.CreateCell(3); cell4.SetCellValue(model.Exam.LineName); var cell5 = row.CreateCell(4); cell5.SetCellValue(model.Exam.WorkOrderContent); var cell6 = row.CreateCell(5); cell6.SetCellValue(model.Exam.BackgroundInformation); var cell7 = row.CreateCell(6); cell7.SetCellValue(model.Exam.IncomingLineModelLength); var cell8 = row.CreateCell(7); cell8.SetCellValue(model.Exam.OutgoingLineModelLength); } if (model.ToolList != null) { if (model.ToolList.Count > 0) { var line_tool = model.ToolList.Where(a => a.Purpose == "巡线").ToList(); for (int i = 0; i < line_tool.Count(); i++) { var row = sheet2.CreateRow(i + 1);//选择第1行 var cell1 = row.CreateCell(0); cell1.SetCellValue(line_tool[i].ToolName); var cell2 = row.CreateCell(1); cell2.SetCellValue(line_tool[i].ToolType); } var fault_tool = model.ToolList.Where(a => a.Purpose == "排故").ToList(); for (int i = 0; i < fault_tool.Count(); i++) { var row = sheet3.CreateRow(i + 1);//选择第1行 var cell1 = row.CreateCell(0); cell1.SetCellValue(fault_tool[i].ToolName); var cell2 = row.CreateCell(1); cell2.SetCellValue(fault_tool[i].ToolType); } } } if (model.FaultList != null) { if (model.FaultList.Count > 0) { for (int i = 0; i < model.FaultList.Count; i++) { var row = sheet4.CreateRow(i + 1);//选择第1行 var cell1 = row.CreateCell(0); var cell2 = row.CreateCell(1); var cell3 = row.CreateCell(2); var first_list = tree_list.Where(a => a.TowerId == model.FaultList[i].RootDeviceId).FirstOrDefault(); if (first_list != null) { cell1.SetCellValue(first_list.Tower); if (first_list.TowerId != model.FaultList[i].DeviceId) { if (first_list.Data != null) { var two_list = first_list.Data.Where(a => a.PowerDistributionBoxId == model.FaultList[i].DeviceId).FirstOrDefault(); if (two_list != null) { cell2.SetCellValue(two_list.PowerDistributionBox); if (two_list.Data != null) { var three_list = two_list.Data.Where(a => a.LineId == model.FaultList[i].DeviceId).FirstOrDefault(); if (three_list != null) { cell3.SetCellValue(three_list.Line); } } } else { var three_list = first_list.Data.SelectMany(a => a.Data).Where(a => a.LineId == model.FaultList[i].DeviceId).FirstOrDefault(); if (three_list != null) { foreach (var box in first_list.Data) { var line_model = box.Data.Where(a => a.LineId == model.FaultList[i].DeviceId).FirstOrDefault(); if (line_model != null) { cell2.SetCellValue(box.PowerDistributionBox); } } cell3.SetCellValue(three_list.Line); } } } } } var cell4 = row.CreateCell(3); cell4.SetCellValue(model.FaultList[i].PorcelainBottleType); var cell5 = row.CreateCell(4); cell5.SetCellValue(model.FaultList[i].FaultDesc); var cell6 = row.CreateCell(5); cell6.SetCellValue(model.FaultList[i].DefectLevel); var cell7 = row.CreateCell(6); cell7.SetCellValue(model.FaultList[i].SpecificSimulation); var cell8 = row.CreateCell(7); cell8.SetCellValue(model.FaultList[i].FaultPosition); var cell9 = row.CreateCell(8); cell9.SetCellValue(model.FaultList[i].SelectSocre); var cell10 = row.CreateCell(9); cell10.SetCellValue(model.FaultList[i].FaultScore); var cell11 = row.CreateCell(10); cell11.SetCellValue(model.FaultList[i].HandlerScore); var cell12 = row.CreateCell(11); if (model.FaultList[i].ChoiceQuestion == "0") { cell12.SetCellValue("正常"); } else if (model.FaultList[i].ChoiceQuestion == "1") { cell12.SetCellValue("单个故障"); } else if (model.FaultList[i].ChoiceQuestion == "2") { cell12.SetCellValue("多个故障"); } var cell13 = row.CreateCell(12); cell13.SetCellValue(model.FaultList[i].MultipleQuestion); var cell14 = row.CreateCell(13); cell14.SetCellValue(model.FaultList[i].DifficultyLevel); var cell15 = row.CreateCell(14); cell15.SetCellValue(model.FaultList[i].PositionValue); var cell16 = row.CreateCell(15); cell16.SetCellValue(model.FaultList[i].DegreesValue); var cell17 = row.CreateCell(16); cell17.SetCellValue(model.FaultList[i].Position); var cell18 = row.CreateCell(17); cell18.SetCellValue(model.FaultList[i].FusibleCoreNormal); var cell19 = row.CreateCell(18); cell19.SetCellValue(model.FaultList[i].FusibleCoreFault); var cell20 = row.CreateCell(19); cell20.SetCellValue(model.FaultList[i].Direction); var cell21 = row.CreateCell(20); if (model.FaultList[i].DegreesSerialNumber == "1") { cell21.SetCellValue("大缺口"); } if (model.FaultList[i].DegreesSerialNumber == "2") { cell21.SetCellValue("上缺口"); } if (model.FaultList[i].DegreesSerialNumber == "3") { cell21.SetCellValue("下缺口"); } if (model.FaultList[i].DegreesSerialNumber == "4") { cell21.SetCellValue("瓷瓶裂纹"); } var cell22 = row.CreateCell(21); cell22.SetCellValue(model.FaultList[i].Topic); var cell23 = row.CreateCell(22); cell23.SetCellValue(model.FaultList[i].OptionA); var cell24 = row.CreateCell(23); cell24.SetCellValue(model.FaultList[i].OptionB); var cell25 = row.CreateCell(24); cell25.SetCellValue(model.FaultList[i].OptionC); var cell26 = row.CreateCell(25); cell26.SetCellValue(model.FaultList[i].OptionD); var cell27 = row.CreateCell(26); cell27.SetCellValue(model.FaultList[i].OptionE); var cell28 = row.CreateCell(27); cell28.SetCellValue(model.FaultList[i].RightKey); var cell29 = row.CreateCell(28); cell29.SetCellValue(model.FaultList[i].Settings); var cell30 = row.CreateCell(29); cell30.SetCellValue(model.FaultList[i].Name); var cell31 = row.CreateCell(30); cell31.SetCellValue(model.FaultList[i].Value); var cell32 = row.CreateCell(31); cell32.SetCellValue(model.FaultList[i].CableName); var cell33 = row.CreateCell(32); cell33.SetCellValue(model.FaultList[i].CableStart); var cell34 = row.CreateCell(33); cell34.SetCellValue(model.FaultList[i].CableEnd); var cell35 = row.CreateCell(34); cell35.SetCellValue(model.FaultList[i].ModelLength); } } } if (model.OperationTicketList != null) { if (model.OperationTicketList.Count > 0) { var power_failure_ticket = model.OperationTicketList.Where(a => a.Type == "停电").ToList(); for (int i = 0; i < power_failure_ticket.Count(); i++) { var row = sheet5.CreateRow(i + 1);//选择第1行 var cell1 = row.CreateCell(0); cell1.SetCellValue(power_failure_ticket[i].SerialNumber); var cell2 = row.CreateCell(1); cell2.SetCellValue(power_failure_ticket[i].Content); } var power_transmission_ticket = model.OperationTicketList.Where(a => a.Type == "送电").ToList(); for (int i = 0; i < power_transmission_ticket.Count(); i++) { var row = sheet6.CreateRow(i + 1);//选择第1行 var cell1 = row.CreateCell(0); cell1.SetCellValue(power_transmission_ticket[i].SerialNumber); var cell2 = row.CreateCell(1); cell2.SetCellValue(power_transmission_ticket[i].Content); } } } } //保存Workbook方式二: 保存到内存流中 var stream = new MemoryStream(); workbook.Write(stream); stream.Flush(); stream.Position = 0; sheet1 = null; sheet2 = null; sheet3 = null; sheet4 = null; sheet5 = null; workbook = null; var exam_name = "考试数据"; if (model != null) { if (model.Exam != null) { exam_name = model.Exam.ExamName; } } var ExcelName = exam_name + ".xls"; //文件下载 return File(stream, "application/vnd.ms-excel", ExcelName); } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } /// /// 获取树结构json /// /// private List ReadTree() { var tree_list = new List(); string fault_tree_file = _webHostEnvironment.WebRootPath + "/Json/FaultTree.json"; //读取json文件 using (StreamReader file = System.IO.File.OpenText(fault_tree_file)) { using (JsonTextReader reader = new JsonTextReader(file)) { tree_list = JsonConvert.DeserializeObject>(JToken.ReadFrom(reader).ToString()); } } return tree_list; } } }