using BLL; using NPOI.SS.Util; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Web.UI.WebControls; namespace FineUIPro.Web.CQMS.QuantityManagement { public partial class DayInputStatistics : PageBase { /// /// 加载表头 /// protected void Page_Init(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.Params["Date"])) { this.txtMonths.Text = string.Format("{0:yyyy-MM}", DateTime.Now); } else { this.txtMonths.Text = string.Format(Request.Params["Date"]); } InitGrid(); } #region 表头 /// /// 表头 /// private void InitGrid() { DateTime startDate, endDate; List days = new List(); DateTime now = Convert.ToDateTime(txtMonths.Text + "-01"); int year = now.Year; int month = now.Month; int day = DateTime.Now.Day; if (now.AddMonths(1) <= DateTime.Now) //选择的是当前月之前的月份 { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = now.AddDays(24); } else //选择的是当前月 { if (now <= DateTime.Now) { if (now.Month == DateTime.Now.Month) { if (day >= 26) { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = now.AddDays(24); } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; } } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; } } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; } } do { days.Add(startDate); startDate = startDate.AddDays(1); } while (startDate <= endDate); GroupField gd = Grid1.Columns[13] as GroupField; for (int i = 0; i < days.Count; i++) { RenderField d = new RenderField(); d.ColumnID = string.Format("{0:MM-dd}", days[i]); d.Width = Unit.Pixel(60); d.DataField = "Day" + (i + 1).ToString(); d.FieldType = FieldType.Double; d.HeaderText = string.Format("{0:MM-dd}", days[i]); d.HeaderTextAlign = TextAlign.Center; d.TextAlign = TextAlign.Center; gd.Columns.Add(d); } } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BLL.DrawingService.InitDrawingChangeDropDownList(drpDrawingNo, this.CurrUser.LoginProjectId, true); //BLL.TeamGroupService.InitTeamGroupProjectDropDownList(drpWorkTeam, this.CurrUser.LoginProjectId, true); if (!string.IsNullOrEmpty(Request.Params["DrawingNo"])) { this.drpDrawingNo.SelectedValue = Request.Params["DrawingNo"]; } if (!string.IsNullOrEmpty(Request.Params["Part"])) { this.txtPart.Text = Request.Params["Part"]; } if (!string.IsNullOrEmpty(Request.Params["ProjectContent"])) { this.txtProjectContent.Text = Request.Params["ProjectContent"]; } ProjectService.InitAllProjectDropDownList(this.drpProject, false); if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { this.drpProject.SelectedValue = this.CurrUser.LoginProjectId; this.drpProject.Enabled = false; this.drpProject.Hidden = true; } if (!string.IsNullOrEmpty(Request.Params["ProjectId"])) { this.drpProject.SelectedValue = Request.Params["ProjectId"]; } GetButtonPower(); BindGrid(); } } /// /// 数据绑定 /// public void BindGrid() { string strSql = @"select newid() as Id,C.* from View_QuantityManagement_DayInput C where C.ProjectId = @ProjectId"; List listStr = new List(); listStr.Add(new SqlParameter("@ProjectId", this.drpProject.SelectedValue)); if (this.drpDrawingNo.SelectedValue != BLL.Const._Null) { strSql += " AND DrawingNo = @DrawingNo"; listStr.Add(new SqlParameter("@DrawingNo", this.drpDrawingNo.SelectedItem.Text)); } if (!string.IsNullOrEmpty(this.txtPart.Text.Trim())) { strSql += " AND Part like @Part"; listStr.Add(new SqlParameter("@Part", "%" + this.txtPart.Text.Trim() + "%")); } if (!string.IsNullOrEmpty(this.txtProjectContent.Text.Trim())) { strSql += " AND ProjectContent like @ProjectContent"; listStr.Add(new SqlParameter("@ProjectContent", "%" + this.txtProjectContent.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(); //绑定数据 DateTime startDate, endDate, startDate1, endDate1; List days = new List(); //int year = DateTime.Now.Year; //int month = DateTime.Now.Month; //int day = DateTime.Now.Day; //if (day >= 26) //{ // startDate = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + "26"); // startDate1 = startDate; // endDate = DateTime.Now; // endDate1 = endDate; //} //else //{ // DateTime lastMonth = DateTime.Now.AddMonths(-1); // startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); // startDate1 = startDate; // endDate = DateTime.Now; // endDate1 = endDate; //} //var dayInputs = from x in Funs.DB.QuantityManagement_DayInput // where x.ProjectId == this.CurrUser.LoginProjectId && x.Date >= startDate && x.Date <= endDate // orderby x.Date // select x; //do //{ // days.Add(startDate1); // startDate1 = startDate1.AddDays(1); //} while (startDate1 <= endDate1); DateTime now = Convert.ToDateTime(txtMonths.Text + "-01"); int year = now.Year; int month = now.Month; int day = DateTime.Now.Day; if (now.AddMonths(1) <= DateTime.Now) //选择的是当前月之前的月份 { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = now.AddDays(24); startDate1 = startDate; endDate1 = endDate; } else //选择的是当前月 { if (now <= DateTime.Now) { if (now.Month == DateTime.Now.Month) { if (day >= 26) { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = now.AddDays(24); startDate1 = startDate; endDate1 = endDate; } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; startDate1 = startDate; endDate1 = endDate; } } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; startDate1 = startDate; endDate1 = endDate; } } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; startDate1 = startDate; endDate1 = endDate; } } do { days.Add(startDate1); startDate1 = startDate1.AddDays(1); } while (startDate1 <= endDate1); var dayInputs = from x in Funs.DB.QuantityManagement_DayInput where x.ProjectId == this.drpProject.SelectedValue && x.Date <= endDate orderby x.Date select x; var bases = from x in Funs.DB.QuantityManagement_Base where x.ProjectId == this.drpProject.SelectedValue select x; for (int i = 0; i < this.Grid1.Rows.Count; i++) { string baseId = this.Grid1.Rows[i].DataKeys[0].ToString(); string workTeamId = this.Grid1.Rows[i].DataKeys[1].ToString(); int j = 0; decimal monthComplete = 0; foreach (var d in days) { var dayInput = dayInputs.FirstOrDefault(x => x.BaseId == baseId && x.Date == d && x.WorkTeam == workTeamId); if (dayInput != null) { this.Grid1.Rows[i].Values[14 + j] = dayInput.DayAmount.Value.ToString("0.##"); monthComplete += dayInput.DayAmount.Value; } j++; } var b = bases.FirstOrDefault(x => x.BaseId == baseId); decimal totalAmount = 0; if (b != null) { totalAmount = b.Amount ?? 0; } decimal completedAmount = dayInputs.Where(x => x.BaseId == baseId && x.WorkTeam == workTeamId).ToList().Sum(x => x.DayAmount ?? 0); decimal totalCompletedAmount = dayInputs.Where(x => x.BaseId == baseId).ToList().Sum(x => x.DayAmount ?? 0); this.Grid1.Rows[i].Values[10] = completedAmount.ToString("0.##"); this.Grid1.Rows[i].Values[11] = (totalAmount - totalCompletedAmount).ToString("0.##"); this.Grid1.Rows[i].Values[12] = monthComplete.ToString("0.##"); } } #endregion #region 分页、排序 /// /// 分页下拉 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } /// /// 分页索引事件 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { BindGrid(); } /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e) { Grid1.SortDirection = e.SortDirection; Grid1.SortField = e.SortField; BindGrid(); } #endregion #region 查询 /// /// 查询 /// /// /// protected void btnSearch_Click(object sender, EventArgs e) { string url = "~/CQMS/QuantityManagement/DayInputStatistics.aspx?Date=" + this.txtMonths.Text.Trim() + "&DrawingId=" + this.drpDrawingNo.SelectedValue + "&Part=" + this.txtPart.Text.Trim() + "&ProjectContent=" + this.txtProjectContent.Text.Trim() + "&ProjectId=" + this.drpProject.SelectedValue; PageContext.Redirect(url, "_self"); } #endregion #region 关闭弹出窗口 /// /// 关闭弹出窗口 /// /// /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } #endregion /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { } #region 获取按钮权限 /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { if (Request.Params["value"] == "0") { return; } var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DayInputMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { } if (buttonList.Contains(BLL.Const.BtnModify)) { this.Grid1.EnableRowDoubleClickEvent = true; } else { this.Grid1.EnableRowDoubleClickEvent = false; } //if (buttonList.Contains(BLL.Const.BtnSave)) //{ // this.btnImport.Hidden = false; //} } } #endregion #region 导出按钮 /// 导出按钮 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string initTemplatePath = string.Empty; string uploadfilepath = string.Empty; string newUrl = string.Empty; string filePath = string.Empty; initTemplatePath = Const.DayInputStatisticsTemplateUrl; uploadfilepath = rootPath + initTemplatePath; if (this.Grid1.Rows.Count > 0) { newUrl = uploadfilepath.Replace(".xlsx", "(" + this.txtMonths.Text.Trim() + ")" + ".xlsx"); File.Copy(uploadfilepath, newUrl); DateTime startDate, endDate; List days = new List(); DateTime now = Convert.ToDateTime(txtMonths.Text + "-01"); int year = now.Year; int month = now.Month; int day = DateTime.Now.Day; if (now.AddMonths(1) <= DateTime.Now) //选择的是当前月之前的月份 { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = now.AddDays(24); } else //选择的是当前月 { if (now <= DateTime.Now) { if (now.Month == DateTime.Now.Month) { if (day >= 26) { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = now.AddDays(24); } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; } } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; } } else { DateTime lastMonth = now.AddMonths(-1); startDate = Convert.ToDateTime(lastMonth.Year.ToString() + "-" + lastMonth.Month.ToString() + "-" + "26"); endDate = DateTime.Now; } } do { days.Add(startDate); startDate = startDate.AddDays(1); } while (startDate <= endDate); // 第一步:读取文件流 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; 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.GetSheetAt(0); var font2 = workbook.CreateFont(); font2.FontHeightInPoints = 10; // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle.WrapText = true; cellStyle.SetFont(font2); // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyleYellow = workbook.CreateCellStyle(); cellStyleYellow.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleYellow.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleYellow.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleYellow.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyleYellow.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyleYellow.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //NPOI.SS.UserModel.IRow row5 = sheet.GetRow(5); //NPOI.SS.UserModel.ICell cell5; //cell5 = row5.GetCell(4); cellStyleYellow.FillBackgroundColor = 120; cellStyleYellow.SetFont(font2); int r0 = 0; int r = 1; NPOI.SS.UserModel.IRow row; NPOI.SS.UserModel.ICell cell; row = sheet.GetRow(r0); for (int i = 0; i < days.Count; i++) { cell = row.CreateCell(13 + i); cell.CellStyle = cellStyle; cell.SetCellValue("完成工程量"); } row = sheet.GetRow(r); for (int i = 0; i < days.Count; i++) { cell = row.CreateCell(13 + i); cell.CellStyle = cellStyle; cell.SetCellValue(string.Format("{0:MM-dd}", days[i])); } sheet.AddMergedRegion(new CellRangeAddress(0, 0, 13, 12 + days.Count)); r++; for (int i = 0; i < Grid1.Rows.Count; i++) { row = sheet.CreateRow(r + i); cell = row.CreateCell(0); cell.CellStyle = cellStyle; cell.SetCellValue((i + 1).ToString()); cell = row.CreateCell(1); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[1].ToString()); cell = row.CreateCell(2); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[2].ToString()); cell = row.CreateCell(3); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[3].ToString()); cell = row.CreateCell(4); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[4].ToString()); cell = row.CreateCell(5); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[5].ToString()); cell = row.CreateCell(6); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[6].ToString()); cell = row.CreateCell(7); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[7].ToString()); cell = row.CreateCell(8); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[8].ToString()); cell = row.CreateCell(9); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[9].ToString()); cell = row.CreateCell(10); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[10].ToString()); cell = row.CreateCell(11); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[11].ToString()); cell = row.CreateCell(12); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[12].ToString()); for (int j = 0; j < days.Count; j++) { cell = row.CreateCell(13 + j); cell.CellStyle = cellStyle; cell.SetCellValue(Grid1.Rows[i].Values[14 + j].ToString()); } } // 第三步:写入文件流 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); } else { Alert.ShowInTop("当期无记录,无法导出!", MessageBoxIcon.Warning); } } #endregion protected void drpDrawingNo_SelectedIndexChanged(object sender, EventArgs e) { Model.QuantityManagement_Drawing drawing = BLL.DrawingService.GetDrawingById(this.drpDrawingNo.SelectedValue); Model.QuantityManagement_Change change = BLL.ChangeService.GetChangeById(this.drpDrawingNo.SelectedValue); if (drawing != null) { //this.txtWorkSection.Text = drawing.WorkSection; //this.txtDrawingName.Text = drawing.DrawingName; //BLL.BaseService.InitPartDropDownList(this.drpPart, drawing.DrawingId, true); //this.drpPart.SelectedValue = BLL.Const._Null; } else if (change != null) { //this.txtWorkSection.Text = change.WorkSection; //this.txtDrawingName.Text = change.ChangeName; //BLL.BaseService.InitPartDropDownList(this.drpPart, change.ChangeId, true); //this.drpPart.SelectedValue = BLL.Const._Null; } else { //this.txtWorkSection.Text = string.Empty; //this.txtDrawingName.Text = string.Empty; //this.drpPart.Items.Clear(); //Funs.FineUIPleaseSelect(this.drpPart); //this.drpPart.SelectedValue = BLL.Const._Null; } //this.drpProjectContent.Items.Clear(); //Funs.FineUIPleaseSelect(this.drpProjectContent); //this.drpProjectContent.SelectedValue = BLL.Const._Null; //this.txtUnit.Text = string.Empty; //this.txtAmount.Text = string.Empty; //this.drpWorkTeam.SelectedValue = BLL.Const._Null; } } }