using BLL; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web.UI.WebControls; namespace FineUIPro.Web.JDGL.Check { public partial class PlanSet : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { UnitWorkService.InitUnitWorkDownList(drpUnitWork, this.CurrUser.LoginProjectId, true); BindGrid(); } } #endregion private void BindGrid() { var table = BLL.WorkPackageService.GetAllTreeDataTable2(this.CurrUser.LoginProjectId, string.Empty, this.txtStartTime.Text.Trim(), this.txtEndTime.Text.Trim(), this.rblIsOK.SelectedValue, this.drpUnitWork.SelectedValue); Grid1.RecordCount = table.Count; var table2 = this.GetPagedDataTable(Grid1, table); Grid1.DataSource = table2; Grid1.DataBind(); for (int i = 0; i < this.Grid1.Rows.Count; i++) { if (string.IsNullOrEmpty(this.Grid1.Rows[i].DataKeys[2].ToString())) { System.Web.UI.WebControls.CheckBox cbIsMileStone = (System.Web.UI.WebControls.CheckBox)(this.Grid1.Rows[i].FindControl("cbIsMileStone")); cbIsMileStone.Visible = false; foreach (GridColumn column in Grid1.Columns) { if (column.ColumnIndex != 0 && column.ColumnIndex != 1) { this.Grid1.Rows[i].CellCssClasses[column.ColumnIndex] = "f-grid-cell-uneditable"; } } } else { System.Web.UI.WebControls.CheckBox cbIsMileStone = (System.Web.UI.WebControls.CheckBox)(this.Grid1.Rows[i].FindControl("cbIsMileStone")); Model.WBS_WorkPackage workPackage = BLL.WorkPackageService.GetWorkPackageByWorkPackageId(this.Grid1.Rows[i].DataKeys[2].ToString()); if (workPackage != null && workPackage.IsMileStone == true) { cbIsMileStone.Checked = true; } } if (!string.IsNullOrEmpty(this.Grid1.Rows[i].Values[11].ToString())) { DateTime planEndDate = Convert.ToDateTime(this.Grid1.Rows[i].Values[11].ToString()); DateTime realEndDate = Funs.GetNewDateTimeOrNow(this.Grid1.Rows[i].Values[13].ToString()); if (planEndDate < realEndDate) { Grid1.Rows[i].RowCssClass = "yellow"; } } } } protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { Save(); BindGrid(); } #region 保存 /// /// 保存按钮 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, BLL.Const.PlanSetMenuId, BLL.Const.BtnSave)) { Save(); BindGrid(); ShowNotify("保存成功!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHideReference()); } else { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); } } private void Save() { Model.SGGLDB db = Funs.DB; var workPackages = from x in db.WBS_WorkPackage where x.ProjectId == this.CurrUser.LoginProjectId select x; var unitWorks = from x in db.WBS_UnitWork where x.ProjectId == this.CurrUser.LoginProjectId select x; string[] ids = this.hdId.Text.Split(','); if (ids.Length > 0) { int i = 0; foreach (JObject mergedRow in Grid1.GetMergedData()) { JObject values = mergedRow.Value("values"); //int i = mergedRow.Value("index"); string name = values.Value("Name"); if (this.Grid1.Rows[i].DataKeys[2] != null && ids.Contains(this.Grid1.Rows[i].DataKeys[2].ToString())) { Model.WBS_WorkPackage workPackage = workPackages.FirstOrDefault(x => x.WorkPackageId == this.Grid1.Rows[i].DataKeys[2].ToString()); if (workPackage != null) { string weights = values.Value("JDWeights"); string unit = values.Value("Unit"); if (unit == "m³") { unit = "m³"; } string planProjectQuantity = values.Value("PlanProjectQuantity"); string drawProjectQuantity = values.Value("DrawProjectQuantity"); string realProjectQuantity = values.Value("RealProjectQuantity"); string planStartDate = values.Value("PlanStartDate"); string planEndDate = values.Value("PlanEndDate"); string realStartDate = values.Value("RealStartDate"); string preWorkCode = values.Value("PreWorkCode"); System.Web.UI.WebControls.CheckBox cbIsMileStone = (System.Web.UI.WebControls.CheckBox)(this.Grid1.Rows[i].FindControl("cbIsMileStone")); workPackage.JDWeights = Funs.GetNewDecimal(weights); workPackage.Unit = unit; workPackage.PlanProjectQuantity = Funs.GetNewDecimal(planProjectQuantity); workPackage.DrawProjectQuantity = Funs.GetNewDecimal(drawProjectQuantity); workPackage.RealProjectQuantity = Funs.GetNewDecimal(realProjectQuantity); workPackage.PlanStartDate = Funs.GetNewDateTime(planStartDate); workPackage.PlanEndDate = Funs.GetNewDateTime(planEndDate); workPackage.RealStartDate = Funs.GetNewDateTime(realStartDate); workPackage.IsMileStone = cbIsMileStone.Checked; workPackage.PreWorkCode = preWorkCode; BLL.WorkPackageService.UpdateWorkPackage(workPackage); if (workPackage.PlanStartDate != null || workPackage.PlanEndDate != null || workPackage.RealStartDate != null) { BLL.WorkPackageService.UpdateWorkPackages(db, workPackage, workPackage.PlanStartDate, workPackage.PlanEndDate, workPackage.RealStartDate, null); var unitWork = unitWorks.FirstOrDefault(x => x.UnitWorkId == workPackage.UnitWorkId); if (workPackage.PlanStartDate != null) { if (unitWork.PlanStartDate == null) { unitWork.PlanStartDate = workPackage.PlanStartDate; } else { if (unitWork.PlanStartDate > workPackage.PlanStartDate) { unitWork.PlanStartDate = workPackage.PlanStartDate; } } } if (workPackage.PlanEndDate != null) { if (unitWork.PlanEndDate == null) { unitWork.PlanEndDate = workPackage.PlanEndDate; } else { if (unitWork.PlanEndDate < workPackage.PlanEndDate) { unitWork.PlanEndDate = workPackage.PlanEndDate; } } } if (workPackage.RealStartDate != null) { if (unitWork.RealStartDate == null) { unitWork.RealStartDate = workPackage.RealStartDate; } else { if (unitWork.RealStartDate > workPackage.RealStartDate) { unitWork.RealStartDate = workPackage.RealStartDate; } } } } db.SubmitChanges(); } } i++; } } } #endregion #region 导出(excel按钮) protected void btnOutExcel_Click(object sender, EventArgs e) { if (this.Grid1.Rows.Count > 0) { try { DataTable table = BLL.WorkPackageService.GetAllTreeDataTable(this.CurrUser.LoginProjectId, "Out", this.txtStartTime.Text.Trim(), this.txtEndTime.Text.Trim(), this.rblIsOK.SelectedValue, BLL.Const._Null); string projectNmae = BLL.ProjectService.GetProjectNameByProjectId(this.CurrUser.LoginProjectId); if (!string.IsNullOrEmpty(projectNmae)) { projectNmae = "(" + projectNmae + ")"; } string rootPath = Server.MapPath("~/"); string initTemplatePath = string.Empty; string uploadfilepath = string.Empty; string newUrl = string.Empty; string filePath = string.Empty; initTemplatePath = Const.JDPlanTemplateUrl; uploadfilepath = rootPath + initTemplatePath; newUrl = uploadfilepath.Replace(".xls", projectNmae + ".xls"); File.Copy(uploadfilepath, newUrl); // 第一步:读取文件流 NPOI.SS.UserModel.IWorkbook workbook; using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(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; var font = workbook.CreateFont(); font.FontHeightInPoints = 12; font.IsBold = true; //font.FontHeightInPoints = (short)8.5;字号为小数时要转为short cellStyle0.SetFont(font); // 第二步:创建新数据行 NPOI.SS.UserModel.ISheet sheet = workbook.GetSheet("Sheet1"); NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(1); NPOI.SS.UserModel.ICell cell2; // 添加测试数据 cell2 = row1.CreateCell(14); cell2.CellStyle = cellStyle0; cell2.SetCellValue("123"); var font2 = workbook.CreateFont(); font2.FontHeightInPoints = 10; // 创建单元格样式 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; var font3 = workbook.CreateFont(); font3.FontHeightInPoints = 14; cellStyle1.SetFont(font3); 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.Left; cellStyle2.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle2.SetFont(font3); int rowCount = 1; NPOI.SS.UserModel.IRow row; NPOI.SS.UserModel.ICell cell; #region 填充内容 foreach (DataRow tr in table.Rows) { row = sheet.CreateRow(rowCount); cell = row.CreateCell(0); cell.CellStyle = cellStyle2; cell.SetCellValue(tr["Code"].ToString()); cell = row.CreateCell(1); cell.CellStyle = cellStyle2; cell.SetCellValue(tr["Name"].ToString()); cell = row.CreateCell(2); cell.CellStyle = cellStyle1; cell.SetCellValue(tr["PreWorkCode"].ToString()); cell = row.CreateCell(3); cell.CellStyle = cellStyle1; if (!string.IsNullOrEmpty(tr["IsMileStone"].ToString()) && Convert.ToBoolean(tr["IsMileStone"].ToString()) == true) { cell.SetCellValue("是"); } cell = row.CreateCell(4); cell.CellStyle = cellStyle1; if (!string.IsNullOrEmpty(tr["JDWeights"].ToString())) { cell.SetCellValue(decimal.Round(Convert.ToDecimal(tr["JDWeights"]), 2).ToString("0.##")); } cell = row.CreateCell(5); cell.CellStyle = cellStyle1; cell.SetCellValue(tr["Unit"].ToString()); cell = row.CreateCell(6); cell.CellStyle = cellStyle1; if (!string.IsNullOrEmpty(tr["PlanProjectQuantity"].ToString())) { cell.SetCellValue(decimal.Round(Convert.ToDecimal(tr["PlanProjectQuantity"]), 2).ToString("0.##")); } cell = row.CreateCell(7); cell.CellStyle = cellStyle1; if (!string.IsNullOrEmpty(tr["PlanCost"].ToString())) { cell.SetCellValue(decimal.Round(Convert.ToDecimal(tr["PlanCost"]), 2).ToString("0.##")); } cell = row.CreateCell(8); cell.CellStyle = cellStyle1; if (!string.IsNullOrEmpty(tr["RealProjectQuantity"].ToString())) { cell.SetCellValue(decimal.Round(Convert.ToDecimal(tr["RealProjectQuantity"]), 2).ToString("0.##")); } cell = row.CreateCell(9); cell.CellStyle = cellStyle1; if (tr["PlanStartDate"] != null) { cell.SetCellValue(string.Format("{0:yyyy-MM-dd}", tr["PlanStartDate"])); } cell = row.CreateCell(10); cell.CellStyle = cellStyle1; if (tr["PlanEndDate"] != null) { cell.SetCellValue(string.Format("{0:yyyy-MM-dd}", tr["PlanEndDate"])); } cell = row.CreateCell(11); cell.CellStyle = cellStyle1; if (tr["RealStartDate"] != null) { cell.SetCellValue(string.Format("{0:yyyy-MM-dd}", tr["RealStartDate"])); } cell = row.CreateCell(12); cell.CellStyle = cellStyle1; if (tr["RealEndDate"] != null) { cell.SetCellValue(string.Format("{0:yyyy-MM-dd}", tr["RealEndDate"])); } rowCount++; } #endregion // 第三步:写入文件流 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); } catch (Exception ex) { throw ex; } } else { ShowNotify("没有数据,无法导出!", MessageBoxIcon.Warning); } } #endregion protected void btnQuery_Click(object sender, EventArgs e) { BindGrid(); } } }