using BLL; using Model; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; namespace FineUIPro.Web.HJGL.WeldingReport { public partial class NDTWeekReport : PageBase { #region 定义变量 /// /// 主键 /// public string NDTReportId { get { return (string)ViewState["NDTReportId"]; } set { ViewState["NDTReportId"] = value; } } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private void BindGrid() { List list = new List(); List NDTReportLists = BLL.NDTReportService.GetNDTReportByProjectId(this.CurrUser.LoginProjectId, "1"); foreach (var item in NDTReportLists) { SpRpNDTWeekReport t = new SpRpNDTWeekReport(); t.NDTReportId = item.NDTReportId; t.ProjectId = item.ProjectId; t.ProjectName = BLL.ProjectService.GetProjectNameByProjectId(item.ProjectId); t.StaDate = string.Format("{0:yyyy-MM-dd}", item.StartDate) + "至" + string.Format("{0:yyyy-MM-dd}", item.EndDate); list.Add(t); } this.Grid1.DataSource = list; this.Grid1.DataBind(); } #endregion #region 查询 /// /// 查询 /// /// /// protected void btnSearch_Click(object sender, EventArgs e) { string projectName = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId).ProjectName; if (!string.IsNullOrEmpty(this.txtStartDate.Text) && !string.IsNullOrEmpty(this.txtEndDate.Text)) { string staDate = this.txtStartDate.Text + "至" + this.txtEndDate.Text; SpRpNDTWeekReport t = new SpRpNDTWeekReport(); this.NDTReportId = SQLHelper.GetNewID(typeof(Model.NDTReport)); t.NDTReportId = this.NDTReportId; t.ProjectId = this.CurrUser.LoginProjectId; t.ProjectName = projectName; t.StaDate = staDate; List list = new List(); list.Add(t); Grid1.DataSource = list; Grid1.DataBind(); } else { Alert.ShowInTop("日期不能为空!", MessageBoxIcon.Warning); return; } } #endregion #region 保存 /// /// 保存 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.txtStartDate.Text) && !string.IsNullOrEmpty(this.txtEndDate.Text)) { Model.NDTReport ndtReport = new Model.NDTReport(); ndtReport.NDTReportId = this.NDTReportId; ndtReport.ProjectId = this.CurrUser.LoginProjectId; ndtReport.StartDate = Funs.GetNewDateTime(this.txtStartDate.Text.Trim()); ndtReport.EndDate = Funs.GetNewDateTime(this.txtEndDate.Text.Trim()); ndtReport.RType = "1"; BLL.NDTReportService.AddNDTReort(ndtReport); ShowNotify("保存成功!", MessageBoxIcon.Success); BindGrid(); this.txtStartDate.Text = string.Empty; this.txtEndDate.Text = string.Empty; } else { Alert.ShowInTop("日期不能为空!", MessageBoxIcon.Warning); return; } } #endregion #region 右键删除 /// /// 右键删除 /// /// /// protected void btnMenuDelete_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); var rep = BLL.NDTReportService.GetNDTReportById(rowID); if (rep != null) { BLL.NDTReportService.DeleteNDTReportById(rowID); } } ShowNotify("删除成功!", MessageBoxIcon.Success); BindGrid(); } } #endregion #region 日期选择事件 /// /// 开始日期选择事件 /// /// /// protected void txtStartDate_TextChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim())) { this.txtEndDate.Text = Funs.GetNewDateTime(this.txtStartDate.Text.Trim()).Value.AddDays(6).ToShortDateString(); } } /// /// 结束日期选择事件 /// /// /// protected void txtEndDate_TextChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.txtEndDate.Text.Trim())) { this.txtStartDate.Text = Funs.GetNewDateTime(this.txtEndDate.Text.Trim()).Value.AddDays(-6).ToShortDateString(); } } #endregion protected void Grid1_RowCommand(object sender, GridCommandEventArgs e) { object[] keys = Grid1.DataKeys[e.RowIndex]; string fileId = string.Empty; if (keys == null) { return; } else { fileId = keys[0].ToString(); } if (e.CommandName == "print") { string dateStr = Grid1.Rows[e.RowIndex].Values[2].ToString(); var dates = dateStr.Split('至'); DateTime begin = DateTime.Parse(dates[0]); DateTime end = DateTime.Parse(dates[1]); var projectName = ProjectService.GetProjectNameByProjectId(CurrUser.LoginProjectId); string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = Server.MapPath("~/") + "File/Excel/HJGL_DataOut/无损检测周报.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); ICellStyle styleCenter = hssfworkbook.CreateCellStyle(); styleCenter.VerticalAlignment = VerticalAlignment.Center; styleCenter.Alignment = HorizontalAlignment.Center; styleCenter.BorderLeft = BorderStyle.Thin; styleCenter.BorderTop = BorderStyle.Thin; styleCenter.BorderRight = BorderStyle.Thin; styleCenter.BorderBottom = BorderStyle.Thin; styleCenter.WrapText = true; IFont font = styleCenter.GetFont(hssfworkbook); font.FontHeightInPoints = 10;//字体高度(与excel中的字号一致) styleCenter.SetFont(font); XSSFSheet recordSheet = (XSSFSheet)hssfworkbook.GetSheet("无损检测周报"); // recordSheet.AddMergedRegion(new CellRangeAddress(0, 0, 8, 9)); recordSheet.GetRow(2).GetCell(2).SetCellValue(projectName.ToString()); //recordSheet.GetRow(2).GetCell(2).CellStyle = styleCenter; recordSheet.GetRow(3).GetCell(2).SetCellValue(dateStr); //recordSheet.GetRow(2).GetCell(16).CellStyle = styleCenter; List units = UnitService.GetUnitByProjectIdUnitTypeList(this.CurrUser.LoginProjectId, Const.ProjectUnitType_5); string unitName = ""; foreach (Base_Unit unit in units) { unitName += unit.UnitName + ","; } recordSheet.GetRow(4).GetCell(2).SetCellValue(unitName.TrimEnd(',')); //recordSheet.GetRow(3).GetCell(2).CellStyle = styleCenter; string strSql1 = @"select unit.UnitId,unit.UnitName,workArea.WorkAreaName,workArea.WorkAreaCode,curr.curr_pass_film,curr.curr_total_film,recurr.recurr_pass_film,recurr.recurr_total_film from Base_Unit unit right join ProjectData_WorkArea workArea on workArea.UnitId=unit.UnitId left join ( SELECT WorkAreaId,IsoInfo.UnitId,COUNT(*) AS curr_check_count_total , SUM(cht_totalfilm) AS curr_total_film , SUM(cht_passfilm) AS curr_pass_film FROM CH_Check Checks LEFT JOIN CH_CheckItem ON ch_checkitem.CHT_CheckID = Checks.CHT_CheckID LEFT JOIN PW_JointInfo as JointInfo ON JointInfo.JOT_ID = ch_checkitem.JOT_ID LEFT JOIN PW_IsoInfo as IsoInfo ON IsoInfo.ISO_ID = JointInfo.ISO_ID where JointInfo.ProjectId=@ProjectId and (cht_checkdate >= @date1 OR @date1 IS NULL) and(cht_checkdate <= @date2 OR @date2 IS NULL) group by WorkAreaId,IsoInfo.UnitId) as curr on unit.UnitId= curr.UnitId and workArea.WorkAreaId=curr.WorkAreaId left join ( SELECT WorkAreaId,IsoInfo.UnitId,COUNT(*) AS recurr_check_count_total , SUM(cht_totalfilm) AS recurr_total_film , SUM(cht_passfilm) AS recurr_pass_film FROM CH_Check Checks LEFT JOIN CH_CheckItem ON ch_checkitem.CHT_CheckID = Checks.CHT_CheckID LEFT JOIN PW_JointInfo as JointInfo ON JointInfo.JOT_ID = ch_checkitem.JOT_ID LEFT JOIN PW_IsoInfo as IsoInfo ON IsoInfo.ISO_ID = JointInfo.ISO_ID LEFT JOIN CH_Trust trust on trust.CH_TrustID = Checks.CH_TrustID where JointInfo.ProjectId=@ProjectId and trust.CH_TrustType='2' and (cht_checkdate >= @date1 OR @date1 IS NULL) and(cht_checkdate <= @date2 OR @date2 IS NULL) group by WorkAreaId,IsoInfo.UnitId) as recurr on unit.UnitId= recurr.UnitId and workArea.WorkAreaId=recurr.WorkAreaId where workArea.ProjectId=@ProjectId "; List listpar = new List { new SqlParameter("@projectId", this.CurrUser.LoginProjectId) }; listpar.Add(new SqlParameter("@date1", begin.ToString("yyyy-MM-dd"))); listpar.Add(new SqlParameter("@date2", end.ToString("yyyy-MM-dd"))); SqlParameter[] parameter5 = listpar.ToArray(); DataTable dt = SQLHelper.GetDataTableRunText(strSql1, parameter5); if (dt != null) { if (dt.Rows.Count > 16) { recordSheet.ShiftRows(8, 25, dt.Rows.Count - 16); for (int j = 0; j < dt.Rows.Count - 16; j++) { recordSheet.CopyRow(7 + j, 8 + j); } for (int ii = 1; ii <= dt.Rows.Count; ii++) { CellRangeAddress region = new CellRangeAddress(6 + ii, 6 + ii, 1, 2); recordSheet.AddMergedRegion(region); } } else { for (int ii = 1; ii <= 16; ii++) { CellRangeAddress region = new CellRangeAddress(6 + ii, 6 + ii, 1, 2); recordSheet.AddMergedRegion(region); } } int i = 1; int curr_pass_film = 0; int curr_total_film = 0; int recurr_pass_film = 0; int recurr_total_film = 0; foreach (DataRow row in dt.Rows) { recordSheet.GetRow(6 + i).Height = 29 * 20; recordSheet.GetRow(6 + i).CreateCell(0).SetCellValue(i + ""); recordSheet.GetRow(6 + i).GetCell(0).CellStyle = styleCenter; recordSheet.GetRow(6 + i).CreateCell(1).SetCellValue(row["UnitName"].ToString()); recordSheet.GetRow(6 + i).GetCell(1).CellStyle = styleCenter; recordSheet.GetRow(6 + i).CreateCell(3).SetCellValue(row["WorkAreaCode"].ToString()); recordSheet.GetRow(6 + i).GetCell(3).CellStyle = styleCenter; recordSheet.GetRow(6 + i).CreateCell(4).SetCellValue(row["WorkAreaName"].ToString()); recordSheet.GetRow(6 + i).GetCell(4).CellStyle = styleCenter; recordSheet.GetRow(6 + i).CreateCell(5).SetCellValue(row["curr_pass_film"].ToString()); recordSheet.GetRow(6 + i).GetCell(5).CellStyle = styleCenter; recordSheet.GetRow(6 + i).CreateCell(6).SetCellValue(row["curr_total_film"].ToString()); recordSheet.GetRow(6 + i).GetCell(6).CellStyle = styleCenter; try { curr_pass_film += Funs.GetNewIntOrZero(row["curr_pass_film"].ToString()); curr_total_film += Funs.GetNewIntOrZero(row["curr_total_film"].ToString()); var celCellValue1 = recordSheet.GetRow(6 + i).CreateCell(7); celCellValue1.CellStyle = styleCenter; celCellValue1.SetCellValue((100.0 * int.Parse(row["curr_pass_film"].ToString()) / int.Parse(row["curr_total_film"].ToString())).ToString("0.00")); } catch (Exception e1) { } recordSheet.GetRow(6 + i).CreateCell(14).SetCellValue(row["recurr_pass_film"].ToString()); recordSheet.GetRow(6 + i).GetCell(14).CellStyle = styleCenter; recordSheet.GetRow(6 + i).CreateCell(15).SetCellValue(row["recurr_total_film"].ToString()); recordSheet.GetRow(6 + i).GetCell(15).CellStyle = styleCenter; try { recurr_pass_film += Funs.GetNewIntOrZero(row["recurr_pass_film"].ToString()); recurr_total_film += Funs.GetNewIntOrZero(row["recurr_total_film"].ToString()); var celCellValue2 = recordSheet.GetRow(6 + i).CreateCell(16); celCellValue2.CellStyle = styleCenter; celCellValue2.SetCellValue((100.0 * int.Parse(row["recurr_pass_film"].ToString()) / int.Parse(row["recurr_total_film"].ToString())).ToString("0.00")); } catch (Exception e2) { } i++; if (dt.Rows.Count > 16) { recordSheet.GetRow(dt.Rows.Count + 7).CreateCell(5).SetCellValue(curr_pass_film + ""); recordSheet.GetRow(dt.Rows.Count + 7).GetCell(5).CellStyle = styleCenter; recordSheet.GetRow(dt.Rows.Count + 7).CreateCell(6).SetCellValue(curr_total_film + ""); recordSheet.GetRow(dt.Rows.Count + 7).GetCell(6).CellStyle = styleCenter; try { if (curr_total_film > 0) { var celCellValue1 = recordSheet.GetRow(dt.Rows.Count + 7).CreateCell(7); celCellValue1.CellStyle = styleCenter; celCellValue1.SetCellValue((100.0 * curr_pass_film / curr_total_film).ToString("0.00")); } } catch (Exception e1) { } recordSheet.GetRow(dt.Rows.Count + 7).CreateCell(14).SetCellValue(recurr_pass_film + ""); recordSheet.GetRow(dt.Rows.Count + 7).GetCell(14).CellStyle = styleCenter; recordSheet.GetRow(dt.Rows.Count + 7).CreateCell(15).SetCellValue(recurr_total_film + ""); recordSheet.GetRow(dt.Rows.Count + 7).GetCell(15).CellStyle = styleCenter; try { if (recurr_total_film > 0) { var celCellValue2 = recordSheet.GetRow(dt.Rows.Count + 7).CreateCell(16); celCellValue2.CellStyle = styleCenter; celCellValue2.SetCellValue((100.0 * recurr_pass_film / recurr_total_film).ToString("0.00")); } } catch (Exception e2) { } } else { recordSheet.GetRow(23).CreateCell(5).SetCellValue(curr_pass_film + ""); recordSheet.GetRow(23).GetCell(5).CellStyle = styleCenter; recordSheet.GetRow(23).CreateCell(6).SetCellValue(curr_total_film + ""); recordSheet.GetRow(23).GetCell(6).CellStyle = styleCenter; try { if (curr_total_film > 0) { var celCellValue1 = recordSheet.GetRow(23).CreateCell(7); celCellValue1.CellStyle = styleCenter; celCellValue1.SetCellValue((100.0 * curr_pass_film / curr_total_film).ToString("0.00")); } } catch (Exception e3) { } recordSheet.GetRow(23).CreateCell(14).SetCellValue(recurr_pass_film + ""); recordSheet.GetRow(23).GetCell(14).CellStyle = styleCenter; recordSheet.GetRow(23).CreateCell(15).SetCellValue(recurr_total_film + ""); recordSheet.GetRow(23).GetCell(15).CellStyle = styleCenter; try { if (recurr_total_film > 0) { var celCellValue2 = recordSheet.GetRow(23).CreateCell(16); celCellValue2.CellStyle = styleCenter; celCellValue2.SetCellValue((100.0 * recurr_pass_film / recurr_total_film).ToString("0.00")); } } catch (Exception e4) { } } } } using (FileStream filess = File.OpenWrite(ReportFileName)) { hssfworkbook.Write(filess); } //PageContext.RegisterStartupScript(Window5.GetShowReference(String.Format("../../ReportPrint/ExReportPrint.aspx?ispop=1&reportId={0}&replaceParameter={1}&varValue={2}&projectId={3}", BLL.Const.HJGL_JointInfoReportId, isoId, varValue, this.CurrUser.LoginProjectId))); FileInfo filet = new FileInfo(ReportFileName); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=无损检测周报_" + 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(); } } } }