using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using NPOI.SS.UserModel; namespace FineUIPro.Web.HSSE.HazardousMG { /// /// 危大、超危工程清单页面 /// public partial class HazardProject : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); this.BindGrid(); } } /// /// 新增按钮事件 /// protected void btnNew_Click(object sender, EventArgs e) { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { Alert.ShowInTop("请先选择项目!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference("HazardProjectEdit.aspx")); } #endregion #region 数据绑定 /// /// 绑定数据 /// private void BindGrid() { if (this.CurrUser == null || string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { Alert.ShowInTop("请先登录或选择项目!", MessageBoxIcon.Warning); return; } List list = this.GetFilteredList(); Grid1.RecordCount = list.Count; var table = this.GetPagedDataTable(Grid1, list); Grid1.DataSource = table; Grid1.DataBind(); } /// /// 获取按查询条件过滤后的清单列表 /// private List GetFilteredList() { List list = BLL.HazardProjectService.GetList(this.CurrUser.LoginProjectId); ////查询条件过滤 string name = this.txtProjectName.Text.Trim(); if (!string.IsNullOrEmpty(name)) { list = list.Where(x => x.ProjectName.Contains(name)).ToList(); } if (!string.IsNullOrEmpty(this.ddlRiskLevel.SelectedValue)) { list = list.Where(x => x.RiskLevel == this.ddlRiskLevel.SelectedValue).ToList(); } ////开工时间范围过滤 if (this.dpStartDateFrom.SelectedDate.HasValue) { DateTime startFrom = this.dpStartDateFrom.SelectedDate.Value; list = list.Where(x => x.StartDate.HasValue && x.StartDate.Value >= startFrom).ToList(); } if (this.dpStartDateTo.SelectedDate.HasValue) { DateTime startTo = this.dpStartDateTo.SelectedDate.Value; list = list.Where(x => x.StartDate.HasValue && x.StartDate.Value <= startTo).ToList(); } ////结束时间范围过滤 if (this.dpEndDateFrom.SelectedDate.HasValue) { DateTime endFrom = this.dpEndDateFrom.SelectedDate.Value; list = list.Where(x => x.EndDate.HasValue && x.EndDate.Value >= endFrom).ToList(); } if (this.dpEndDateTo.SelectedDate.HasValue) { DateTime endTo = this.dpEndDateTo.SelectedDate.Value; list = list.Where(x => x.EndDate.HasValue && x.EndDate.Value <= endTo).ToList(); } return list; } /// /// 分页事件 /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { this.BindGrid(); } /// /// 每页记录数下拉事件 /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue); this.BindGrid(); } /// /// 排序事件 /// protected void Grid1_Sort(object sender, GridSortEventArgs e) { this.BindGrid(); } #endregion #region 查询 /// /// 查询 /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.BindGrid(); } #endregion #region 导入导出 /// /// 导出 /// protected void btnOut_Click(object sender, EventArgs e) { List list = this.GetFilteredList(); string[] cols = { "序号", "分部分项工程", "施工内容", "风险等级", "开工时间", "结束时间", "现场负责人", "备注" }; short[] widths = { 8, 30, 40, 16, 16, 16, 16, 30 }; string rootPath = Server.MapPath("~/"); string fullPath = rootPath + BLL.Const.ExcelUrl; if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } string fileName = "危大超危工程清单" + BLL.Funs.GetNewFileName() + ".xls"; string filePath = fullPath + fileName; BLL.Common.NPOIExcel excel = new BLL.Common.NPOIExcel(); ////表头样式:白字加粗、蓝底、带边框 IFont headerFont = excel.CreateFont(); headerFont.Boldweight = 700; headerFont.Color = BLL.Common.NPOIColor.WHITE; ICellStyle headerStyle = excel.CreateCellStyle(); headerStyle.SetFont(headerFont); headerStyle.FillForegroundColor = BLL.Common.NPOIColor.ROYAL_BLUE; headerStyle.FillPattern = FillPattern.SolidForeground; headerStyle.BorderTop = BorderStyle.Thin; headerStyle.BorderBottom = BorderStyle.Thin; headerStyle.BorderLeft = BorderStyle.Thin; headerStyle.BorderRight = BorderStyle.Thin; ////数据样式:带边框 ICellStyle dataStyle = excel.CreateCellStyle(); dataStyle.BorderTop = BorderStyle.Thin; dataStyle.BorderBottom = BorderStyle.Thin; dataStyle.BorderLeft = BorderStyle.Thin; dataStyle.BorderRight = BorderStyle.Thin; for (int i = 0; i < cols.Length; i++) { excel.SetValue(0, i, cols[i]); excel.SetStyle(0, i, headerStyle); excel.SetColumnWidth(i, widths[i]); } int rowIndex = 1; foreach (Model.HSSE_HazardProject item in list) { excel.SetValue(rowIndex, 0, rowIndex.ToString()); excel.SetValue(rowIndex, 1, item.ProjectName ?? string.Empty); excel.SetValue(rowIndex, 2, item.WorkContent ?? string.Empty); excel.SetValue(rowIndex, 3, item.RiskLevel ?? string.Empty); excel.SetValue(rowIndex, 4, item.StartDate.HasValue ? item.StartDate.Value.ToString("yyyy-MM-dd") : string.Empty); excel.SetValue(rowIndex, 5, item.EndDate.HasValue ? item.EndDate.Value.ToString("yyyy-MM-dd") : string.Empty); excel.SetValue(rowIndex, 6, item.ManagerMan ?? string.Empty); excel.SetValue(rowIndex, 7, item.Remark ?? string.Empty); for (int i = 0; i < cols.Length; i++) { excel.SetStyle(rowIndex, i, dataStyle); } rowIndex++; } excel.Save(filePath); FileInfo info = new FileInfo(filePath); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(filePath, 0, fileSize); Response.Flush(); Response.End(); } /// /// 导入 /// protected void btnImport_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Window2.GetShowReference("HazardProjectDataIn.aspx")); } /// /// 导入窗口关闭事件 /// protected void Window2_Close(object sender, WindowCloseEventArgs e) { this.BindGrid(); } #endregion #region 编辑 /// /// 双击行编辑事件 /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { this.EditData(); } /// /// 右键菜单编辑事件 /// protected void btnMenuEdit_Click(object sender, EventArgs e) { this.EditData(); } /// /// 编辑数据方法 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("HazardProjectEdit.aspx?HazardProjectId={0}", id))); } #endregion #region 删除 /// /// 右键菜单删除事件 /// protected void btnMenuDelete_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); BLL.HazardProjectService.Delete(rowID); } this.BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } } #endregion #region 关联数据查看 /// /// 右键菜单查看该危大、超危工程关联的安全巡检记录 /// protected void btnMenuSafetyPatrol_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(String.Format("window.open('SafetyPatrol.aspx?HazardProjectId={0}', '_blank');", id)); } /// /// 右键菜单查看该危大、超危工程关联的安全防护验收记录 /// protected void btnMenuSafetyAcceptance_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(String.Format("window.open('SafetyAcceptance.aspx?HazardProjectId={0}', '_blank');", id)); } #endregion #region 公示牌二维码 /// /// 生成现场公示牌二维码(安全员扫码后进入巡检) /// protected void btnQR_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; Model.HSSE_HazardProject info = BLL.HazardProjectService.GetById(id); if (info != null) { ////二维码内容:危大工程标识(编号),接入数据库后替换为巡检页面URL string strValue = "hazard$hid=" + info.HazardProjectId + "&hname=" + info.ProjectName + "&projectId=" + info.ProjectId; ////urlName 仅作生成的二维码图片文件名,用「主键+内容哈希」保证文件名安全(不含 ? / = 等非法字符)且内容变化时不复用旧图 string urlName = info.HazardProjectId + "_" + ((uint)strValue.GetHashCode()).ToString("X"); string title = "危大工程公示牌:" + info.ProjectName; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("~/Controls/ShowQRImage.aspx?strValue={0}&urlName={1}&title={2}", System.Web.HttpUtility.UrlEncode(strValue), System.Web.HttpUtility.UrlEncode(urlName), System.Web.HttpUtility.UrlEncode(title)), "公示牌二维码", 340, 400)); } } #endregion } }