//using AsbSaubReport; //using AutoMapper; using System; using System.IO; using System.Collections.Generic; using System.Linq; using ClosedXML.Excel; using ClosedXML.Excel.Drawings; namespace ConsoleApp1 { //var options = new DbContextOptionsBuilder() // .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") // .Options; //var context = new AsbCloudDbContext(options); class ImageInfo { public int Id { get; set; } public byte[] Data { get; set; } public int Height { get; set; } public int Width { get; set; } public IXLAddress TopLeftCellAddress { get; set; } public int Left { get; set; } public int Top { get; set; } } class Program { private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable imagesInfos, IXLWorksheet resultSheet) { foreach (var image in imagesInfos) { var stream = new MemoryStream(); stream.Write(image.Data, 0, image.Data.Length); resultSheet.AddPicture(stream) .WithPlacement(XLPicturePlacement.Move) .WithSize(image.Width, image.Height) .MoveTo(resultSheet.Cell(image.TopLeftCellAddress), image.Left, image.Top); } return resultSheet; } private static void RemovePicturesFromSheet(IXLWorksheet sheet) { var filteredPics = sheet.Pictures.Select(p => p.Name).Distinct().ToList(); foreach (var n in filteredPics) sheet.Pictures.Delete(n); } private static IXLRange GetCellsRange(IXLWorksheet sheet) { var firstTableCell = sheet.FirstCellUsed(); var lastTableCell = sheet.LastCellUsed(); var rngData = sheet.Range(firstTableCell.Address, lastTableCell.Address); return rngData; } 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\e6.XLSX" }; const string resultExcelPath = @"D:\excels\result.xlsx"; using var resultExcelFile = new XLWorkbook(XLEventTracking.Disabled); const int maxAllowedColumns = 256; foreach(var sourceExcelPath in sourceExcelPaths) { using var sourceExcelFile = new XLWorkbook(sourceExcelPath, XLEventTracking.Disabled); Console.WriteLine($"Добавляется файл {Path.GetFileName(sourceExcelPath)}"); foreach (var sheet in sourceExcelFile.Worksheets) { var imagesInfos = sheet.Pictures.Select(p => new ImageInfo { Id = p.Id, Data = p.ImageStream.GetBuffer(), Height = p.Height, Width = p.Width, TopLeftCellAddress = p.TopLeftCell.Address, Left = p.Left, Top = p.Top }).ToList(); if (sheet.Columns().Count() > maxAllowedColumns) { var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name); var rngData = GetCellsRange(sheet); rngData.CopyTo(resultSheet.Cell(1, 1)); var lastRowWithData = rngData.LastRowUsed().RangeAddress .LastAddress.RowNumber; for (int i = 1; i < lastRowWithData; i++) { resultSheet.Row(i).Height = sheet.Row(i).Height; resultSheet.Column(i).Width = sheet.Column(i).Width; } CopyImagesToAnotherSheet(imagesInfos, resultSheet); } else { RemovePicturesFromSheet(sheet); var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name); CopyImagesToAnotherSheet(imagesInfos, resultSheet); } } } resultExcelFile.SaveAs(resultExcelPath); Console.WriteLine("Done. Press any key to quit."); Console.ReadKey(); } } }