using BLL; using Model; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; namespace FineUIPro.Web.Evaluation { public partial class SafePerformanceReport : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower(); txtReportDate.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM"); BindGrid(); if (this.CurrUser.Account == Const.Gly) { btnSync.Hidden = false; } } } private void BindGrid() { string strSql = @"select (CASE WHEN con.Contractor IS NULL THEN con.ContractorCN WHEN con.ContractorCN IS NULL THEN con.Contractor ELSE con.Contractor+con.ContractorCN END)AS Contractor, (CASE WHEN dis.Discipline IS NULL THEN dis.DisciplineCN WHEN dis.DisciplineCN IS NULL THEN dis.Discipline ELSE dis.Discipline+dis.DisciplineCN END) AS ServiceContent, ReportDate, r.ViolationScoreId, r.Fo_No,r.Points1,r.Points2,r.Points3,r.TotalScore,r.ServiceContent from ViolationScoreReport r left join Base_Contractor con on r.ContractorId=con.ContractorId LEFT JOIN dbo.Base_Discipline AS dis ON dis.DisciplineId =r.ServiceContent where r.ReportDate=@ReportDate "; List listStr = new List(); listStr.Add(new SqlParameter("@ReportDate", txtReportDate.Text.Trim())); 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(); } protected void Grid1_RowDataBound(object sender, GridRowEventArgs e) { DataRowView row = e.DataItem as DataRowView; int totalScore = Convert.ToInt32(row["TotalScore"]); if (totalScore<80) { e.RowCssClass = "color1"; } } protected void btnExtractMonth_Click(object sender, EventArgs e) { if (txtExtractMonth.Text != "") { DateTime exDate = Convert.ToDateTime(txtExtractMonth.Text + "-01"); List constractorIdList=new List(); var bcon = from x in Funs.DB.Base_Contractor select x; var condata = from x in Funs.DB.FC_SESRelatedData join y in Funs.DB.Base_Contractor on x.Contractor equals y.ContractorId where y.ContractorCN != "" && y.ContractorCN != null group y by y.ContractorCN into n select new { ContractorCN = n.Key }; var vioScore = from x in Funs.DB.ViolationScoreReport group x by x.ReportDate into n select new { ReportDate = n.Key }; if (vioScore.Select(x => x.ReportDate).Contains(txtExtractMonth.Text.Trim())) { Alert.ShowInTop("该月份数据已提取!"); } else { var cons = from x in Funs.DB.EMC_Contractor where x.WorkDate == txtExtractMonth.Text.Trim() select x; if (cons.Count() > 0) { foreach (var q in cons) { if (condata.Select(x => x.ContractorCN).Contains(q.ContractorCN)) { var c = bcon.Where(x => x.ContractorCN == q.ContractorCN); constractorIdList.Add(c.First().ContractorId); } } foreach (string constractorId in constractorIdList) { var dataList = from x in Funs.DB.FC_SESRelatedData where x.Contractor == constractorId && x.Expire_Date != null && x.Expire_Date.Value.Date >=exDate.Date select x; if (dataList.Count() > 0) { foreach (var data in dataList) { int num1 = 0; int num2 = 0; int num3 = 0; var punList = from x in Funs.DB.EMC_Punishment where x.FO_NO == data.FO_NO && x.PunishDate.Value.Year.ToString()== exDate.Year.ToString() && x.PunishDate.Value.Month.ToString() == exDate.Month.ToString() && x.Flag == "1" select x; if (punList.Count() > 0) { num1 = punList.Where(x => x.ViolationDegree == "1").Count(); num2 = punList.Where(x => x.ViolationDegree == "2").Count(); num3 = punList.Where(x => x.ViolationDegree == "3").Count(); } string newKeyID = SQLHelper.GetNewID(typeof(Model.ViolationScoreReport)); Model.ViolationScoreReport r = new Model.ViolationScoreReport(); r.ViolationScoreId = newKeyID; r.ReportDate = txtExtractMonth.Text.Trim(); r.ContractorId = constractorId; r.Fo_No = data.FO_NO; r.ServiceContent = data.DisciplineId; r.Points1 = num1 * 5; r.Points2 = num2 * 10; r.Points3 = num3 * 30; if ((r.Points1 + r.Points2 + r.Points3) < 100) { r.TotalScore = 100 - (r.Points1 + r.Points2 + r.Points3); } else { r.TotalScore = 0; } Funs.DB.ViolationScoreReport.InsertOnSubmit(r); Funs.DB.SubmitChanges(); } } } } Alert.ShowInTop("该月份数据提取成功!"); } } } protected void btnSelect_Click(object sender, EventArgs e) { BindGrid(); } protected void btnSync_Click(object sender, EventArgs e) { string username = "EpermitContractor_EFC"; string password = "nM3#yU5$pO8%aZ6"; //string baseUrl = "http://localhost/fclAPI/api/SyncContractor/GetContractorList"; string baseUrl = "https://fcl.basf-ypc.net.cn/api/api/SyncContractor/GetContractorList"; //BLL.Common.HttpHelper.HttpGetRequest(baseUrl); BLL.Common.HttpHelper.HttpGetRequest(baseUrl, username, password); } #region 导出 /// /// 导出 /// /// /// protected void btnExport_Click(object sender, EventArgs e) { Grid1.PageSize = 10000; BindGrid(); string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "安全绩效报告.xlsx"; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ReportFileName = filePath + "out.xls"; FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); XSSFSheet ws = (XSSFSheet)hssfworkbook.GetSheet("Sheet1"); #region 样式 // 字体样式 IFont font = hssfworkbook.CreateFont(); font.FontHeightInPoints = 11; font.IsBold = false; font.FontName = "Arial"; ICellStyle fontStyle = hssfworkbook.CreateCellStyle(); fontStyle.SetFont(font); ICellStyle yearStyle = hssfworkbook.CreateCellStyle(); yearStyle.VerticalAlignment = VerticalAlignment.Center; yearStyle.Alignment = HorizontalAlignment.Center; yearStyle.SetFont(font); //创建单元格样式 XSSFCellStyle backgroundstyle = (XSSFCellStyle)hssfworkbook.CreateCellStyle(); //填充模式 backgroundstyle.FillPattern = FillPattern.SolidForeground; //创建颜色 XSSFColor xssfcolor = new XSSFColor(); //rbg值 byte[] rgb = { (byte)255, (byte)192, (byte)203 }; //写入rgb 粉色背景颜色定义 xssfcolor.SetRgb(rgb); //设置颜色值 backgroundstyle.SetFillForegroundColor(xssfcolor); backgroundstyle.SetFont(font); string yearMonth = txtReportDate.Text.Trim() + "份安全绩效报告"; #endregion if (Grid1.Rows.Count > 0) { CellRangeAddress region = new CellRangeAddress(0, 0, 2, 6); ws.AddMergedRegion(region); if (ws.GetRow(0).GetCell(2) == null) ws.GetRow(0).CreateCell(2); ws.GetRow(0).GetCell(2).SetCellValue(yearMonth); //ws.GetRow(0).GetCell(2).CellStyle = fontStyle; var rowIndex = 2; for (int i = 0; i < Grid1.Rows.Count; i++) { //int rowID = Convert.ToInt32(Grid1.DataKeys[i][0]); if (ws.GetRow(rowIndex) == null) ws.CreateRow(rowIndex); #region 列赋值 if (ws.GetRow(rowIndex).GetCell(0) == null) ws.GetRow(rowIndex).CreateCell(0); ws.GetRow(rowIndex).GetCell(0).SetCellValue(rowIndex.ToString()); ws.GetRow(rowIndex).GetCell(0).CellStyle = fontStyle; if (ws.GetRow(rowIndex).GetCell(1) == null) ws.GetRow(rowIndex).CreateCell(1); ws.GetRow(rowIndex).GetCell(1).SetCellValue(Grid1.Rows[i].Values[1].ToString()); ws.GetRow(rowIndex).GetCell(1).CellStyle = fontStyle; if (ws.GetRow(rowIndex).GetCell(2) == null) ws.GetRow(rowIndex).CreateCell(2); ws.GetRow(rowIndex).GetCell(2).SetCellValue(Grid1.Rows[i].Values[2].ToString()); if (ws.GetRow(rowIndex).GetCell(3) == null) ws.GetRow(rowIndex).CreateCell(3); ws.GetRow(rowIndex).GetCell(3).SetCellValue(Grid1.Rows[i].Values[3].ToString()); if (ws.GetRow(rowIndex).GetCell(4) == null) ws.GetRow(rowIndex).CreateCell(4); ws.GetRow(rowIndex).GetCell(4).SetCellValue(Grid1.Rows[i].Values[4].ToString()); if (ws.GetRow(rowIndex).GetCell(5) == null) ws.GetRow(rowIndex).CreateCell(5); ws.GetRow(rowIndex).GetCell(5).SetCellValue(Grid1.Rows[i].Values[5].ToString()); if (ws.GetRow(rowIndex).GetCell(6) == null) ws.GetRow(rowIndex).CreateCell(6); ws.GetRow(rowIndex).GetCell(6).SetCellValue(Grid1.Rows[i].Values[6].ToString()); if (ws.GetRow(rowIndex).GetCell(7) == null) ws.GetRow(rowIndex).CreateCell(7); ws.GetRow(rowIndex).GetCell(7).SetCellValue(Grid1.Rows[i].Values[7].ToString()); rowIndex++; } #endregion } 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=" + yearMonth + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } #endregion #region 分页、排序 /// /// 分页 /// /// /// 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(); } #endregion #region 权限设置 /// /// 菜单按钮权限 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.SafePerformanceReportMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnOut)) { this.btnExport.Hidden = false; } else { btnExport.Hidden = true; } } } #endregion } }