private void ExportToTag_SecurityNotification(string filePath, MSDS_Specification model) { Tag_SecurityNotification tag = new Tag_SecurityNotification(); tag.SheetName = "大标签"; tag.templateFilePath = filePath; tag.ProductName = model.CN_Name ?? model.Product_Name; #region 危险图标 List <string> imgPathList = new List <string>(); string strRootPath = Server.MapPath("~/Content/Images/"); if (model.IsExplosive) { imgPathList.Add(strRootPath + "Explosive.png"); } if (model.IsFlammable) { imgPathList.Add(strRootPath + "Flammable.png"); } if (model.IsCorrosive) { imgPathList.Add(strRootPath + "Corrosive.png"); } if (model.IsHealthHazard) { imgPathList.Add(strRootPath + "HealthHazard.png"); } if (model.IsToxic) { imgPathList.Add(strRootPath + "Toxic.png"); } if (model.IsOxidizing) { imgPathList.Add(strRootPath + "Oxidizing.png"); } if (model.IsGasUnderPressure) { imgPathList.Add(strRootPath + "GasUnderPressure.png"); } if (model.IsIrritant) { imgPathList.Add(strRootPath + "Irritant.png"); } if (model.IsDangerousToEnvironment) { imgPathList.Add(strRootPath + "DangerousToEnvironment.png"); } tag.WarningPicPaths = imgPathList; tag.BlankPicPath = strRootPath + "blank.png"; #endregion #region 个人防护图标 List <string> imgPathList_Protection = new List <string>(); if (model.IsProtection_FaceAndEye) { imgPathList_Protection.Add(strRootPath + "Product_Protection_FaceAndEye.png"); } if (model.IsProtection_Hand) { imgPathList_Protection.Add(strRootPath + "Product_Protection_Hand.png"); } if (model.IsProtection_Breathing) { imgPathList_Protection.Add(strRootPath + "Product_Protection_Breathing.png"); } if (model.IsProtection_Foot) { imgPathList_Protection.Add(strRootPath + "Product_Protection_Foot.png"); } if (model.IsProtection_Body) { imgPathList_Protection.Add(strRootPath + "Product_Protection_Body.png"); } if (model.IsProtection_Other) { imgPathList_Protection.Add(strRootPath + "Product_Protection_Other.png"); } tag.ProtectionPicPaths = imgPathList_Protection; #endregion char[] sp = new char[1] { ',' }; #region 危害简述 tag.HazardousDescription = ""; if (!string.IsNullOrEmpty(model.GHS_HazardouDes_Values)) { string[] names = model.GHS_HazardouDes_Values.Split(sp, StringSplitOptions.RemoveEmptyEntries); IList <MSDS_H_Statement> h_statments = _h_StatementService.GetListByNames(names); int i = 1; foreach (MSDS_H_Statement item in h_statments) { tag.HazardousDescription += "● " + item.Description + "\r\n"; i++; if (i > 3) { break; } } } else { tag.HazardousDescription = model.GHS_HazardouDes_Append; } #endregion tag.Product_ET_FaceAndEye = model.Product_ET_FaceAndEye?.Replace("\r\n", ""); tag.Product_ET_SkinAndHand = model.Product_ET_SkinAndHand?.Replace("\r\n", ""); tag.Product_ET_Inhalation = model.Product_ET_Inhalation?.Replace("\r\n", ""); tag.Product_ET_Ingestion = model.Product_ET_Ingestion?.Replace("\r\n", ""); _importExportService.ExportTagWithTemplate(tag); }
public void ExportTagWithTemplate(Tag_SecurityNotification model) { FileInfo fileInfo = new FileInfo(model.templateFilePath); if (!fileInfo.Exists) { throw new Exception("未找到名为 " + model.templateFilePath + " 的模板"); } using (var xlPackage = new ExcelPackage(fileInfo)) { var worksheet = xlPackage.Workbook.Worksheets[model.SheetName]; if (worksheet == null) { throw new Exception("模板中未包含名为 " + model.SheetName + " 的Sheet"); } worksheet.Cells["C6"].RichText.Text = model.ProductName; worksheet.Cells["C9"].RichText.Text = model.HazardousDescription; worksheet.Cells["E14"].RichText.Text = model.Product_ET_FaceAndEye; worksheet.Cells["E15"].RichText.Text = model.Product_ET_SkinAndHand; worksheet.Cells["E16"].RichText.Text = model.Product_ET_Inhalation; worksheet.Cells["E17"].RichText.Text = model.Product_ET_Ingestion; #region 警示标识 int warningCount = model.WarningPicPaths.Count; if (warningCount == 1) { #region string pName = "Picture2"; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.WarningPicPaths[0]); } for (int i = 1; i < 6; i++) { if (i == 2) { continue; } string _pName = "Picture" + i; var _listA = worksheet.Drawings.Where(o => o.Name == _pName).ToList(); foreach (var item in _listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.BlankPicPath); } } #endregion } else if (warningCount == 2) { string pName = ""; #region for (int i = 0; i < warningCount; i++) { if (i == 0) { pName = "Picture2"; } else { pName = "Picture3"; } var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.WarningPicPaths[i]); } } for (int i = 1; i < 6; i++) { if (i == 2 || i == 3) { continue; } pName = "Picture" + i; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.BlankPicPath); } } #endregion } else if (warningCount == 3) { #region string pName = ""; for (int i = 0; i < warningCount; i++) { if (i == 0) { pName = "Picture1"; } else if (i == 1) { pName = "Picture2"; } else { pName = "Picture3"; } var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.WarningPicPaths[i]); } } for (int i = 1; i < 6; i++) { if (i == 1 || i == 2 || i == 3) { continue; } pName = "Picture" + i; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.BlankPicPath); } } #endregion } else if (warningCount == 4) { #region string pName = ""; for (int i = 0; i < warningCount; i++) { if (i == 0) { pName = "Picture2"; } else if (i == 1) { pName = "Picture3"; } else if (i == 2) { pName = "Picture4"; } else { pName = "Picture5"; } var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.WarningPicPaths[i]); } } for (int i = 1; i < 2; i++) { if (i == 1) { pName = "Picture" + i; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.BlankPicPath); } } } #endregion } else if (warningCount == 5) { int j = 1; foreach (string picUrl in model.WarningPicPaths) { string pName = "Picture" + j; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(picUrl); } j++; } } #endregion #region 个人防护 int protectCount = model.ProtectionPicPaths.Count; int k = 1; foreach (string picUrl in model.ProtectionPicPaths) { string pName = "PictureProtection" + k; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(picUrl); } k++; } for (int i = k; i < 7; i++) { string pName = "PictureProtection" + i; var listA = worksheet.Drawings.Where(o => o.Name == pName).ToList(); foreach (var item in listA) { var img = item as ExcelPicture; img.Image = Image.FromFile(model.BlankPicPath); } } #endregion xlPackage.Save(); } }