using BLL; using Newtonsoft.Json.Linq; using NPOI.SS.Util; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web.UI.WebControls; namespace FineUIPro.Web.PZHGL.InformationProject { public partial class MonthConstructionLogWorkEfficiencySum : PageBase { /// /// 加载表头 /// protected void Page_Init(object sender, EventArgs e) { InitGrid(); } #region 表头 /// /// 表头 /// private void InitGrid() { Model.SGGLDB db = Funs.DB; var workEfficiencys = from x in db.ZHGL_ConstructionLogWorkEfficiency join y in db.ZHGL_ConstructionLog on x.ConstructionLogId equals y.ConstructionLogId where y.ProjectId == this.CurrUser.LoginProjectId select x; List workPostIds = (from x in workEfficiencys where x.Type == "Person" select x.TypeId).Distinct().ToList(); List machineIds = (from x in workEfficiencys where x.Type == "Machine" select x.TypeId).Distinct().ToList(); GroupField gf1 = new GroupField(); gf1.ColumnID = "Person"; gf1.HeaderText = "人工消耗(工·h)"; gf1.HeaderTextAlign = TextAlign.Center; for (int i = 0; i < workPostIds.Count; i++) { GroupField gf = new GroupField(); gf.ColumnID = "Person" + i.ToString(); gf.HeaderText = BLL.WorkPostService.getWorkPostNameById(workPostIds[i]); gf.HeaderTextAlign = TextAlign.Center; RenderField rd1 = new RenderField(); rd1.ColumnID = "PersonConsumeHours" + i.ToString(); rd1.Width = Unit.Pixel(100); rd1.DataField = "PersonConsumeHours" + i.ToString(); rd1.FieldType = FieldType.Double; rd1.HeaderText = "消耗工时"; rd1.HeaderTextAlign = TextAlign.Center; gf.Columns.Add(rd1); RenderField rd2 = new RenderField(); rd2.ColumnID = "PersonWorkEfficiency" + i.ToString(); rd2.Width = Unit.Pixel(100); rd2.DataField = "PersonWorkEfficiency" + i.ToString(); rd2.FieldType = FieldType.Double; rd2.HeaderText = "工效"; rd2.HeaderTextAlign = TextAlign.Center; gf.Columns.Add(rd2); gf1.Columns.Add(gf); } Grid1.Columns.Add(gf1); GroupField gf2 = new GroupField(); gf2.ColumnID = "Machine"; gf2.HeaderText = "机械消耗(台·h)"; gf2.HeaderTextAlign = TextAlign.Center; for (int i = 0; i < machineIds.Count; i++) { GroupField gf = new GroupField(); gf.ColumnID = "Machine" + i.ToString(); gf.HeaderText = BLL.SpecialEquipmentService.GetSpecialEquipmentNameById(machineIds[i]); gf.HeaderTextAlign = TextAlign.Center; RenderField rd1 = new RenderField(); rd1.ColumnID = "MachineConsumeHours" + i.ToString(); rd1.Width = Unit.Pixel(100); rd1.DataField = "MachineConsumeHours" + i.ToString(); rd1.FieldType = FieldType.Double; rd1.HeaderText = "消耗台时"; rd1.HeaderTextAlign = TextAlign.Center; gf.Columns.Add(rd1); RenderField rd2 = new RenderField(); rd2.ColumnID = "MachineWorkEfficiency" + i.ToString(); rd2.Width = Unit.Pixel(100); rd2.DataField = "MachineWorkEfficiency" + i.ToString(); rd2.FieldType = FieldType.Double; rd2.HeaderText = "工效"; rd2.HeaderTextAlign = TextAlign.Center; gf.Columns.Add(rd2); gf2.Columns.Add(gf); } Grid1.Columns.Add(gf2); } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.txtMonth.Text = string.Format("{0:yyyy-MM}", DateTime.Now); //合同编号 this.drpContractNo.DataTextField = "ContractNum"; this.drpContractNo.DataValueField = "ContractId"; this.drpContractNo.DataSource = BLL.PHTGL_ContractReviewService.GetContractReview_CompleteData(this.CurrUser.LoginProjectId); this.drpContractNo.DataBind(); Funs.FineUIPleaseSelect(this.drpContractNo); } } #endregion protected void drpContractNo_SelectedIndexChanged(object sender, EventArgs e) { this.lblUnitName.Text = string.Empty; this.btnSave.Hidden = false; if (this.drpContractNo.SelectedValue != BLL.Const._Null) { var contractTrack = (from x in Funs.DB.PHTGL_ContractTrack where x.ContractId == this.drpContractNo.SelectedValue && x.ConstructionSubcontractor != null && x.ConstructionSubcontractor != string.Empty select x).FirstOrDefault(); if (contractTrack != null) { this.lblUnitName.Text = contractTrack.ConstructionSubcontractor; } this.btnSave.Hidden = true; } this.Grid1.DataSource = null; this.Grid1.DataBind(); } private void GetData() { Model.SGGLDB db = Funs.DB; DateTime startDate = Funs.GetNewDateTimeOrNow(this.txtMonth.Text.Trim() + "-01"); DateTime endDate = startDate.AddMonths(1); string contractNo = string.Empty, professional = string.Empty; var personLogs = from x in db.ZHGL_ConstructionLog where x.ProjectId == this.CurrUser.LoginProjectId && x.CompileDate >= startDate && x.CompileDate < endDate orderby x.CompileDate descending select x; if (personLogs.Count() > 0) { foreach (var personLog in personLogs) { if (!string.IsNullOrEmpty(personLog.ContractNo)) { contractNo += "," + personLog.ContractNo; } if (!string.IsNullOrEmpty(personLog.Professional)) { professional += "," + personLog.Professional; } } } if (this.drpContractNo.SelectedValue != BLL.Const._Null) { contractNo = this.drpContractNo.SelectedValue; } List contractNoList = Funs.GetStrListByStr(contractNo, ','); List professionalList = Funs.GetStrListByStr(professional, ','); var workEfficiencys = from x in db.ZHGL_ConstructionLogWorkEfficiency join y in db.ZHGL_ConstructionLog on x.ConstructionLogId equals y.ConstructionLogId where y.ProjectId == this.CurrUser.LoginProjectId select x; List workPostIds = (from x in workEfficiencys where x.Type == "Person" select x.TypeId).Distinct().ToList(); List machineIds = (from x in workEfficiencys where x.Type == "Machine" select x.TypeId).Distinct().ToList(); DataTable table = BLL.PhtglContractTrackService.GetMonthProjectAllTreeDataTable(this.CurrUser.LoginProjectId, string.Empty, contractNoList, professionalList, workPostIds, machineIds, startDate, endDate); Grid1.DataSource = table; Grid1.DataBind(); } #region 统计按钮事件 /// /// 统计 /// /// /// protected void BtnAnalyse_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.txtMonth.Text.Trim())) { GetData(); } else { Grid1.DataSource = null; Grid1.DataBind(); Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning); } } #endregion #region 保存 /// /// 保存按钮 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.txtMonth.Text.Trim())) { if (this.Grid1.Rows.Count > 0) { Model.SGGLDB db = Funs.DB; DateTime month = Funs.GetNewDateTimeOrNow(this.txtMonth.Text.Trim() + "-01"); BLL.ConstructionLogWorkEfficiencyMonthService.DeleteConstructionLogWorkEfficiencyMonthsByConstructionLogId(this.CurrUser.LoginProjectId, month); var workEfficiencys = from x in db.ZHGL_ConstructionLogWorkEfficiency join y in db.ZHGL_ConstructionLog on x.ConstructionLogId equals y.ConstructionLogId where y.ProjectId == this.CurrUser.LoginProjectId select x; List workPostIds = (from x in workEfficiencys where x.Type == "Person" select x.TypeId).Distinct().ToList(); List machineIds = (from x in workEfficiencys where x.Type == "Machine" select x.TypeId).Distinct().ToList(); foreach (JObject mergedRow in Grid1.GetMergedData()) { JObject values = mergedRow.Value("values"); int a = mergedRow.Value("index"); if (this.Grid1.Rows[a].DataKeys[2] != null) { string physicalCompletionQuantity = values.Value("PhysicalCompletionQuantity"); string materialConsumption = values.Value("MaterialConsumption"); string unitOfMeasurement = values.Value("UnitOfMeasurement"); for (int i = 0; i < workPostIds.Count; i++) { Model.ZHGL_ConstructionLogWorkEfficiencyMonth workEfficiency = new Model.ZHGL_ConstructionLogWorkEfficiencyMonth(); workEfficiency.ConstructionLogWorkEfficiencyMonthId = SQLHelper.GetNewID(); workEfficiency.ProjectId = this.CurrUser.LoginProjectId; workEfficiency.Month = month; workEfficiency.ContractTrackId = this.Grid1.Rows[a].DataKeys[2].ToString(); workEfficiency.PhysicalCompletionQuantity = Funs.GetNewDecimal(physicalCompletionQuantity); workEfficiency.MaterialConsumption = Funs.GetNewDecimal(materialConsumption); workEfficiency.UnitOfMeasurement = unitOfMeasurement; workEfficiency.Type = "Person"; workEfficiency.TypeId = workPostIds[i]; workEfficiency.ConsumeHours = Funs.GetNewDecimal(values.Value("PersonConsumeHours" + i.ToString())); workEfficiency.WorkEfficiency = Funs.GetNewDecimal(values.Value("PersonWorkEfficiency" + i.ToString())); BLL.ConstructionLogWorkEfficiencyMonthService.AddConstructionLogWorkEfficiencyMonth(workEfficiency); } for (int i = 0; i < machineIds.Count; i++) { Model.ZHGL_ConstructionLogWorkEfficiencyMonth workEfficiency = new Model.ZHGL_ConstructionLogWorkEfficiencyMonth(); workEfficiency.ConstructionLogWorkEfficiencyMonthId = SQLHelper.GetNewID(); workEfficiency.ProjectId = this.CurrUser.LoginProjectId; workEfficiency.Month = month; workEfficiency.ContractTrackId = this.Grid1.Rows[a].DataKeys[2].ToString(); workEfficiency.PhysicalCompletionQuantity = Funs.GetNewDecimal(physicalCompletionQuantity); workEfficiency.MaterialConsumption = Funs.GetNewDecimal(materialConsumption); workEfficiency.UnitOfMeasurement = unitOfMeasurement; workEfficiency.Type = "Machine"; workEfficiency.TypeId = machineIds[i]; workEfficiency.ConsumeHours = Funs.GetNewDecimal(values.Value("MachineConsumeHours" + i.ToString())); workEfficiency.WorkEfficiency = Funs.GetNewDecimal(values.Value("MachineWorkEfficiency" + i.ToString())); BLL.ConstructionLogWorkEfficiencyMonthService.AddConstructionLogWorkEfficiencyMonth(workEfficiency); } } } ShowNotify("保存成功!", MessageBoxIcon.Success); } else { Alert.ShowInTop("请先统计后再保存!", MessageBoxIcon.Warning); } } else { Alert.ShowInTop("请选择月份!", MessageBoxIcon.Warning); } } #endregion protected void txtMonth_TextChanged(object sender, EventArgs e) { Grid1.DataSource = null; Grid1.DataBind(); } #region 导出按钮 /// 导出按钮 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { if (this.Grid1.Rows.Count == 0) { Alert.ShowInTop("无数据可导出!", MessageBoxIcon.Warning); return; } string rootPath = Server.MapPath("~/"); string initTemplatePath = string.Empty; string uploadfilepath = string.Empty; string newUrl = string.Empty; string filePath = string.Empty; initTemplatePath = Const.MonthConstructionLogWorkEfficiencyTemplateUrl; uploadfilepath = rootPath + initTemplatePath; Model.SGGLDB db = Funs.DB; var workEfficiencys = from x in db.ZHGL_ConstructionLogWorkEfficiency join y in db.ZHGL_ConstructionLog on x.ConstructionLogId equals y.ConstructionLogId where y.ProjectId == this.CurrUser.LoginProjectId select x; List workPostIds = (from x in workEfficiencys where x.Type == "Person" select x.TypeId).Distinct().ToList(); List machineIds = (from x in workEfficiencys where x.Type == "Machine" select x.TypeId).Distinct().ToList(); string projectName = BLL.ProjectService.GetProjectNameByProjectId(this.CurrUser.LoginProjectId); string unitName = this.lblUnitName.Text.Trim(); string contractNo = string.Empty; if (this.drpContractNo.SelectedValue != BLL.Const._Null) { contractNo = this.drpContractNo.SelectedItem.Text; } newUrl = uploadfilepath.Replace(".xlsx", "(" + this.txtMonth.Text.Trim() + ")" + ".xlsx"); File.Copy(uploadfilepath, newUrl); // 第一步:读取文件流 NPOI.SS.UserModel.IWorkbook workbook; using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); } // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyle0 = workbook.CreateCellStyle(); cellStyle0.BorderTop = NPOI.SS.UserModel.BorderStyle.None; cellStyle0.BorderRight = NPOI.SS.UserModel.BorderStyle.None; cellStyle0.BorderBottom = NPOI.SS.UserModel.BorderStyle.None; cellStyle0.BorderLeft = NPOI.SS.UserModel.BorderStyle.None; cellStyle0.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle0.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle0.WrapText = true; var font = workbook.CreateFont(); font.FontHeightInPoints = 16; font.IsBold = true; cellStyle0.SetFont(font); NPOI.SS.UserModel.ICellStyle cellStyle1 = workbook.CreateCellStyle(); cellStyle1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle1.WrapText = true; var font1 = workbook.CreateFont(); font1.FontHeightInPoints = 11; font1.IsBold = true; cellStyle1.SetFont(font1); NPOI.SS.UserModel.ICellStyle cellStyle2 = workbook.CreateCellStyle(); cellStyle2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle2.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle2.WrapText = true; var font0 = workbook.CreateFont(); font0.FontHeightInPoints = 11; font0.IsBold = false; cellStyle2.SetFont(font0); NPOI.SS.UserModel.ICellStyle cellStyle3 = workbook.CreateCellStyle(); cellStyle3.BorderTop = NPOI.SS.UserModel.BorderStyle.None; cellStyle3.BorderRight = NPOI.SS.UserModel.BorderStyle.None; cellStyle3.BorderBottom = NPOI.SS.UserModel.BorderStyle.None; cellStyle3.BorderLeft = NPOI.SS.UserModel.BorderStyle.None; cellStyle3.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left; cellStyle3.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //cellStyle3.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; //cellStyle3.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground; cellStyle3.WrapText = true; var font3 = workbook.CreateFont(); font3.FontHeightInPoints = 11; font3.IsBold = true; cellStyle3.SetFont(font3); // 第二步:创建新数据行 NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); NPOI.SS.UserModel.IRow row1 = sheet.GetRow(0); NPOI.SS.UserModel.ICell cell3; cell3 = row1.CreateCell(0); cell3.CellStyle = cellStyle0; cell3.SetCellValue("月工效汇总表"); cell3 = row1.CreateCell(9 + workPostIds.Count * 2 + machineIds.Count * 2); cell3.CellStyle = cellStyle0; sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 9 + workPostIds.Count * 2 + machineIds.Count * 2)); row1 = sheet.GetRow(1); cell3 = row1.CreateCell(1); cell3.CellStyle = cellStyle3; cell3.SetCellValue(projectName); cell3 = row1.CreateCell(12); cell3.CellStyle = cellStyle3; cell3.SetCellValue(unitName); cell3 = row1.CreateCell(19); cell3.CellStyle = cellStyle3; cell3.SetCellValue(contractNo); row1 = sheet.GetRow(2); cell3 = row1.CreateCell(12); cell3.CellStyle = cellStyle3; cell3.SetCellValue(this.txtMonth.Text.Trim()); row1 = sheet.GetRow(3); cell3 = row1.CreateCell(10); cell3.CellStyle = cellStyle1; cell3.SetCellValue("人工消耗(工·h)"); for (int i = 1; i < workPostIds.Count * 2; i++) { cell3 = row1.CreateCell(10 + i); cell3.CellStyle = cellStyle1; } sheet.AddMergedRegion(new CellRangeAddress(3, 3, 10, 9 + workPostIds.Count * 2)); cell3 = row1.CreateCell(10 + workPostIds.Count * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue("机械消耗(台·h)"); for (int i = 1; i < machineIds.Count * 2; i++) { cell3 = row1.CreateCell(10 + workPostIds.Count * 2 + i); cell3.CellStyle = cellStyle1; } sheet.AddMergedRegion(new CellRangeAddress(3, 3, 10 + workPostIds.Count * 2, 9 + workPostIds.Count * 2 + machineIds.Count * 2)); row1 = sheet.GetRow(4); for (int i = 0; i < workPostIds.Count; i++) { cell3 = row1.CreateCell(10 + i * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue(BLL.WorkPostService.getWorkPostNameById(workPostIds[i])); cell3 = row1.CreateCell(11 + i * 2); cell3.CellStyle = cellStyle1; sheet.AddMergedRegion(new CellRangeAddress(4, 4, 10 + i * 2, 11 + i * 2)); } for (int i = 0; i < machineIds.Count; i++) { cell3 = row1.CreateCell(10 + workPostIds.Count * 2 + i * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue(BLL.SpecialEquipmentService.GetSpecialEquipmentNameById(machineIds[i])); cell3 = row1.CreateCell(11 + workPostIds.Count * 2 + i * 2); cell3.CellStyle = cellStyle1; sheet.AddMergedRegion(new CellRangeAddress(4, 4, 10 + workPostIds.Count * 2 + i * 2, 11 + workPostIds.Count * 2 + i * 2)); } row1 = sheet.GetRow(5); for (int i = 0; i < workPostIds.Count; i++) { cell3 = row1.CreateCell(10 + i * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue("消耗工时"); cell3 = row1.CreateCell(11 + i * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue("工效"); } for (int i = 0; i < machineIds.Count; i++) { cell3 = row1.CreateCell(10 + workPostIds.Count * 2 + i * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue("消耗台时"); cell3 = row1.CreateCell(11 + workPostIds.Count * 2 + i * 2); cell3.CellStyle = cellStyle1; cell3.SetCellValue("工效"); } int rowCount = 6; foreach (JObject mergedRow in Grid1.GetMergedData()) { JObject values = mergedRow.Value("values"); int a = mergedRow.Value("index"); string Code = values.Value("Code"); string MainItemCode = values.Value("MainItemCode"); string MainItemName = values.Value("MainItemName"); string MajorCode = values.Value("MajorCode"); string MajorName = values.Value("MajorName"); string ProjectCode = values.Value("ProjectCode"); string ProjectName = values.Value("ProjectName"); string UnitOfMeasurement = values.Value("UnitOfMeasurement"); string PhysicalCompletionQuantity = values.Value("PhysicalCompletionQuantity"); string MaterialConsumption = values.Value("MaterialConsumption"); row1 = sheet.GetRow(rowCount); cell3 = row1.CreateCell(0); cell3.CellStyle = cellStyle2; cell3.SetCellValue(Code); cell3 = row1.CreateCell(1); cell3.CellStyle = cellStyle2; cell3.SetCellValue(MainItemCode); cell3 = row1.CreateCell(2); cell3.CellStyle = cellStyle2; cell3.SetCellValue(MainItemName); cell3 = row1.CreateCell(3); cell3.CellStyle = cellStyle2; cell3.SetCellValue(MajorCode); cell3 = row1.CreateCell(4); cell3.CellStyle = cellStyle2; cell3.SetCellValue(MajorName); cell3 = row1.CreateCell(5); cell3.CellStyle = cellStyle2; cell3.SetCellValue(ProjectCode); cell3 = row1.CreateCell(6); cell3.CellStyle = cellStyle2; cell3.SetCellValue(ProjectName); cell3 = row1.CreateCell(7); cell3.CellStyle = cellStyle2; cell3.SetCellValue(UnitOfMeasurement); cell3 = row1.CreateCell(8); cell3.CellStyle = cellStyle2; cell3.SetCellValue(PhysicalCompletionQuantity); cell3 = row1.CreateCell(9); cell3.CellStyle = cellStyle2; cell3.SetCellValue(MaterialConsumption); for (int i = 0; i < workPostIds.Count; i++) { cell3 = row1.CreateCell(10 + i * 2); cell3.CellStyle = cellStyle2; cell3.SetCellValue(values.Value("PersonConsumeHours" + i.ToString())); cell3 = row1.CreateCell(11 + i * 2); cell3.CellStyle = cellStyle2; cell3.SetCellValue(values.Value("PersonWorkEfficiency" + i.ToString())); } for (int i = 0; i < machineIds.Count; i++) { cell3 = row1.CreateCell(10 + workPostIds.Count * 2 + i * 2); cell3.CellStyle = cellStyle2; cell3.SetCellValue(values.Value("MachineConsumeHours" + i.ToString())); cell3 = row1.CreateCell(11 + workPostIds.Count * 2 + i * 2); cell3.CellStyle = cellStyle2; cell3.SetCellValue(values.Value("MachineWorkEfficiency" + i.ToString())); } rowCount++; } // 第三步:写入文件流 using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) { workbook.Write(stream); workbook.Close(); } string fileName = Path.GetFileName(newUrl); FileInfo info = new FileInfo(newUrl); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(newUrl, 0, fileSize); Response.Flush(); Response.Close(); File.Delete(newUrl); } #endregion } }