using BLL; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using Model; using NPOI.SS.Formula.Functions; namespace FineUIPro.Web.TestRun.ProduceTestRun { public partial class RunningLogManagementList : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //绑定数据 InitTreeMenu(); BindGrid(); } } #region 树绑定 /// /// 加载树 /// private void InitTreeMenu() { this.tvControlItem.Nodes.Clear(); TreeNode rootNode = new TreeNode(); rootNode.Text = "装置"; rootNode.NodeID = "0"; rootNode.Expanded = true; rootNode.ToolTip = ""; rootNode.EnableClickEvent = true; this.tvControlItem.Nodes.Add(rootNode); var list = Funs.DB.PreRun_SysDevice.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.PreRunLevel == 1).OrderBy(x => x.Sort).ToList(); if (list.Count() > 0) { foreach (var itemOne in list) { TreeNode rootOneNode = new TreeNode(); rootOneNode.NodeID = itemOne.PreRunId; rootOneNode.Text = itemOne.PreRunName; rootOneNode.ToolTip = itemOne.PreRunName; rootOneNode.CommandName = ""; rootOneNode.EnableClickEvent = true; rootOneNode.EnableExpandEvent = false; rootNode.Nodes.Add(rootOneNode); rootOneNode.Expanded = true; } } } /// /// 点击TreeView /// protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e) { this.BindGrid(); } #endregion #region 绑定数据 /// /// 数据绑定 /// public void BindGrid() { var years = DateTime.Now.Year; string strSql = @"select a.RunningId,a.ProjectId,project.ProjectName as ProjectName,a.InstallationId,a.ProcessesId,a.SystemId,inst.PreRunName as InstallationName,proce.PreRunName as ProcessesName,syst.PreRunName as SystemName,a.JobNo,a.ShiftType,(case a.ShiftType when 1 then '白班' else '夜班' end) as ShiftTypeName,a.ShiftUser,shiftuser.UserName as ShiftUserName,a.SuccessionUser,successuser.UserName as SuccessionUserName,a.StartData,a.EndData,a.AddUser,a.AddTime,a.Sort from Running_LogManagement as a inner join Sys_User as shiftuser on shiftuser.UserId=a.ShiftUser inner join Sys_User as successuser on successuser.UserId=a.SuccessionUser inner join Base_Project as project on project.ProjectId=a.ProjectId inner join PreRun_SysDevice as inst on inst.PreRunId=a.InstallationId inner join PreRun_SysDevice as proce on proce.PreRunId=a.ProcessesId inner join PreRun_SysDevice as syst on syst.PreRunId=a.SystemId where a.ProjectId=@ProjectId "; List listStr = new List(); listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId)); if (!string.IsNullOrWhiteSpace(this.tvControlItem.SelectedNodeID)) { if (this.tvControlItem.SelectedNodeID != "0" && !string.IsNullOrWhiteSpace(this.tvControlItem.SelectedNodeID)) { strSql += " and a.InstallationId=@InstallationId"; listStr.Add(new SqlParameter("@InstallationId", this.tvControlItem.SelectedNodeID)); } } if (!string.IsNullOrWhiteSpace(txtStartData.Text)) { strSql += " and a.StartData>=@StartData"; listStr.Add(new SqlParameter("@StartData", DateTime.Parse(txtStartData.Text).ToString("yyyy-MM-dd") + " 00:00:00")); years = DateTime.Parse(txtStartData.Text).Year; } if (!string.IsNullOrWhiteSpace(txtEndData.Text)) { strSql += " and a.EndData<=@EndData"; listStr.Add(new SqlParameter("@EndData", DateTime.Parse(txtEndData.Text).ToString("yyyy-MM-dd") + " 23:59:59")); } if (!string.IsNullOrWhiteSpace(ddlShiftType.SelectedValue)) { strSql += " and a.ShiftType=@ShiftType"; listStr.Add(new SqlParameter("@ShiftType", ddlShiftType.SelectedValue)); } strSql += " order by inst.PreRunLevel,inst.Sort,proce.Sort,syst.Sort asc"; SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); if (Funs.DB.Running_LogManagement.Count(x => x.StartData.Value.Year == years) > 0) { var minTime = Funs.DB.Running_LogManagement.Where(x => x.StartData.Value.Year == years).Min(m => m.StartData); lblminTime.Text = $"{years}年日志最小日期:{minTime.Value.ToString("yyyy-MM-dd")}"; } else { lblminTime.Text = $"{years}年无日志信息"; } } /// /// 分页 /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { Grid1.PageIndex = e.NewPageIndex; BindGrid(); } /// /// 分页下拉框 /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } /// /// 排序 /// protected void Grid1_Sort(object sender, GridSortEventArgs e) { BindGrid(); } /// /// 双击修改 /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { btnMenuModify_Click(null, null); } #endregion #region 按钮 /// /// 关闭弹框 /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } /// /// 搜索 /// protected void btnQuery_Click(object sender, EventArgs e) { BindGrid(); } /// /// 添加 /// protected void btnAdd_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("RunningLogManagementEdit.aspx?RunningId=", "新增 - "))); } /// /// 修改 /// protected void btnModify_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("RunningLogManagementEdit.aspx?RunningId={0}", Grid1.SelectedRowID, "编辑 - "))); } /// /// 删除 /// protected void btnDel_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } var ids = Grid1.SelectedRowIDArray.ToList(); var list = Funs.DB.Running_LogManagement.Where(x => ids.Contains(x.RunningId)).ToList(); if (list.Count > 0) { Funs.DB.Running_LogManagement.DeleteAllOnSubmit(list); Funs.DB.SubmitChanges(); BindGrid(); Alert.ShowInTop("删除成功!", MessageBoxIcon.Success); } } /// /// 导出 /// protected void btnExport_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0 || Grid1.SelectedRowIndexArray.Length > 1) { Alert.ShowInTop("请至少选择记录,最大只可选择一条!", MessageBoxIcon.Warning); return; } var runningId = this.Grid1.Rows[Grid1.SelectedRowIndex].DataKeys[0].ToString(); string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } var data = from a in Funs.DB.Running_LogManagement join b in Funs.DB.Base_Project on a.ProjectId equals b.ProjectId join c in Funs.DB.Sys_User on a.ShiftUser equals c.UserId join d in Funs.DB.Sys_User on a.SuccessionUser equals d.UserId where a.RunningId == runningId select new { a.RunningId, a.ProjectId, a.InstallationId, a.ProcessesId, a.SystemId, a.JobNo, a.ShiftType, ShiftTypeName = a.ShiftType == "1" ? "白班" : "夜班", a.ShiftUser, ShiftUserName = c.UserName, a.SuccessionUser, SuccessionUserName = d.UserName, a.HandoverCare, a.StartData, a.EndData, a.Situation, a.Remarks, a.AddUser, a.AddTime, a.Sort }; var model = data.FirstOrDefault(); if (model != null) { //操作问题 var operations = Funs.DB.Running_Operation.Where(x => x.RunningId == model.RunningId).ToList(); //维护问题 var maintes = Funs.DB.Running_Maintenance.Where(x => x.RunningId == model.RunningId).ToList(); string ReportFileName = $"{filePath}Production report({DateTime.Now.ToString("yyyy-MM-dd")} {model.ShiftUserName}).xlsx"; int rowIndex = 0; XSSFWorkbook hssfworkbook = new XSSFWorkbook(); XSSFSheet ws = (XSSFSheet)hssfworkbook.CreateSheet($"Production report({DateTime.Now.ToString("yyyy-MM-dd")} {model.ShiftUserName})"); #region 列宽 ws.SetColumnWidth(0, (9 * 256)); ws.SetColumnWidth(1, (9 * 256)); ws.SetColumnWidth(2, (43 * 256)); ws.SetColumnWidth(3, (7 * 256)); ws.SetColumnWidth(4, (8 * 256)); ws.SetColumnWidth(5, (5 * 256)); #endregion #region 样式 //头部样式居中 ICellStyle titleStyle = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 14, true, true); //头部样式靠左 ICellStyle leftTitleStyle = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Left, 14, true, true); //公共样式 ICellStyle style = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10.5, true); //公共样式靠左 ICellStyle leftStyle = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Left, 10.5, true); //公共样式靠左上对其 ICellStyle leftTopStyle = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Top, HorizontalAlignment.Left, 10.5, true); //公共样式加粗 ICellStyle styleBold = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10.5, true, true); //公共样式靠左加粗 ICellStyle styleLeftBold = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Left, 10.5, true, true); #endregion #region 头部 ws = ExcelCreateRowTitle(ws, hssfworkbook, style, rowIndex, rowIndex + 5, 0, 5); //行1 var region = new CellRangeAddress(rowIndex, rowIndex + 3, 0, 1); ws.AddMergedRegion(region); ws.GetRow(rowIndex).GetCell(0).SetCellValue("WUHUAN ENGINEERING CO.,LTD"); ws.GetRow(rowIndex).GetCell(0).CellStyle = titleStyle; ws.GetRow(rowIndex).GetCell(2).SetCellValue("PT PETROKIMIA GRESIK"); ws.GetRow(rowIndex).GetCell(2).CellStyle = titleStyle; region = new CellRangeAddress(rowIndex, rowIndex, 3, 5); ws.AddMergedRegion(region); ws.GetRow(rowIndex).GetCell(3).SetCellValue("WUHUAN Job. No."); ws.GetRow(rowIndex).GetCell(3).CellStyle = styleBold; //行2 ws.GetRow(rowIndex + 1).GetCell(2).SetCellValue("AMMONIA-Urea II PROJECT"); ws.GetRow(rowIndex + 1).GetCell(2).CellStyle = titleStyle; region = new CellRangeAddress(rowIndex + 1, rowIndex + 1, 3, 5); ws.AddMergedRegion(region); //行3行4 region = new CellRangeAddress(rowIndex + 2, rowIndex + 3, 2, 2); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 2).GetCell(2).SetCellValue("Production report"); ws.GetRow(rowIndex + 2).GetCell(2).CellStyle = titleStyle; region = new CellRangeAddress(rowIndex + 2, rowIndex + 3, 3, 5); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 2).GetCell(3).SetCellValue("Page 1"); ws.GetRow(rowIndex + 2).GetCell(3).CellStyle = titleStyle; //行5 region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 0, 5); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 4).GetCell(0).SetCellValue($"Shift:({model.ShiftUserName}) Succession:({model.SuccessionUserName}) {model.StartData.Value.ToString("yyyy-MM-dd")}— {model.EndData.Value.ToString("yyyy-MM-dd")}"); ws.GetRow(rowIndex + 4).GetCell(0).CellStyle = styleBold; //行6 region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 0, 3); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 5).GetCell(0).SetCellValue("Operation situation"); ws.GetRow(rowIndex + 5).GetCell(0).CellStyle = styleBold; region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 4, 5); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 5).GetCell(4).SetCellValue("Remarks"); ws.GetRow(rowIndex + 5).GetCell(4).CellStyle = styleBold; #endregion #region 数据 var dataIndex = 5; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 3); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue("DW Station 脱盐水"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = styleLeftBold; region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 4, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(4).SetCellValue(""); ws.GetRow(dataIndex + 1).GetCell(4).CellStyle = leftStyle; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 2, dataIndex + 2, 0, 5, 90); region = new CellRangeAddress(dataIndex + 2, dataIndex + 2, 0, 3); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 2).GetCell(0).SetCellValue(model.Situation); ws.GetRow(dataIndex + 2).GetCell(0).CellStyle = leftStyle; region = new CellRangeAddress(dataIndex + 2, dataIndex + 2, 4, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 2).GetCell(4).SetCellValue(model.Remarks); ws.GetRow(dataIndex + 2).GetCell(4).CellStyle = leftStyle; dataIndex += 2; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue("Attention of next shift交班注意"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = styleLeftBold; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 2, dataIndex + 2, 0, 5, 30); region = new CellRangeAddress(dataIndex + 2, dataIndex + 2, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 2).GetCell(0).SetCellValue(model.HandoverCare); ws.GetRow(dataIndex + 2).GetCell(0).CellStyle = leftStyle; dataIndex += 2; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue("operation issue操作问题"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = styleLeftBold; dataIndex += 1; foreach (var itemoption in operations) { ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5, 30); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue($"reason原因:{itemoption.OperationReason}"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = leftStyle; dataIndex += 1; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5, 30); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue($"solution处理:{itemoption.OperationHandle}"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = leftStyle; dataIndex += 1; } ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue("maintenance issue维护问题"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = styleLeftBold; dataIndex += 1; foreach (var itemmait in maintes) { ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5, 30); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue($"reason原因:{itemmait.MaintenanceReason}"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = leftStyle; dataIndex += 1; ws = ExcelCreateRowTitle(ws, hssfworkbook, style, dataIndex + 1, dataIndex + 1, 0, 5, 30); region = new CellRangeAddress(dataIndex + 1, dataIndex + 1, 0, 5); ws.AddMergedRegion(region); ws.GetRow(dataIndex + 1).GetCell(0).SetCellValue($"solution处理:{itemmait.MaintenanceHandle}"); ws.GetRow(dataIndex + 1).GetCell(0).CellStyle = leftStyle; dataIndex += 1; } #endregion ws.PrintSetup.Landscape = false; ws.PrintSetup.PaperSize = 9; ws.ForceFormulaRecalculation = true; using (FileStream filess = File.OpenWrite(ReportFileName)) { hssfworkbook.Write(filess); } FileInfo filet = new FileInfo(ReportFileName); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode($"Production report({DateTime.Now.ToString("yyyy-MM-dd")} {model.ShiftUserName}).xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } } /// ///右击修改 /// protected void btnMenuModify_Click(object sender, EventArgs e) { btnModify_Click(null, null); } #endregion #region 私有方法 /// /// 创建样式 /// /// public static ICellStyle SetExcelStyle(XSSFWorkbook wb, BorderStyle Bottom, BorderStyle Left, BorderStyle Right, BorderStyle Top, VerticalAlignment VerAig, HorizontalAlignment HorAig, double FontSize, bool WrapText = true, bool Bold = false, string FontName = "宋体") { ICellStyle style = wb.CreateCellStyle(); style.BorderBottom = Bottom; style.BorderLeft = Left; style.BorderRight = Right; style.BorderTop = Top; style.VerticalAlignment = VerAig; style.Alignment = HorAig; IFont font = wb.CreateFont(); font.FontHeightInPoints = FontSize; font.IsBold = Bold; font.FontName = FontName; style.SetFont(font); style.WrapText = WrapText; return style; } /// /// 创建头部 /// /// private XSSFSheet ExcelCreateRowTitle(XSSFSheet ws, XSSFWorkbook hssfworkbook, ICellStyle style, int sRows, int eRows, int cStart, int cEnd, float height = 21) { for (int i = sRows; i <= eRows; i++) { ws.CreateRow(i); ws.GetRow(i).HeightInPoints = height; for (int j = cStart; j <= cEnd; j++) { ws.GetRow(i).CreateCell(j); ws.GetRow(i).CreateCell(j).CellStyle = style; } } return ws; } #endregion } }