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 ProjectContentStatistics : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.txtMonths.Text = string.Format("{0:yyyy-MM}", DateTime.Now); 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; } GetButtonPower(); BindGrid(); } } /// /// 数据绑定 /// public void BindGrid() { string strSql = @"select * from View_QuantityManagement_ProjectContentStatistics C where C.ProjectId = @ProjectId"; List listStr = new List(); listStr.Add(new SqlParameter("@ProjectId", this.drpProject.SelectedValue)); if (!string.IsNullOrEmpty(this.txtWorkTeam.Text.Trim())) { strSql += " AND WorkTeam like @WorkTeam"; listStr.Add(new SqlParameter("@WorkTeam", "%" + this.txtWorkTeam.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(); DateTime now = Convert.ToDateTime(txtMonths.Text + "-01"); int year = now.Year; int month = now.Month; int day = DateTime.Now.Day; 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; } var totalDayInputs = from x in Funs.DB.View_QuantityManagement_DayInput2 where x.ProjectId == this.drpProject.SelectedValue && x.Date <= endDate orderby x.Date select x; var dayInputs = from x in Funs.DB.View_QuantityManagement_DayInput2 where x.ProjectId == this.drpProject.SelectedValue && x.Date >= startDate && x.Date <= endDate orderby x.Date select x; var bases = from x in Funs.DB.View_QuantityManagement_Base where x.ProjectId == this.drpProject.SelectedValue select x; do { days.Add(startDate1); startDate1 = startDate1.AddDays(1); } while (startDate1 <= endDate1); for (int i = 0; i < this.Grid1.Rows.Count; i++) { string[] strs = this.Grid1.Rows[i].RowID.Split(','); string projectContent = strs[1]; string workTeam = strs[2]; decimal totalAmount = 0, completedAmount = 0, totalCompletedAmount = 0, monthComplete = 0; totalAmount = bases.Where(x => x.ProjectContent == projectContent).ToList().Sum(x => x.Amount ?? 0); foreach (var d in days) { monthComplete = dayInputs.Where(x => x.ProjectContent == projectContent && x.WorkTeam == workTeam).ToList().Sum(x => x.DayAmount ?? 0); } completedAmount = totalDayInputs.Where(x => x.ProjectContent == projectContent && x.WorkTeam == workTeam).ToList().Sum(x => x.DayAmount ?? 0); totalCompletedAmount = totalDayInputs.Where(x => x.ProjectContent == projectContent).ToList().Sum(x => x.DayAmount ?? 0); this.Grid1.Rows[i].Values[3] = totalAmount.ToString("0.##"); this.Grid1.Rows[i].Values[5] = completedAmount.ToString("0.##"); this.Grid1.Rows[i].Values[6] = (totalAmount - totalCompletedAmount).ToString("0.##"); this.Grid1.Rows[i].Values[7] = monthComplete.ToString("0.##"); } } #endregion #region 分页、排序 /// /// 排序 /// /// /// 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) { BindGrid(); } #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.ProjectContentStatisticsTemplateUrl; 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 (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; } 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 r = 1; NPOI.SS.UserModel.IRow row; NPOI.SS.UserModel.ICell cell; 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()); } // 第三步:写入文件流 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 } }