using BLL; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using Newtonsoft.Json.Linq; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; using System.Runtime.Serialization; using NPOI.SS.Util; using NPOI.HSSF.UserModel; using System.Net; using System.Drawing; using System.IO; namespace FineUIPro.Web.Customization.CNCCG.HSSE.InApproveManager { public partial class EquipmentHazard : PageBase { #region 项目主键 /// /// 项目主键 /// public string ProjectId { get { return (string)ViewState["ProjectId"]; } set { ViewState["ProjectId"] = value; } } /// /// 特种设备id /// public string EquipmentInItemId { get { return (string)ViewState["EquipmentInItemId"]; } set { ViewState["EquipmentInItemId"] = value; } } #endregion protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.ProjectId = this.CurrUser.LoginProjectId; if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId) { this.ProjectId = Request.Params["projectId"]; } if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId) { this.ProjectId = Request.Params["projectId"]; } if (this.CurrUser != null && this.CurrUser.PageSize.HasValue) { Grid1.PageSize = this.CurrUser.PageSize.Value; } this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); EquipmentInItemId = Request.Params["EquipmentInItemId"]; this.ucTree.UnitId = this.CurrUser.UnitId; this.ucTree.ProjectId = this.ProjectId; if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { this.panelLeftRegion.Hidden = true; // this.Grid1.Columns[0].Hidden = true; } // 绑定表格 this.BindGrid(); } } protected void changeTree(object sender, EventArgs e) { this.ProjectId = this.ucTree.ProjectId; this.BindGrid(); } protected void BindGrid() { if (!string.IsNullOrEmpty(this.ProjectId)) { string strSql = @"select x.State,x.ProjectId,x.Type,x.Special_EquipmentCheckId,y.EquipmentInItemId,y.SizeCode,y.SizeModel,x.CheckDate, case x.Type when '2' then '安装前' when '3' then '安装中' when '4' then '使用中' else '拆除中' end as TypeName,z.UserName as CompileManName from Special_EquipmentCheck x left join InApproveManager_EquipmentInItem y on x.EquipmentInItemId=y.EquipmentInItemId left join Sys_User z on x.CompileMan=z.userid where (Type='2' or Type ='3' or Type ='4 'or Type ='5') and x.projectid = '" + ProjectId + "' "; List listStr = new List(); if (this.rbStates.SelectedValue != "-1") { strSql += " AND Type =@Type"; listStr.Add(new SqlParameter("@Type", this.rbStates.SelectedValue)); } if (!string.IsNullOrEmpty(txtSizeCode.Text)) { strSql += " AND SizeCode LIKE @SizeCode"; listStr.Add(new SqlParameter("@SizeCode", "%" + this.txtSizeCode.Text.Trim() + "%")); } if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim())) { strSql += " AND CheckDate >= @StartDate"; listStr.Add(new SqlParameter("@StartDate", this.txtStartDate.Text.Trim())); } if (!string.IsNullOrEmpty(this.txtEndDate.Text.Trim())) { strSql += " AND CheckDate <= @EndDate"; listStr.Add(new SqlParameter("@EndDate", this.txtEndDate.Text.Trim())); } if (!string.IsNullOrEmpty(EquipmentInItemId)) { strSql += " AND x.EquipmentInItemId =@EquipmentInItemId"; listStr.Add(new SqlParameter("@EquipmentInItemId", EquipmentInItemId)); } 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(); } } /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim()) && !string.IsNullOrEmpty(this.txtEndDate.Text.Trim())) { if (Convert.ToDateTime(this.txtStartDate.Text.Trim()) > Convert.ToDateTime(this.txtEndDate.Text.Trim())) { Alert.ShowInTop("开始时间不能大于结束时间", MessageBoxIcon.Warning); return; } } BindGrid(); } #region 分页 排序 /// /// 改变索引事件 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { BindGrid(); } /// /// 分页下拉选择事件 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { this.Grid1.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue); BindGrid(); } /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e) { this.BindGrid(); } #endregion #region 格式化字符串 /// /// 获取设备名称 /// /// /// protected string ConvertEqiupment(object EquipmentInItemId) { string equipmentName = string.Empty; if (EquipmentInItemId != null) { var equipmentId = Funs.DB.InApproveManager_EquipmentInItem.FirstOrDefault(x => x.EquipmentInItemId == EquipmentInItemId.ToString()).SpecialEquipmentId; var specialEquipment = BLL.SpecialEquipmentService.GetSpecialEquipmentById(equipmentId.ToString()); if (specialEquipment != null) { equipmentName = specialEquipment.SpecialEquipmentName; } } return equipmentName; } protected string ConvertState(object State) { if (State != null) { if (State.ToString() == "0") { return "重新编制"; } else if (State.ToString() == "1") { return "编制"; } else if (State.ToString() == "2") { return "安质部部长审批"; } else if (State.ToString() == "3") { return "副经理审批"; } else if (State.ToString() == "4") { return "经理审批"; } else if (State.ToString() == "5") { return "审批完成"; } return ""; } 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 model = Funs.DB.Special_EquipmentCheck.FirstOrDefault(x => x.Special_EquipmentCheckId == rowID); if (model != null) { BLL.Special_EquipmentCheckService.DeleteById(rowID); } } this.BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } } #endregion protected void btnMenuView_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("EquipmentHazardView.aspx?Special_EquipmentCheckId={0}", id, "详情 - "))); } protected void Grid1_RowDoubleClick(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("EquipmentHazardView.aspx?Special_EquipmentCheckId={0}", id, "详情 - "))); } #region 导出 protected void btnOut_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string id = Grid1.SelectedRowID; OutExcel(id); } #region 导出 protected void OutExcel(string Special_EquipmentCheckId) { var Special_EquipmentCheckModel = Funs.DB.Special_EquipmentCheck.FirstOrDefault(x => x.Special_EquipmentCheckId == Special_EquipmentCheckId); var Type = Special_EquipmentCheckModel.Type; string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/安装前.xls"; if (Type == "3") { TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/安装中.xls"; } else if (Type == "4") { TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/使用中.xls"; } else if (Type == "5") { TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/拆除中.xls"; } //导出文件 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); HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); ICellStyle styleCenter = hssfworkbook.CreateCellStyle(); styleCenter.VerticalAlignment = VerticalAlignment.Center; styleCenter.Alignment = HorizontalAlignment.Center; styleCenter.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; styleCenter.BorderTop = BorderStyle.Thin; styleCenter.BorderRight = BorderStyle.Thin; styleCenter.BorderBottom = BorderStyle.Thin; styleCenter.WrapText = true; IFont font = styleCenter.GetFont(hssfworkbook); // font.Color = 10;//颜色 font.FontHeightInPoints = 10;//字体高度(与excel中的字号一致) styleCenter.SetFont(font); HSSFSheet recordSheet = (HSSFSheet)hssfworkbook.GetSheet("安装前"); if (Type == "3") { recordSheet = (HSSFSheet)hssfworkbook.GetSheet("安装中"); } else if (Type == "4") { recordSheet = (HSSFSheet)hssfworkbook.GetSheet("使用中(塔吊)"); } else if (Type == "5") { recordSheet = (HSSFSheet)hssfworkbook.GetSheet("拆除中"); } //项目名称 var Pname = ProjectService.GetProjectNameByProjectId(Special_EquipmentCheckModel.ProjectId); recordSheet.GetRow(1).CreateCell(0).SetCellValue("项目名称:" + Pname); recordSheet.GetRow(1).GetCell(0).CellStyle = styleCenter; var EquipmentCheck_CList = Funs.DB.EquipmentCheck_C.Where(x => x.Special_EquipmentCheckId == Special_EquipmentCheckId).OrderBy(x => x.SortIndex).ToList(); var rowCount = 3; if (EquipmentCheck_CList.Count > 0) { foreach (var item in EquipmentCheck_CList) { recordSheet.GetRow(rowCount).CreateCell(3).SetCellValue(item.CheckResult); recordSheet.GetRow(rowCount).GetCell(3).CellStyle = styleCenter; recordSheet.GetRow(rowCount).CreateCell(4).SetCellValue(item.CheckRemark); recordSheet.GetRow(rowCount).GetCell(4).CellStyle = styleCenter; rowCount += 1; //安装前 if (rowCount == 27 && Type == "2") { break; } //安装中 if (rowCount == 9 && Type == "3") { break; } //使用中 if (rowCount == 77 && Type == "4") { break; } //拆除中 if (rowCount == 19 && Type == "5") { break; } } } //增加签名和图片 if (Special_EquipmentCheckModel != null) { if (Type == "2") { //安装前 //结论 recordSheet.GetRow(27).CreateCell(1).SetCellValue(Special_EquipmentCheckModel.Conclusion); recordSheet.GetRow(27).GetCell(1).CellStyle = styleCenter; //签字 if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; AddCellPicture(recordSheet, hssfworkbook, imgUrls, 29, 1); recordSheet.GetRow(29).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(30).CreateCell(1).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(30).GetCell(1).CellStyle = styleCenter; } else if (Type == "3") { //安装中 recordSheet.GetRow(9).CreateCell(1).SetCellValue(Special_EquipmentCheckModel.Conclusion); recordSheet.GetRow(9).GetCell(1).CellStyle = styleCenter; //签字 if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; AddCellPicture(recordSheet, hssfworkbook, imgUrls, 11, 1); recordSheet.GetRow(11).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(12).CreateCell(1).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(12).GetCell(1).CellStyle = styleCenter; } else if (Type == "4") { //使用中 recordSheet.GetRow(77).CreateCell(1).SetCellValue(Special_EquipmentCheckModel.Conclusion); recordSheet.GetRow(77).GetCell(1).CellStyle = styleCenter; //签字 if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; AddCellPicture(recordSheet, hssfworkbook, imgUrls, 79, 1); recordSheet.GetRow(79).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(80).CreateCell(1).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(80).GetCell(1).CellStyle = styleCenter; } else if (Type == "5") { //拆除中 recordSheet.GetRow(19).CreateCell(1).SetCellValue(Special_EquipmentCheckModel.Conclusion); recordSheet.GetRow(19).GetCell(1).CellStyle = styleCenter; //签字 if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; AddCellPicture(recordSheet, hssfworkbook, imgUrls, 21, 1); recordSheet.GetRow(21).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(22).CreateCell(1).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(22).GetCell(1).CellStyle = styleCenter; } } using (FileStream filess = File.OpenWrite(ReportFileName)) { hssfworkbook.Write(filess); } FileInfo filet = new FileInfo(ReportFileName); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 if (Type == "2") { Response.AddHeader("Content-Disposition", "attachment; filename=建筑起重机械安装前隐患排查清单_" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls")); } else if (Type == "3") { Response.AddHeader("Content-Disposition", "attachment; filename=建筑起重机械安装中隐患排查清单__" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls")); } else if (Type == "4") { Response.AddHeader("Content-Disposition", "attachment; filename=建筑起重机械使用中隐患排查清单__" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls")); } else if (Type == "5") { Response.AddHeader("Content-Disposition", "attachment; filename=建筑起重机械拆除中隐患排查清单__" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls")); } // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } /// /// 向sheet插入图片 /// /// /// 文档对象 /// 网络图片地址 /// 哪行 /// 哪列 private static void AddCellPicture(ISheet sheet, HSSFWorkbook workbook, string imgPath, int row, int col) { Uri uri = new Uri(imgPath); //imgPath :网络图片地址 WebRequest webRequest = WebRequest.Create(uri); using (WebResponse webResponse = webRequest.GetResponse()) { //防止发生报错:GDI+中发生一般性错误的解决办法 Bitmap bitmap = new Bitmap(webResponse.GetResponseStream()); //读取图片流 Bitmap OldImage = new Bitmap(bitmap);//将图片流复制到新的图片流中 bitmap.Dispose(); //将原来的图片流释放,将图片文件进行解锁。 using (MemoryStream ms = new MemoryStream()) { OldImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] bytes = ms.ToArray(); int pictureIdx = 0; if (imgPath.Contains(".png") || imgPath.Contains(".PNG")) { pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.PNG); //添加图片 } if (imgPath.Contains(".jpg") || imgPath.Contains(".jpeg") || imgPath.Contains(".JPG")) { pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG); //添加图片 } HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, col, row, col + 1, row + 1); //图片位置,图片左上角为(col, row) HSSFPicture pict = (HSSFPicture)sheet.CreateDrawingPatriarch().CreatePicture(anchor, pictureIdx); //pict.Resize(); //用图片原始大小来显示 } } } #endregion #endregion } }