From ddcb90870fc8eb2904cc37e5abb528432d04830e Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Mon, 20 Sep 2021 15:12:02 +0500 Subject: [PATCH] Added images copying --- ConsoleApp1/Program.cs | 67 ++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index aea971ea..ef014e22 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -17,16 +17,31 @@ namespace ConsoleApp1 class Program { - private static void CopyImages(IXLWorksheet sourceSheet, IXLWorksheet resultSheet) + private static IXLWorksheet CopyImagesToAnotherSheet(IXLWorksheet sourceSheet, IXLWorksheet resultSheet) { foreach (var picture in sourceSheet.Pictures) { - var newImage = resultSheet.AddPicture(picture.ImageStream) - .WithPlacement(XLPicturePlacement.FreeFloating) - .WithSize(picture.Width, picture.Height) - .MoveTo(resultSheet.Cell(picture.TopLeftCell.Address), - picture.Left, picture.Top); + resultSheet.AddPicture(picture.ImageStream) + .WithPlacement(XLPicturePlacement.Move) + .WithSize(picture.Width, picture.Height) + .MoveTo(resultSheet.Cell(picture.TopLeftCell.Address), + picture.Left, picture.Top); } + + return resultSheet; + } + + private static void RemovePicturesFromSheet(IXLWorksheet sheet) + { + var picsNames = new List(); + + foreach (var p in sheet.Pictures) + picsNames.Add(p.Name); + + var filteredPics = picsNames.Distinct(); + + foreach (var n in filteredPics) + sheet.Pictures.Delete(n); } private static IXLRange GetCellsRange(IXLWorksheet sheet) @@ -38,17 +53,36 @@ namespace ConsoleApp1 return rngData; } + // Изображения не переносятся при вызове метода sheet.CopyTo(). Падает с exception. + // Для этого сначала все изображения переносятся на временный лист "PicsCopy", + // затем удаляются из копируемого листа, копируемый лист копируется в лист-результат + // и после этого из temp листа "PicsCopy" в лист-результат также копируются изображения. + private static void CopySheetToFile(IXLWorksheet currentSheet, XLWorkbook resultExcelFile) + { + var picturesCopySheet = resultExcelFile.AddWorksheet("PicsCopy"); + + CopyImagesToAnotherSheet(currentSheet, picturesCopySheet); + + RemovePicturesFromSheet(currentSheet); + + var resultSheet = currentSheet.CopyTo(resultExcelFile, currentSheet.Name); + + CopyImagesToAnotherSheet(picturesCopySheet, resultSheet); + + picturesCopySheet.Delete(); + } + static void Main(/*string[] args*/) { var sourceExcelPaths = new List { //@"D:\excels\excel1.xlsx", //@"D:\excels\excel2.xlsx" - //@"D:\excels\e1.xlsx", - //@"D:\excels\e2.xlsx", - //@"D:\excels\e3.xlsx", - //@"D:\excels\e4.XLSX", - //@"D:\excels\e5.XLSX", + @"D:\excels\e1.xlsx", + @"D:\excels\e2.xlsx", + @"D:\excels\e3.xlsx", + @"D:\excels\e4.XLSX", + @"D:\excels\e5.XLSX", @"D:\excels\e6.XLSX" }; @@ -84,7 +118,6 @@ namespace ConsoleApp1 return; var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name); - //resultSheet.Cell(1, 1).Value = rngData; var rngData = GetCellsRange(sheet); @@ -98,7 +131,7 @@ namespace ConsoleApp1 resultSheet.Column(i).Width = sheet.Column(i).Width; } - CopyImages(sheet, resultSheet); + CopyImagesToAnotherSheet(sheet, resultSheet); GC.Collect(); } @@ -113,18 +146,14 @@ namespace ConsoleApp1 foreach (var d in columnsToDelete) d.Delete(); - var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name); - - CopyImages(sheet, resultSheet); + CopySheetToFile(sheet, resultExcelFile); GC.Collect(); } // Если по колонкам все хорошо, копируем лист красиво. else { - var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name); - - CopyImages(sheet, resultSheet); + CopySheetToFile(sheet, resultExcelFile); GC.Collect(); }