using BLL; using BLL.Common; using NPOI.SS.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; namespace FineUIPro.Web.Customization.CNCCG.HSSE.Rewards { public partial class GreenSiteManager : PageBase { #region 加载页面 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ////权限按钮方法 this.GetButtonPower(); this.btnNew.OnClientClick = Window1.GetShowReference("GreenSiteEdit.aspx") + "return false;"; if (this.CurrUser != null && this.CurrUser.PageSize.HasValue) { Grid1.PageSize = this.CurrUser.PageSize.Value; } this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 this.BindGrid(); } } /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { if (Request.Params["value"] == "0") { return; } var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.GreenSiteMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuEdit.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnMenuDelete.Hidden = false; } } } /// /// 绑定数据 /// private void BindGrid() { string strSql = @"SELECT f.*,b.UnitName,p.ProjectName from HSSE_GreenSite f left join Base_Project p on f.projectid=p.projectid left join Base_Unit b on f.Unitid=b.UnitId where 1=1 "; List listStr = new List(); if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim())) { strSql += " AND f.CreateMonth = @CreateMonth"; listStr.Add(new SqlParameter("@CreateMonth", Convert.ToDateTime(this.txtStartDate.Text.Trim()).ToString())); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; //tb = GetFilteredTable(Grid1.FilteredData, tb); var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } /// /// 改变索引事件 /// /// /// 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, FineUIPro.GridSortEventArgs e) { Grid1.SortDirection = e.SortDirection; Grid1.SortField = e.SortField; BindGrid(); } /// /// 关闭弹出窗口 /// /// /// protected void Window1_Close(object sender, EventArgs e) { BindGrid(); } #endregion #region 查询 /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs 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; var result = GreenSiteService.Detail(id); if (result != null) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("GreenSiteEdit.aspx?Id={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(); var result = GreenSiteService.Detail(rowID); if (result != null) { GreenSiteService.Delete(rowID); } } this.BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } } #endregion #region 导出 /// /// 导出 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { string strSql = @"SELECT a.*,d.UnitName,c.ProjectName ,DateAdd(day,-1,DateAdd(month,1,a.CreateMonth)) endDateTime ,isnull((select count(1) from HSSE_EnvironmentalAward b where b.Projectid=a.Projectid AND b.CreateTime>=a.CreateMonth AND b.CreateTime<=DateAdd(day,-1,DateAdd(month,1,a.CreateMonth))),0) CellCount from HSSE_GreenSite a left join Base_Project c on a.projectid=c.projectid left join Base_Unit d on a.Unitid=d.UnitId where 1=1"; List listStr = new List(); if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim())) { strSql += " AND f.CreateMonth = @CreateMonth"; listStr.Add(new SqlParameter("@CreateMonth", Convert.ToDateTime(this.txtStartDate.Text.Trim()).ToString())); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); if (tb.Rows.Count <= 0) { ShowNotify("没有要导出的数据,请检查!", MessageBoxIcon.Warning); return; } //某单位最多奖项个数 int CellCountMax = tb.AsEnumerable().Select(t => t.Field("CellCount")).Max(); int CellCount = CellCountMax; NPOIExcel excel = new NPOIExcel(); int indexRow = 0; excel.SetColumnWidth(0, 10); excel.SetColumnWidth(1, 40); excel.SetColumnWidth(2, 70); excel.SetColumnWidth(3, 20); excel.SetColumnWidth(4, 20); for (int i = 0; i < CellCount; i++) { excel.SetColumnWidth(5 + i, 20); } //一个奖项或者零个奖项 if (CellCount <= 1) { excel.SetColumnWidth(5, 40); } excel.SetColumnWidth(5 + CellCount, 40); excel.SetColumnWidth(6 + CellCount, 40); excel.SetColumnWidth(7 + CellCount, 40); //样式 var styleHead = excel.CreateCellStyle(); styleHead.VerticalAlignment = VerticalAlignment.Center; styleHead.Alignment = HorizontalAlignment.Center; IFont font = excel.CreateFont(); font.IsBold = true; styleHead.SetFont(font); styleHead.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; styleHead.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; styleHead.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; styleHead.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; //样式-序号 var styleXh = excel.CreateCellStyle(); styleXh.VerticalAlignment = VerticalAlignment.Center; styleXh.Alignment = HorizontalAlignment.Center; styleXh.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; styleXh.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; styleXh.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; styleXh.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; //样式 var styleData = excel.CreateCellStyle(); styleData.VerticalAlignment = VerticalAlignment.Center; styleData.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; styleData.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; styleData.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; styleData.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; #region 标题 excel.SetValue(indexRow, 0, "序号"); excel.SetStyle(indexRow, 0, styleHead); excel.SetValue(indexRow, 1, "项目名称"); excel.SetStyle(indexRow, 1, styleHead); excel.SetValue(indexRow, 2, "所属单位"); excel.SetStyle(indexRow, 2, styleHead); excel.SetValue(indexRow, 3, "项目现场安全检查平均得分*90%"); excel.SetStyle(indexRow, 3, styleHead); excel.SetValue(indexRow, 4, "创建材料得分*10%"); excel.SetStyle(indexRow, 4, styleHead); for (int i = 0; i < CellCount; i++) excel.SetStyle(indexRow, 5 + i, styleHead); //合并单元格 if (CellCount > 1) excel.MergeCells(indexRow, 5, indexRow, 5 + CellCountMax - 1); excel.SetValue(indexRow, 5, "安全生产、生态环保、质量获奖处罚加减分"); excel.SetStyle(indexRow, 5, styleHead); //这里加一是为了后面列可以往后移动一位,不然就把奖项列覆盖了 if (CellCount == 0) CellCount = 1; excel.SetValue(indexRow, 5 + CellCount, "合计得分"); excel.SetStyle(indexRow, 5 + CellCount, styleHead); excel.SetValue(indexRow, 6 + CellCount, "编制月份"); excel.SetStyle(indexRow, 6 + CellCount, styleHead); excel.SetRowHeight(indexRow, 15); #endregion #region 数据列 int rowCount = 1; int rowIndex = 1; foreach (DataRow dr in tb.Rows) { //序号 excel.SetStyle(indexRow + rowCount, 0, styleXh); excel.SetStyle(indexRow + rowCount + 1, 0, styleXh); excel.MergeCells(indexRow + rowCount, 0, indexRow + rowCount + 1, 0); excel.SetValue(indexRow + rowCount, 0, rowIndex); //单位名称 excel.SetStyle(indexRow + rowCount, 1, styleData); excel.SetStyle(indexRow + rowCount + 1, 1, styleData); excel.MergeCells(indexRow + rowCount, 1, indexRow + rowCount + 1, 1); excel.SetValue(indexRow + rowCount, 1, dr["ProjectName"].ToString()); //(各子(分)公司机关安全检查平均得分*50%+所属项目安全检查平均得分*50%)*70% excel.SetStyle(indexRow + rowCount, 2, styleData); excel.SetStyle(indexRow + rowCount + 1, 2, styleData); excel.MergeCells(indexRow + rowCount, 2, indexRow + rowCount + 1, 2); excel.SetValue(indexRow + rowCount, 2, dr["UnitName"].ToString()); //日常管理得分*20% excel.SetStyle(indexRow + rowCount, 3, styleData); excel.SetStyle(indexRow + rowCount + 1, 3, styleData); excel.MergeCells(indexRow + rowCount, 3, indexRow + rowCount + 1, 3); excel.SetValue(indexRow + rowCount, 3, dr["Score1"].ToString()); excel.SetStyle(indexRow + rowCount, 3, styleData); //先进事迹材料得分*10% excel.SetStyle(indexRow + rowCount, 4, styleData); excel.SetStyle(indexRow + rowCount + 1, 4, styleData); excel.MergeCells(indexRow + rowCount, 4, indexRow + rowCount + 1, 4); excel.SetValue(indexRow + rowCount, 4, dr["Score2"].ToString()); excel.SetStyle(indexRow + rowCount, 4, styleData); //奖项 DateTime sTime = Convert.ToDateTime(dr["CreateMonth"].ToString()); DateTime eTime = Convert.ToDateTime(dr["endDateTime"].ToString()); var environmentalAwards = Funs.DB.HSSE_EnvironmentalAward.Where(x => x.ProjectId == dr["ProjectId"].ToString() && x.CreateTime >= sTime && x.CreateTime <= eTime).ToList(); for (int i = 0; i < CellCountMax; i++) { if (i >= environmentalAwards.Count) { excel.SetStyle(indexRow + rowCount, 5 + i, styleData); excel.SetStyle(indexRow + rowCount + 1, 5 + i, styleData); continue; } excel.SetStyle(indexRow + rowCount, 5 + i, styleData); excel.SetStyle(indexRow + rowCount + 1, 5 + i, styleData); excel.SetValue(indexRow + rowCount, 5 + i, environmentalAwards[i].AwardsName); excel.SetValue(indexRow + rowCount + 1, 5 + i, environmentalAwards[i].Scores); } //无奖项 处理 if (environmentalAwards.Count == 0) { if (CellCountMax > 1) excel.MergeCells(indexRow + rowCount, 5, indexRow + rowCount + 1, 5 + CellCountMax - 1); else excel.MergeCells(indexRow + rowCount, 5, indexRow + rowCount + 1, 5); excel.SetValue(indexRow + rowCount, 5, "无奖项"); excel.SetStyle(indexRow + rowCount, 5, styleData); } //合计得分 excel.SetStyle(indexRow + rowCount, 5 + CellCount, styleData); excel.SetStyle(indexRow + rowCount + 1, 5 + CellCount, styleData); excel.MergeCells(indexRow + rowCount, 5 + CellCount, indexRow + rowCount + 1, 5 + CellCount); excel.SetValue(indexRow + rowCount, 5 + CellCount, dr["AllScore"].ToString()); //编制月份 excel.SetStyle(indexRow + rowCount, 6 + CellCount, styleData); excel.SetStyle(indexRow + rowCount + 1, 6 + CellCount, styleData); excel.MergeCells(indexRow + rowCount, 6 + CellCount, indexRow + rowCount + 1, 6 + CellCount); excel.SetValue(indexRow + rowCount, 6 + CellCount, sTime.ToString("yyyy-MM-dd")); rowCount = rowCount + 2; rowIndex++; } #endregion string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //导出文件 string filePath = rootPath + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string initTemplatePath = Const.ExcelUrl + Funs.GetNewFileName() + ".xls"; string uploadfilepath = Server.MapPath("~/") + initTemplatePath; string fileName = Path.GetFileName(initTemplatePath); excel.Save(uploadfilepath); FileInfo filet = new FileInfo(uploadfilepath); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode($"安全质量绿色环保示范工地评选推荐表{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } #endregion protected string getScore(object unitId,object projectId, object CreateMonth) { var msg = ""; if (unitId != null && CreateMonth != null && projectId!=null) { var sTime = Convert.ToDateTime(CreateMonth); var eTime = sTime.AddMonths(1); var uid = unitId.ToString(); var list = Funs.DB.HSSE_EnvironmentalAward.Where(x => x.ProjectId== projectId.ToString() && x.CreateTime >= sTime && x.CreateTime <= eTime).ToList(); if (list.Count > 0) { foreach (var item in list) { msg += "奖项名称:" + item.AwardsName + ",加分值:" + item.Scores.ToString() + ";"; } if (!string.IsNullOrEmpty(msg)) { msg = msg.Substring(0, msg.Length - 1) + "。"; } } else { msg = "无奖项"; } } return msg; } } }