diff --git a/SGGL/FineUIPro.Web/Transfer/PunchlistFrom.aspx.cs b/SGGL/FineUIPro.Web/Transfer/PunchlistFrom.aspx.cs index b09e45d1..5069cb1e 100644 --- a/SGGL/FineUIPro.Web/Transfer/PunchlistFrom.aspx.cs +++ b/SGGL/FineUIPro.Web/Transfer/PunchlistFrom.aspx.cs @@ -13,6 +13,8 @@ using System.Data.SqlClient; using System.Drawing.Imaging; using System.IO; using System.Linq; +using System.Net; +using System.Text; using System.Web.Services.Description; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; @@ -530,189 +532,206 @@ namespace FineUIPro.Web.Transfer ShowNotify("没有要导出的数据!!!", MessageBoxIcon.Warning); return; } - newUrl = uploadfilepath.Replace("导出模板", "导出数据").Replace(".xlsx", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"); - File.Copy(uploadfilepath, newUrl); - NPOI.SS.UserModel.IWorkbook workbook; - using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) + try { - workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); - } - - NPOI.SS.UserModel.ICellStyle cellStyleC = workbook.CreateCellStyle(); - cellStyleC.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; - cellStyleC.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; - cellStyleC.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; - cellStyleC.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; - cellStyleC.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; - cellStyleC.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; - cellStyleC.WrapText = true; - - NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); - - //var pcList = Funs.DB.AttachFile.Where(p => p.MenuId == BLL.Const.PunchlistFromMenuId && p.AttachUrl != null && p.AttachUrl != "").ToList(); - var pcList = new List(); - if (rbOutType.SelectedValue == "1") - {//导出带图片 - pcList = Funs.DB.AttachFile.Where(p => p.MenuId == BLL.Const.PunchlistFromMenuId && p.AttachUrl != null && p.AttachUrl != "").ToList(); - } - int i = 2; - foreach (DataRow tbRow in tb.Rows) - { - string Id = tbRow["Id"].ToString(); - - NPOI.SS.UserModel.IRow row = sheet.CreateRow(i); - NPOI.SS.UserModel.ICell cell; - cell = row.CreateCell(0); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Num_NO"] == null ? "" : tbRow["Num_NO"].ToString()); - cell = row.CreateCell(1); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["System_No"] == null ? "" : tbRow["System_No"].ToString()); - cell = row.CreateCell(2); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Sub_Sys_No"] == null ? "" : tbRow["Sub_Sys_No"].ToString()); - cell = row.CreateCell(3); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["DESCRIPTION"] == null ? "" : tbRow["DESCRIPTION"].ToString()); - - //Photoes附件 - var photoesUrl = pcList.FirstOrDefault(p => p.ToKeyId == $"{Id}_A"); - if (photoesUrl != null && !string.IsNullOrWhiteSpace(photoesUrl.AttachUrl)) + newUrl = uploadfilepath.Replace("导出模板", "导出数据").Replace(".xlsx", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"); + File.Copy(uploadfilepath, newUrl); + NPOI.SS.UserModel.IWorkbook workbook; + using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { - string[] arrUrl = photoesUrl.AttachUrl.Split(','); - sheet.SetColumnWidth(4, 30 * 256); - row.Height = (short)(90 * 20 * arrUrl.Length); - foreach (string url in arrUrl) - { - var oneUrl = Server.MapPath("~/") + url; - byte[] bytes = System.IO.File.ReadAllBytes(oneUrl); - int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); - - // 第三步:创建画部 - IDrawing patriarch = sheet.CreateDrawingPatriarch(); - // 第四步:设置锚点 - int rowline = 1; // y方向 - // 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数) - IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 4, i, 5, i + 1); - // 第五步:把图片插到相应的位置+1 - IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); - //就取第一张,如果后期想取全部 那把下面这个跳出语句去掉 - break; - } + workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); } - cell = row.CreateCell(5); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Cat"] == null ? "" : tbRow["Cat"].ToString()); - cell = row.CreateCell(6); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Raised_By"] == null ? "" : tbRow["Raised_By"].ToString()); - cell = row.CreateCell(7); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Date_Raised"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Date_Raised"]).ToString("yyyy-MM-dd")); - cell = row.CreateCell(8); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Disc"] == null ? "" : tbRow["Disc"].ToString()); - cell = row.CreateCell(9); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["IsEngStr"] == null ? "" : tbRow["IsEngStr"].ToString()); - cell = row.CreateCell(10); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["IsMatIStr"] == null ? "" : tbRow["IsMatIStr"].ToString()); - cell = row.CreateCell(11); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Punch_Type"] == null ? "" : tbRow["Punch_Type"].ToString()); - cell = row.CreateCell(12); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Required_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Required_Date"]).ToString("yyyy-MM-dd")); - cell = row.CreateCell(13); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Action_By"] == null ? "" : tbRow["Action_By"].ToString()); - cell = row.CreateCell(14); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["PIC"] == null ? "" : tbRow["PIC"].ToString()); - cell = row.CreateCell(15); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["PIC_WUH"] == null ? "" : tbRow["PIC_WUH"].ToString()); - cell = row.CreateCell(16); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Correction_Action"] == null ? "" : tbRow["Correction_Action"].ToString()); + NPOI.SS.UserModel.ICellStyle cellStyleC = workbook.CreateCellStyle(); + cellStyleC.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyleC.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyleC.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyleC.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; + cellStyleC.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; + cellStyleC.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; + cellStyleC.WrapText = true; - //CorrectedPhotos附件 - var correctedPhotosUrl = pcList.FirstOrDefault(p => p.ToKeyId == $"{Id}_B"); - if (correctedPhotosUrl != null && !string.IsNullOrWhiteSpace(correctedPhotosUrl.AttachUrl)) + NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); + + //var pcList = Funs.DB.AttachFile.Where(p => p.MenuId == BLL.Const.PunchlistFromMenuId && p.AttachUrl != null && p.AttachUrl != "").ToList(); + var pcList = new List(); + if (rbOutType.SelectedValue == "1") + {//导出带图片 + pcList = Funs.DB.AttachFile.Where(p => p.MenuId == BLL.Const.PunchlistFromMenuId && p.AttachUrl != null && p.AttachUrl != "").ToList(); + } + int i = 2; + foreach (DataRow tbRow in tb.Rows) { - string[] arrUrl = correctedPhotosUrl.AttachUrl.Split(','); - sheet.SetColumnWidth(17, 30 * 256); - row.Height = (short)(90 * 20); - foreach (string url in arrUrl) - { - var oneUrl = Server.MapPath("~/") + url; - byte[] bytes = System.IO.File.ReadAllBytes(oneUrl); - int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); + string Id = tbRow["Id"].ToString(); - // 第三步:创建画部 - IDrawing patriarch = sheet.CreateDrawingPatriarch(); - // 第四步:设置锚点 - int rowline = 1; // y方向 - // 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数) - IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 17, i, 18, i + 1); - // 第五步:把图片插到相应的位置+1 - IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); - //就取第一张,如果后期想取全部 那把下面这个跳出语句去掉 - break; + NPOI.SS.UserModel.IRow row = sheet.CreateRow(i); + NPOI.SS.UserModel.ICell cell; + cell = row.CreateCell(0); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Num_NO"] == null ? "" : tbRow["Num_NO"].ToString()); + cell = row.CreateCell(1); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["System_No"] == null ? "" : tbRow["System_No"].ToString()); + cell = row.CreateCell(2); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Sub_Sys_No"] == null ? "" : tbRow["Sub_Sys_No"].ToString()); + cell = row.CreateCell(3); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["DESCRIPTION"] == null ? "" : tbRow["DESCRIPTION"].ToString()); + + //Photoes附件 + var photoesUrl = pcList.FirstOrDefault(p => p.ToKeyId == $"{Id}_A"); + if (photoesUrl != null && !string.IsNullOrWhiteSpace(photoesUrl.AttachUrl)) + { + string[] arrUrl = photoesUrl.AttachUrl.Split(','); + sheet.SetColumnWidth(4, 30 * 256); + row.Height = (short)(90 * 20 * arrUrl.Length); + foreach (string url in arrUrl) + { + var oneUrl = Server.MapPath("~/") + url; + byte[] bytes = System.IO.File.ReadAllBytes(oneUrl); + int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); + + // 第三步:创建画部 + IDrawing patriarch = sheet.CreateDrawingPatriarch(); + // 第四步:设置锚点 + int rowline = 1; // y方向 + // 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数) + IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 4, i, 5, i + 1); + // 第五步:把图片插到相应的位置+1 + IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); + //就取第一张,如果后期想取全部 那把下面这个跳出语句去掉 + break; + } } + + cell = row.CreateCell(5); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Cat"] == null ? "" : tbRow["Cat"].ToString()); + cell = row.CreateCell(6); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Raised_By"] == null ? "" : tbRow["Raised_By"].ToString()); + cell = row.CreateCell(7); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Date_Raised"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Date_Raised"]).ToString("yyyy-MM-dd")); + cell = row.CreateCell(8); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Disc"] == null ? "" : tbRow["Disc"].ToString()); + cell = row.CreateCell(9); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["IsEngStr"] == null ? "" : tbRow["IsEngStr"].ToString()); + cell = row.CreateCell(10); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["IsMatIStr"] == null ? "" : tbRow["IsMatIStr"].ToString()); + cell = row.CreateCell(11); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Punch_Type"] == null ? "" : tbRow["Punch_Type"].ToString()); + cell = row.CreateCell(12); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Required_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Required_Date"]).ToString("yyyy-MM-dd")); + cell = row.CreateCell(13); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Action_By"] == null ? "" : tbRow["Action_By"].ToString()); + cell = row.CreateCell(14); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["PIC"] == null ? "" : tbRow["PIC"].ToString()); + cell = row.CreateCell(15); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["PIC_WUH"] == null ? "" : tbRow["PIC_WUH"].ToString()); + cell = row.CreateCell(16); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Correction_Action"] == null ? "" : tbRow["Correction_Action"].ToString()); + + //CorrectedPhotos附件 + var correctedPhotosUrl = pcList.FirstOrDefault(p => p.ToKeyId == $"{Id}_B"); + if (correctedPhotosUrl != null && !string.IsNullOrWhiteSpace(correctedPhotosUrl.AttachUrl)) + { + string[] arrUrl = correctedPhotosUrl.AttachUrl.Split(','); + sheet.SetColumnWidth(17, 30 * 256); + row.Height = (short)(90 * 20); + foreach (string url in arrUrl) + { + var oneUrl = Server.MapPath("~/") + url; + byte[] bytes = System.IO.File.ReadAllBytes(oneUrl); + int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); + + // 第三步:创建画部 + IDrawing patriarch = sheet.CreateDrawingPatriarch(); + // 第四步:设置锚点 + int rowline = 1; // y方向 + // 参数说明:(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格列数,行数,终止单元格列数,行数) + IClientAnchor anchor = patriarch.CreateAnchor(0, 0, 0, 0, 17, i, 18, i + 1); + // 第五步:把图片插到相应的位置+1 + IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); + //就取第一张,如果后期想取全部 那把下面这个跳出语句去掉 + break; + } + } + + cell = row.CreateCell(18); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Actual_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Actual_Date"]).ToString("yyyy-MM-dd")); + cell = row.CreateCell(19); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Cleared_By"] == null ? "" : tbRow["Cleared_By"].ToString()); + cell = row.CreateCell(20); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Cleared_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Cleared_Date"]).ToString("yyyy-MM-dd")); + cell = row.CreateCell(21); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Confirmed_By"] == null ? "" : tbRow["Confirmed_By"].ToString()); + cell = row.CreateCell(22); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Confirmed_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Confirmed_Date"]).ToString("yyyy-MM-dd")); + cell = row.CreateCell(23); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Verified_By"] == null ? "" : tbRow["Verified_By"].ToString()); + cell = row.CreateCell(24); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Verified_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Verified_Date"]).ToString("yyyy-MM-dd")); + cell = row.CreateCell(25); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Status"] == null ? "" : tbRow["Status"].ToString()); + cell = row.CreateCell(26); + cell.CellStyle = cellStyleC; + cell.SetCellValue(tbRow["Remark"] == null ? "" : tbRow["Remark"].ToString()); + + + i++; } - cell = row.CreateCell(18); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Actual_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Actual_Date"]).ToString("yyyy-MM-dd")); - cell = row.CreateCell(19); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Cleared_By"] == null ? "" : tbRow["Cleared_By"].ToString()); - cell = row.CreateCell(20); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Cleared_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Cleared_Date"]).ToString("yyyy-MM-dd")); - cell = row.CreateCell(21); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Confirmed_By"] == null ? "" : tbRow["Confirmed_By"].ToString()); - cell = row.CreateCell(22); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Confirmed_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Confirmed_Date"]).ToString("yyyy-MM-dd")); - cell = row.CreateCell(23); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Verified_By"] == null ? "" : tbRow["Verified_By"].ToString()); - cell = row.CreateCell(24); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Verified_Date"] == DBNull.Value ? "" : Convert.ToDateTime(tbRow["Verified_Date"]).ToString("yyyy-MM-dd")); - cell = row.CreateCell(25); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Status"] == null ? "" : tbRow["Status"].ToString()); - cell = row.CreateCell(26); - cell.CellStyle = cellStyleC; - cell.SetCellValue(tbRow["Remark"] == null ? "" : tbRow["Remark"].ToString()); - - - i++; + // 第三步:写入文件流 + using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) + { + workbook.Write(stream); + workbook.Close(); + } + string fileName = Path.GetFileName(newUrl); + FileInfo info = new FileInfo(newUrl); + long fileSize = info.Length; + Response.Clear(); + Response.ContentType = "application/x-zip-compressed"; + Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); + Response.AddHeader("Content-Length", fileSize.ToString()); + Response.TransmitFile(newUrl, 0, fileSize); + Response.Flush(); + Response.Close(); + File.Delete(newUrl); } - - // 第三步:写入文件流 - using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) + catch (WebException ex) { - workbook.Write(stream); - workbook.Close(); + if (File.Exists(newUrl)) + { + File.Delete(newUrl); + } + } + finally + { + if (File.Exists(newUrl)) + { + File.Delete(newUrl); + } } - string fileName = Path.GetFileName(newUrl); - FileInfo info = new FileInfo(newUrl); - long fileSize = info.Length; - Response.Clear(); - Response.ContentType = "application/x-zip-compressed"; - Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); - Response.AddHeader("Content-Length", fileSize.ToString()); - Response.TransmitFile(newUrl, 0, fileSize); - Response.Flush(); - Response.Close(); - File.Delete(newUrl); } } } \ No newline at end of file