using BLL; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; 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 EquipmentCheckView : PageBase { /// /// 主键 /// public string Special_EquipmentCheckId { get { return (string)ViewState["Special_EquipmentCheckId"]; } set { ViewState["Special_EquipmentCheckId"] = value; } } public string Type { get { return (string)ViewState["Type"]; } set { ViewState["Type"] = value; } } /// /// 页面加载 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { hdAttachUrl.Text = string.Empty; hdId.Text = string.Empty; btnClose.OnClientClick = ActiveWindow.GetHideReference(); this.Special_EquipmentCheckId = Request.Params["Special_EquipmentCheckId"]; var SecModel = Funs.DB.Special_EquipmentCheck.FirstOrDefault(x => x.Special_EquipmentCheckId == Special_EquipmentCheckId); if (SecModel!=null) { Type = SecModel.Type; this.labCheckDate.Text = string.Format("{0:yyyy-MM-dd}", SecModel.CheckDate); if (!string.IsNullOrEmpty(SecModel.SignatureUrl)) { this.Image1.ImageUrl = "~/" + SecModel.SignatureUrl; } if (Type=="1") { lbConclusion.Text = SecModel.Conclusion; frConclusion.Hidden = false; } if (SecModel.Type == "0") { lbType.Text = "日常检查"; } else if (SecModel.Type == "1") { lbType.Text = "定期检查"; } else if (SecModel.Type == "6") { lbType.Text = "日常检查(定稿)"; lbConclusion.Hidden = true; Grid1.Hidden = true; } else if (SecModel.Type == "7") { lbType.Text = "定期检查(定稿)"; lbConclusion.Hidden = true; Grid1.Hidden = true; } //加载grid this.BindGrid(); } } } #region 加载grid protected void BindGrid() { string strSql = @"select c.SupCheckItem,a.EquipmentCheck_Cid,a.Special_EquipmentCheckId,a.CheckResult, a.CheckRemark,a.SortIndex,b.CheckContent,c.CheckItemName as CheckContent2, d.CheckItemName as CheckContent1 from EquipmentCheck_C a left join Technique_CheckItemDetail b on a.CheckItemDetailId=b.CheckItemDetailId left join Technique_CheckItemSet c on b.CheckItemSetId=c.CheckItemSetId left join Technique_CheckItemSet d on d.CheckItemSetId=c.SupCheckItem where Special_EquipmentCheckId='"+ Special_EquipmentCheckId + "' order by a.SortIndex"; List listStr = new List(); SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.DataSource = tb; Grid1.DataBind(); } #endregion #region 附件上传 /// /// 上传附件 /// /// /// protected void btnAttachUrl_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.Special_EquipmentCheckId)) { PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/WebApi&menuId={1}&type=0", this.Special_EquipmentCheckId, "7D91A998-A665-4CC9-9E6B-141B14FA2690"))); } } #endregion #region 导出 /// /// 导出excel /// /// /// protected void btnOut_Click(object sender, EventArgs e) { OutExcel(); } /// /// 导出Excel /// protected void OutExcel (){ string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = Server.MapPath("~/") + "File/Excel/CNCCG/日常检查表.xls"; 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(4).SetCellValue(item.CheckResult); recordSheet.GetRow(rowCount).GetCell(4).CellStyle = styleCenter; recordSheet.GetRow(rowCount).CreateCell(5).SetCellValue(item.CheckRemark); recordSheet.GetRow(rowCount).GetCell(5).CellStyle = styleCenter; rowCount += 1; //日常 if (rowCount == 18 && Type == "0") { break; } //定期 if (rowCount == 22 && Type == "1") { break; } } } //增加签名和图片 var Special_EquipmentCheckModel = Funs.DB.Special_EquipmentCheck.FirstOrDefault(x => x.Special_EquipmentCheckId == Special_EquipmentCheckId); if (Special_EquipmentCheckModel != null) { if (Type == "0") { //日常 //结论 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; 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 { //定期 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; 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; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 if (Type == "0") { Response.AddHeader("Content-Disposition", "attachment; filename=起重机械(塔吊)日常检查表_" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx")); } else { 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(); } /// /// 向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 } }