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 EquipmentHazardView : PageBase
{
///
/// 主键
///
public string Special_EquipmentCheckId
{
get
{
return (string)ViewState["Special_EquipmentCheckId"];
}
set
{
ViewState["Special_EquipmentCheckId"] = 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)
{
this.labCheckDate.Text = string.Format("{0:yyyy-MM-dd}", SecModel.CheckDate);
if (!string.IsNullOrEmpty(SecModel.SignatureUrl))
{
this.Image1.ImageUrl = "~/" + SecModel.SignatureUrl;
}
lbConclusion.Text = SecModel.Conclusion;
if (SecModel.Type=="2")
{
lbType.Text = "安装前";
}
else if (SecModel.Type == "3")
{
lbType.Text = "安装中";
}
else if (SecModel.Type == "4")
{
lbType.Text = "使用中";
}
else if (SecModel.Type == "5")
{
lbType.Text = "拆除中";
}
//加载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=-1", this.Special_EquipmentCheckId, "2F477173-25A5-4107-AA73-720DEACC00B2")));
}
}
#endregion
#region 导出
protected void btnOut_Click(object sender,EventArgs e) {
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") + ".xlsx"));
}
else if(Type=="3")
{
Response.AddHeader("Content-Disposition", "attachment; filename=建筑起重机械安装中隐患排查清单__" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
}
else if (Type == "4")
{
Response.AddHeader("Content-Disposition", "attachment; filename=建筑起重机械使用中隐患排查清单__" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
}
else if (Type == "5")
{
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
}
}