using BLL; using Newtonsoft.Json.Linq; 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.Report { public partial class TimeSheetSummary : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower();//权限设置 BLL.DepartService.InitCTEDepartDropDownList(this.drpWorkCenter, false); ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 BindGrid(); } } /// /// 绑定数据 /// private void BindGrid() { List listStr = new List(); if (!string.IsNullOrEmpty(this.txtMonth.Text.Trim())) { listStr.Add(new SqlParameter("@YearMonths", this.txtMonth.Text.Trim())); } if (this.drpWorkCenter.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkCenter.SelectedItem.Text.Trim()) && this.drpWorkCenter.SelectedItem.Text != "CTE") { listStr.Add(new SqlParameter("@WorkCenter", this.drpWorkCenter.SelectedItem.Text.Trim())); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunProc("Proc_TimeSheetSummary", parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); OutputSummaryData(table);//计算和 } #endregion #region 合计列 private void OutputSummaryData(DataTable tb) { float TargetHours = 0.0f; float GrandHours = 0.0f; float NormalWorkHours = 0.0f; float OverTimeHours = 0.0f; float TripHours = 0.0f; float TrainingHours = 0.0f; float LeaveHours = 0.0f; float sickLeaveHours = 0.0f; float OtherHours = 0.0f; for (int i = 0; i < tb.Rows.Count; i++) { TargetHours += Convert.ToInt32(tb.Rows[i]["TargetHours"]); GrandHours += Convert.ToInt32(tb.Rows[i]["GrandHours"]); NormalWorkHours += Convert.ToInt32(tb.Rows[i]["NormalWorkHours"]); OverTimeHours += Convert.ToInt32(tb.Rows[i]["OverTimeHours"]); TripHours += Convert.ToInt32(tb.Rows[i]["TripHours"]); TrainingHours += Convert.ToInt32(tb.Rows[i]["TrainingHours"]); LeaveHours += Convert.ToInt32(tb.Rows[i]["LeaveHours"]); sickLeaveHours += Convert.ToInt32(tb.Rows[i]["SickLeaveHours"]); OtherHours += Convert.ToInt32(tb.Rows[i]["OtherHours"]); } JObject summary = new JObject(); //summary.Add("major", "全部合计"); summary.Add("TargetHours", TargetHours.ToString("F2")); summary.Add("GrandHours", GrandHours.ToString("F2")); summary.Add("NormalWorkHours", NormalWorkHours.ToString("F2")); summary.Add("OverTimeHours", OverTimeHours.ToString("F2")); summary.Add("TripHours", TripHours.ToString("F2")); summary.Add("TrainingHours", TrainingHours.ToString("F2")); summary.Add("LeaveHours", LeaveHours.ToString("F2")); summary.Add("SickLeaveHours", sickLeaveHours.ToString("F2")); summary.Add("OtherHours", OtherHours.ToString("F2")); Grid1.SummaryData = summary; } #endregion #region 查询 /// /// 下拉框选择事件 /// /// /// protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) { BindGrid(); } protected void txtMonth_TextChanged(object sender, EventArgs e) { BindGrid(); } #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 btnExport_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "Time_Sheet_Summary.xlsx"; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ReportFileName = filePath + "out.xlsx"; FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); #region Time_Sheet_Summary XSSFSheet reportModel = (XSSFSheet)hssfworkbook.GetSheet("Time_Sheet_Summary"); XSSFFont cs_content_Font = (XSSFFont)hssfworkbook.CreateFont(); //创建字体 cs_content_Font.FontName = "sans-serif";//字体 cs_content_Font.FontHeightInPoints = 10; //字体大小 List listStr = new List(); if (!string.IsNullOrEmpty(txtMonth.Text.Trim())) { listStr.Add(new SqlParameter("@YearMonths", txtMonth.Text.Trim())); } if (this.drpWorkCenter.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkCenter.SelectedValue) && this.drpWorkCenter.SelectedItem.Text != "CTE") { listStr.Add(new SqlParameter("@WorkCenter", drpWorkCenter.SelectedItem.Text.Trim())); } SqlParameter[] parameter = listStr.ToArray(); DataTable table = SQLHelper.GetDataTableRunProc("Proc_TimeSheetSummary", parameter); if (table.Rows.Count > 0) { var rowIndex = 1; double targetHours = 0; double grandHours = 0; double normalWorkHours = 0; double overTimeHours = 0; double tripHours = 0; double trainingHours = 0; double leaveHours = 0; double sickLeaveHours = 0; double otherHours = 0; for (int i = 0; i < table.Rows.Count; i++) { if (reportModel.GetRow(rowIndex) == null) reportModel.CreateRow(rowIndex); #region 列赋值 //Month if (reportModel.GetRow(rowIndex).GetCell(0) == null) reportModel.GetRow(rowIndex).CreateCell(0); reportModel.GetRow(rowIndex).GetCell(0).SetCellValue(table.Rows[i]["ManHoursDate"].ToString()); reportModel.GetRow(rowIndex).GetCell(0).CellStyle.SetFont(cs_content_Font);//将字体绑定到样式 //WorkCenter if (reportModel.GetRow(rowIndex).GetCell(1) == null) reportModel.GetRow(rowIndex).CreateCell(1); reportModel.GetRow(rowIndex).GetCell(1).SetCellValue(table.Rows[i]["WorkCenter"].ToString()); //Name if (reportModel.GetRow(rowIndex).GetCell(2) == null) reportModel.GetRow(rowIndex).CreateCell(2); reportModel.GetRow(rowIndex).GetCell(2).SetCellValue(table.Rows[i]["UserName"].ToString()); //Target Hours if (reportModel.GetRow(rowIndex).GetCell(3) == null) reportModel.GetRow(rowIndex).CreateCell(3); reportModel.GetRow(rowIndex).GetCell(3).SetCellValue(table.Rows[i]["TargetHours"] != null ? table.Rows[i]["TargetHours"].ToString() : ""); //Grand Hours if (reportModel.GetRow(rowIndex).GetCell(4) == null) reportModel.GetRow(rowIndex).CreateCell(4); reportModel.GetRow(rowIndex).GetCell(4).SetCellValue(Convert.ToDouble(table.Rows[i]["GrandHours"])); //Normal Work Hours if (reportModel.GetRow(rowIndex).GetCell(5) == null) reportModel.GetRow(rowIndex).CreateCell(5); reportModel.GetRow(rowIndex).GetCell(5).SetCellValue(Convert.ToDouble(table.Rows[i]["NormalWorkHours"])); //OverTime Hours if (reportModel.GetRow(rowIndex).GetCell(6) == null) reportModel.GetRow(rowIndex).CreateCell(6); reportModel.GetRow(rowIndex).GetCell(6).SetCellValue(Convert.ToDouble(table.Rows[i]["OverTimeHours"])); //Trip Hours if (reportModel.GetRow(rowIndex).GetCell(7) == null) reportModel.GetRow(rowIndex).CreateCell(7); reportModel.GetRow(rowIndex).GetCell(7).SetCellValue(Convert.ToDouble(table.Rows[i]["TripHours"])); //Training Hours if (reportModel.GetRow(rowIndex).GetCell(8) == null) reportModel.GetRow(rowIndex).CreateCell(8); reportModel.GetRow(rowIndex).GetCell(8).SetCellValue(Convert.ToDouble(table.Rows[i]["TrainingHours"])); //Leave Hours if (reportModel.GetRow(rowIndex).GetCell(9) == null) reportModel.GetRow(rowIndex).CreateCell(9); reportModel.GetRow(rowIndex).GetCell(9).SetCellValue(Convert.ToDouble(table.Rows[i]["LeaveHours"])); //Sick Leave Hours if (reportModel.GetRow(rowIndex).GetCell(10) == null) reportModel.GetRow(rowIndex).CreateCell(10); reportModel.GetRow(rowIndex).GetCell(10).SetCellValue(Convert.ToDouble(table.Rows[i]["SickLeaveHours"])); //Other Hours if (reportModel.GetRow(rowIndex).GetCell(11) == null) reportModel.GetRow(rowIndex).CreateCell(11); reportModel.GetRow(rowIndex).GetCell(11).SetCellValue(Convert.ToDouble(table.Rows[i]["OtherHours"])); #endregion #region 求和 if (!string.IsNullOrEmpty(table.Rows[i]["TargetHours"].ToString())) { targetHours += Convert.ToDouble(table.Rows[i]["TargetHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["GrandHours"].ToString())) { grandHours += Convert.ToDouble(table.Rows[i]["GrandHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["NormalWorkHours"].ToString())) { normalWorkHours += Convert.ToDouble(table.Rows[i]["NormalWorkHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["OverTimeHours"].ToString())) { overTimeHours += Convert.ToDouble(table.Rows[i]["OverTimeHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["TripHours"].ToString())) { tripHours += Convert.ToDouble(table.Rows[i]["TripHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["TrainingHours"].ToString())) { trainingHours += Convert.ToDouble(table.Rows[i]["TrainingHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["LeaveHours"].ToString())) { leaveHours += Convert.ToDouble(table.Rows[i]["LeaveHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["SickLeaveHours"].ToString())) { sickLeaveHours += Convert.ToDouble(table.Rows[i]["SickLeaveHours"].ToString()); } if (!string.IsNullOrEmpty(table.Rows[i]["OtherHours"].ToString())) { otherHours += Convert.ToDouble(table.Rows[i]["OtherHours"].ToString()); } #endregion rowIndex++; } #region 求和赋值 if (reportModel.GetRow(rowIndex) == null) reportModel.CreateRow(rowIndex); if (reportModel.GetRow(rowIndex).GetCell(3) == null) reportModel.GetRow(rowIndex).CreateCell(3); reportModel.GetRow(rowIndex).GetCell(3).SetCellValue(targetHours); if (reportModel.GetRow(rowIndex).GetCell(4) == null) reportModel.GetRow(rowIndex).CreateCell(4); reportModel.GetRow(rowIndex).GetCell(4).SetCellValue(grandHours); if (reportModel.GetRow(rowIndex).GetCell(5) == null) reportModel.GetRow(rowIndex).CreateCell(5); reportModel.GetRow(rowIndex).GetCell(5).SetCellValue(normalWorkHours); if (reportModel.GetRow(rowIndex).GetCell(6) == null) reportModel.GetRow(rowIndex).CreateCell(6); reportModel.GetRow(rowIndex).GetCell(6).SetCellValue(overTimeHours); if (reportModel.GetRow(rowIndex).GetCell(7) == null) reportModel.GetRow(rowIndex).CreateCell(7); reportModel.GetRow(rowIndex).GetCell(7).SetCellValue(tripHours); if (reportModel.GetRow(rowIndex).GetCell(8) == null) reportModel.GetRow(rowIndex).CreateCell(8); reportModel.GetRow(rowIndex).GetCell(8).SetCellValue(trainingHours); if (reportModel.GetRow(rowIndex).GetCell(9) == null) reportModel.GetRow(rowIndex).CreateCell(9); reportModel.GetRow(rowIndex).GetCell(9).SetCellValue(leaveHours); if (reportModel.GetRow(rowIndex).GetCell(10) == null) reportModel.GetRow(rowIndex).CreateCell(10); reportModel.GetRow(rowIndex).GetCell(10).SetCellValue(sickLeaveHours); if (reportModel.GetRow(rowIndex).GetCell(11) == null) reportModel.GetRow(rowIndex).CreateCell(11); reportModel.GetRow(rowIndex).GetCell(11).SetCellValue(otherHours); #endregion } #endregion reportModel.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=P12_Time_Sheet_Summary_" + 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.TimeSheetSummaryMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnOut)) { this.btnExport.Hidden = false; } } } #endregion } }