using BLL; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using Newtonsoft.Json; namespace FineUIPro.Web.Evaluation { public partial class ExportDetails : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower();//权限设置 StartTime.Text = DateTime.Now.AddMonths(-1).ToString(StartTime.DateFormatString); EndTime.Text = DateTime.Now.ToString(EndTime.DateFormatString); ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 BindGrid(); } } /// /// 绑定数据 /// private void BindGrid() { string strSql = @"SELECT * from View_FC_SESRelatedData WHERE 1=1 "; List listStr = new List(); if (!string.IsNullOrEmpty(this.txtFo.Text.Trim())) { strSql += " AND FO_NO LIKE @foNo"; listStr.Add(new SqlParameter("@foNo", "%" + this.txtFo.Text.Trim() + "%")); } 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(); } #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 打印 /// /// 打印 /// /// /// protected void btnPrint_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInParent("Please select at least one record!"); return; } string Id = Grid1.SelectedRowID; var fc = BLL.SESRelatedDataService.GetSESRelatedDataById(Id); string sData = StartTime.Text; string eData = EndTime.Text; PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("../Report/ReportPrint.aspx?report=4&fcid=" + fc.FO_NO + "&sData=" + sData + "&eData=" + eData + "", "Print - "))); } #endregion #region 导出 /// /// 导出 /// /// /// protected void btnExport_Click(object sender, EventArgs e) { DateTime startTime = DateTime.Parse(StartTime.Text + "-01"); DateTime endTime = DateTime.Parse(EndTime.Text + "-13"); int monthNum = (endTime.Year - startTime.Year) * 12 + (endTime.Month - startTime.Month) + 1; if (monthNum < 0) { Alert.ShowInParent("The start date cannot be greater than the end date!"); return; } string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "MonthScoreReport.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); var sesRelatedData = Funs.DB.View_FC_SESRelatedData.Where(x => x.FC_Status != "Closed").ToList(); List Param = new List(); Param.Add(new SqlParameter("@StartTime", startTime)); Param.Add(new SqlParameter("@EndTime", endTime)); SqlParameter[] ParList = Param.ToArray(); var sesDataTable = SQLHelper.GetDataTableRunProc("FN_YearToDateReport", ParList); var sesDataScore = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(sesDataTable)); XSSFSheet wsMonth = (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); #endregion if (sesRelatedData.Count > 0) { var colIndex = 0; var rowIndex = 1; foreach (var item in sesRelatedData) { if (wsMonth.GetRow(rowIndex) == null) wsMonth.CreateRow(rowIndex); #region 列赋值 if (wsMonth.GetRow(rowIndex).GetCell(0) == null) wsMonth.GetRow(rowIndex).CreateCell(0); wsMonth.GetRow(rowIndex).GetCell(0).SetCellValue(item.FO_NO); wsMonth.GetRow(rowIndex).GetCell(0).CellStyle = fontStyle; if (wsMonth.GetRow(rowIndex).GetCell(1) == null) wsMonth.GetRow(rowIndex).CreateCell(1); wsMonth.GetRow(rowIndex).GetCell(1).SetCellValue(item.Discipline); wsMonth.GetRow(rowIndex).GetCell(1).CellStyle = fontStyle; if (wsMonth.GetRow(rowIndex).GetCell(2) == null) wsMonth.GetRow(rowIndex).CreateCell(2); wsMonth.GetRow(rowIndex).GetCell(2).SetCellValue(item.Contractor); wsMonth.GetRow(rowIndex).GetCell(2).CellStyle = fontStyle; if (wsMonth.GetRow(rowIndex).GetCell(3) == null) wsMonth.GetRow(rowIndex).CreateCell(3); wsMonth.GetRow(rowIndex).GetCell(3).SetCellValue(item.Contract_Admin); wsMonth.GetRow(rowIndex).GetCell(3).CellStyle = fontStyle; if (wsMonth.GetRow(rowIndex).GetCell(4) == null) wsMonth.GetRow(rowIndex).CreateCell(4); wsMonth.GetRow(rowIndex).GetCell(4).SetCellValue(item.Main_Coordinator); wsMonth.GetRow(rowIndex).GetCell(4).CellStyle = fontStyle; var sesFoDataScore = sesDataScore.Where(p => p.FO_NO == item.FO_NO).ToList(); for (int j = 0; j < monthNum; j++) { var startData = startTime.AddMonths(j); if (colIndex == 0) { if (wsMonth.GetRow(0) == null) wsMonth.CreateRow(0); if (wsMonth.GetRow(0).GetCell(j + 5) == null) wsMonth.GetRow(0).CreateCell(j + 5); wsMonth.GetRow(0).GetCell(j + 5).SetCellValue(startData.ToString("yyyy-MM")); wsMonth.GetRow(0).GetCell(j + 5).CellStyle = yearStyle; } if (wsMonth.GetRow(rowIndex).GetCell(j + 5) == null) wsMonth.GetRow(rowIndex).CreateCell(j + 5); if (sesFoDataScore.Count(p => p.dataMonth == startData.ToString("yyyy-MM")) > 0 && Funs.GetNewDecimal(sesFoDataScore.FirstOrDefault(p => p.dataMonth == startData.ToString("yyyy-MM")).SumScore) > 0) { var yearNums = float.Parse(sesFoDataScore.FirstOrDefault(p => p.dataMonth == startData.ToString("yyyy-MM")).SumScore).ToString("0.##"); wsMonth.GetRow(rowIndex).GetCell(j + 5).SetCellValue(double.Parse(yearNums)); //如果小于60分,填充背景色为粉色 if (double.Parse(yearNums) > 0 && double.Parse(yearNums) < 60) { wsMonth.GetRow(rowIndex).GetCell(j + 5).CellStyle = backgroundstyle; } else { wsMonth.GetRow(rowIndex).GetCell(j + 5).CellStyle = fontStyle; } } else { wsMonth.GetRow(rowIndex).GetCell(j + 5).SetCellValue("-"); wsMonth.GetRow(rowIndex).GetCell(j + 5).CellStyle = fontStyle; } } #endregion colIndex++; rowIndex++; } } wsMonth.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=Month_TotalScore_Report" + 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 权限设置 /// /// 菜单按钮权限 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.ExportDetailsMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnOut)) { this.btnPrint.Hidden = false; } } } #endregion #region 行绑定后事件 /// /// 行绑定后事件 /// /// /// protected void Grid1_RowDataBound(object sender, GridRowEventArgs e) { DataRowView row = e.DataItem as DataRowView; string id = row["ID"].ToString(); //for (int i = 0; i < this.Grid1.Rows.Count; i++) //{ if (!string.IsNullOrEmpty(id)) { var data = BLL.SESRelatedDataService.GetSESRelatedDataById(id); if (data != null) { if (data.Expire_Date.HasValue) { if (data.Expire_Date < DateTime.Now)//过期 { e.RowCssClass = "color2"; } else if (data.Expire_Date.Value.AddMonths(-6) <= DateTime.Now && data.Expire_Date > DateTime.Now)//六个月后过期 { e.RowCssClass = "color1"; } } //Remaining Budget/Contract Budget<10 显示红色 var checkedValue = BLL.SESReportService.getSumSSRActualCostByFo(data.FO_NO); var remainmingBudget = data.Actual_Budget - checkedValue; if (data.Actual_Budget > 0) { var remainmingBudgetRate = remainmingBudget / data.Actual_Budget * 100; if (remainmingBudgetRate < 10) { e.CellCssClasses[30] = "colorRed"; } } //如果(合同过期日-今天)/(合同生效总天数)<10显示红色 if (data.Validate_Date.HasValue && data.Expire_Date.HasValue) { decimal a = (data.Expire_Date - DateTime.Now).Value.Days; decimal b = (data.Expire_Date - data.Validate_Date).Value.Days;//合同生效总天数 if (b > 0) { decimal c = a / b * 100; if (c < 10) { e.CellCssClasses[31] = "colorRed"; } } } } //} } } #endregion #region 查询 /// /// 查找 /// /// /// protected void btnSearch_Click(object sender, EventArgs e) { BindGrid(); } #endregion #region 定义实体 // 定义实体 public class YearToDateModel { public string FO_NO { get; set; } = string.Empty; public string Contract_Title { get; set; } = string.Empty; public string Contractor { get; set; } = string.Empty; public string Contract_Admin { get; set; } = string.Empty; public string Main_Coordinator { get; set; } = string.Empty; public string dataMonth { get; set; } = string.Empty; public string SumScore { get; set; } = string.Empty; } #endregion } }