DD.WellWorkover.Cloud/ConsoleApp1/Program.cs

107 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//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<AsbCloudDbContext>()
// .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<string>();
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<string>
{
//@"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();
}
}
}