//using AsbSaubReport; //using AutoMapper; using System; using System.IO; using System.Collections.Generic; using System.Linq; using ClosedXML.Excel; 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 Program { 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); } 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(); const int maxAllowedColumns = 256; foreach(var sourceExcelPath in sourceExcelPaths) { using var sourceExcelFile = new XLWorkbook(sourceExcelPath); foreach (var sheet in sourceExcelFile.Worksheets) { //var columnsToDelete = sheet.Columns().Skip(sheet.ColumnsUsed().Count()); //foreach (var d in columnsToDelete) // d.Delete(); if (sheet.Columns().Count() > maxAllowedColumns && sheet.Columns().Count() / sheet.ColumnsUsed().Count() > 5) { Console.WriteLine($"В файле {Path.GetFileName(sourceExcelPath)} " + $"{sheet.Columns().Count() - sheet.ColumnsUsed().Count()} пустых колонок " + $"без записей. \n Если продолжить выполнение, форматирование выходного" + $"листа может быть нарушено. \n Для сохранения структуры документа " + $"рекомендуется прервать выполнение и удалить неиспользуемые колонки. \n" + $" Продолжить выполнение? (y/n)"); var res = Console.ReadLine(); if (res != "y") return; var firstTableCell = sheet.FirstCellUsed(); var lastTableCell = sheet.LastCellUsed(); var rngData = sheet.Range(firstTableCell.Address, lastTableCell.Address); RemovePicturesFromSheet(sheet); var wsCopy = resultExcelFile.Worksheets.Add(sheet.Name); wsCopy.Cell(1, 1).Value = rngData; GC.Collect(); } else { RemovePicturesFromSheet(sheet); sheet.CopyTo(resultExcelFile, sheet.Name); GC.Collect(); } } } resultExcelFile.SaveAs(resultExcelPath); Console.WriteLine("Done. Press any key to quit."); Console.ReadKey(); } } }