using BLL; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.IO; 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; namespace FineUIPro.Web.Customization.CNCCG.HSSE.InApproveManager { public partial class EquipmentCheck : 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; } } public string retrunStates { get { return (string)ViewState["retrunStates"]; } set { ViewState["retrunStates"] = 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"]; } this.ucTree.UnitId = this.CurrUser.UnitId; this.ucTree.ProjectId = this.ProjectId; if (this.CurrUser != null && this.CurrUser.PageSize.HasValue) { Grid1.PageSize = this.CurrUser.PageSize.Value; } EquipmentInItemId = Request.Params["EquipmentInItemId"]; retrunStates = Request.Params["rbStates"]; if (retrunStates=="0") { rbStates.SelectedValue = "0"; } else if (retrunStates=="1") { rbStates.SelectedValue = "1"; } this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); if (!string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { this.panelLeftRegion.Hidden = true; // this.Grid1.Columns[0].Hidden = true; ////权限按钮方法 //this.GetButtonPower(); } // 绑定表格 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.ProjectId,x.Type,x.Special_EquipmentCheckId,y.EquipmentInItemId,y.SizeCode,y.SizeModel,x.CheckDate, case x.Type when '0' then '日常检查' when '1' then '定期检查' when '6' then '日常检查(定稿)' when '7' then '定期检查(定稿)' 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='1' or Type ='0' or Type ='6' or Type ='7') 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; } #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("EquipmentCheckView.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("EquipmentCheckView.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); } /// /// 导出Excel /// protected void OutExcel(string Special_EquipmentCheckId) { var Special_EquipmentCheckModel = Funs.DB.Special_EquipmentCheck.FirstOrDefault(x => x.Special_EquipmentCheckId == Special_EquipmentCheckId); var imodel = Funs.DB.InApproveManager_EquipmentInItem.FirstOrDefault(x => x.EquipmentInItemId == Special_EquipmentCheckModel.EquipmentInItemId); //设备名称 var EquipmentInItemName = BLL.SpecialEquipmentService.GetSpecialEquipmentById(imodel.SpecialEquipmentId).SpecialEquipmentName; var Type = Special_EquipmentCheckModel.Type; if (Type=="6"|| Type=="7") { Alert.ShowInTop("定稿文件无法导出。", MessageBoxIcon.Warning); return; } string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/(塔吊)日常检查表.xls"; if (Type=="0") { //日常检查要判断 if (EquipmentInItemName== "施工升降机") { TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/(施工升降机)日常检查表.xls"; } else if (EquipmentInItemName== "架桥机") { TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/(架桥机)日常检查表.xls"; } else if (EquipmentInItemName == "普通门式起重机") { TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/(门式起重机)日常检查表.xls"; } } else if (Type == "1") { 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 == "1") { recordSheet = (HSSFSheet)hssfworkbook.GetSheet("定期检查表"); } //获取子表内容 加载是否符合要求和备注 var EquipmentCheck_CList = Funs.DB.EquipmentCheck_C.Where(x => x.Special_EquipmentCheckId == Special_EquipmentCheckId).OrderBy(x => x.SortIndex).ToList(); var rowCount = 2; if (EquipmentCheck_CList.Count > 0) { foreach (var item in EquipmentCheck_CList) { recordSheet.GetRow(rowCount).CreateCell(2).SetCellValue(item.CheckResult); recordSheet.GetRow(rowCount).GetCell(2).CellStyle = styleCenter; recordSheet.GetRow(rowCount).CreateCell(3).SetCellValue(item.CheckRemark); recordSheet.GetRow(rowCount).GetCell(3).CellStyle = styleCenter; rowCount += 1; //日常 if (rowCount == 6 && Type == "0" && (EquipmentInItemName == "架桥机" || EquipmentInItemName == "塔式起重机")) { break; } if (rowCount == 5 && Type == "0" && (EquipmentInItemName == "施工升降机")) { break; } if (rowCount == 7 && Type == "0" && (EquipmentInItemName == "普通门式起重机")) { break; } //定期 if (rowCount == 22 && Type == "1") { break; } } } //增加签名和图片 if (Special_EquipmentCheckModel != null) { if (Type == "0") { if ((EquipmentInItemName == "架桥机" || EquipmentInItemName == "塔式起重机")) { //签字 if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; imgUrls = ImgByJpg(imgUrls); AddCellPicture(recordSheet, hssfworkbook, imgUrls, 6, 1); recordSheet.GetRow(6).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(6).CreateCell(3).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(6).GetCell(3).CellStyle = styleCenter; } else if (EquipmentInItemName == "施工升降机") { if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; imgUrls = ImgByJpg(imgUrls); AddCellPicture(recordSheet, hssfworkbook, imgUrls, 5, 1); recordSheet.GetRow(5).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(5).CreateCell(3).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(5).GetCell(3).CellStyle = styleCenter; } else if (EquipmentInItemName == "普通门式起重机") { if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; imgUrls = ImgByJpg(imgUrls); AddCellPicture(recordSheet, hssfworkbook, imgUrls, 7, 1); recordSheet.GetRow(7).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(7).CreateCell(3).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(7).GetCell(3).CellStyle = styleCenter; } ////日常 ////结论 //recordSheet.GetRow(18).CreateCell(1).SetCellValue(Special_EquipmentCheckModel.Conclusion); //recordSheet.GetRow(18).GetCell(1).CellStyle = styleCenter; ////签字 //if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) //{ // var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; // imgUrls = ImgByJpg(imgUrls); // AddCellPicture(recordSheet, hssfworkbook, imgUrls, 20, 1); // recordSheet.GetRow(20).GetCell(1).CellStyle = styleCenter; //} ////日期 //recordSheet.GetRow(21).CreateCell(1).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); //recordSheet.GetRow(21).GetCell(1).CellStyle = styleCenter; } else if(Type=="1") { //定期 recordSheet.GetRow(22).CreateCell(1).SetCellValue(Special_EquipmentCheckModel.Conclusion); recordSheet.GetRow(22).GetCell(1).CellStyle = styleCenter; //签字 if (!string.IsNullOrEmpty(Special_EquipmentCheckModel.SignatureUrl)) { var imgUrls = Server.MapPath("~/") + Special_EquipmentCheckModel.SignatureUrl; imgUrls = ImgByJpg(imgUrls); AddCellPicture(recordSheet, hssfworkbook, imgUrls, 24, 1); recordSheet.GetRow(24).GetCell(1).CellStyle = styleCenter; } //日期 recordSheet.GetRow(25).CreateCell(1).SetCellValue("检查日期:" + string.Format("{0:yyyy-MM-dd}", Special_EquipmentCheckModel.CheckDate)); recordSheet.GetRow(25).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; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=日常检查表_" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls")); if (Type == "1") { 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(); } /// /// 透明背景改成白色 /// /// /// private string ImgByJpg(string imgUrls) { System.Drawing.Image img = System.Drawing.Image.FromFile(imgUrls); // Assumes myImage is the PNG you are converting using (var b = new Bitmap(img.Width, img.Height)) { b.SetResolution(img.HorizontalResolution, img.VerticalResolution); using (var g = Graphics.FromImage(b)) { g.Clear(Color.White); g.DrawImageUnscaled(img, 0, 0); } // Now save b as a JPEG like you normally would b.Save(imgUrls.Replace("png", "jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); return imgUrls.Replace("png", "jpg"); } } /// /// 向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 } }