2021-09-17 15:41:55 +05:00
|
|
|
|
//using AsbSaubReport;
|
2021-07-23 14:55:31 +05:00
|
|
|
|
//using AutoMapper;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using System;
|
2021-09-17 15:41:55 +05:00
|
|
|
|
using System.IO;
|
2021-09-15 17:48:34 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-09-15 10:43:46 +05:00
|
|
|
|
using System.Linq;
|
2021-09-17 15:41:55 +05:00
|
|
|
|
using ClosedXML.Excel;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
namespace ConsoleApp1
|
|
|
|
|
{
|
2021-09-15 10:43:46 +05:00
|
|
|
|
//var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
|
|
|
|
// .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
|
|
|
|
|
// .Options;
|
|
|
|
|
//var context = new AsbCloudDbContext(options);
|
|
|
|
|
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-09-17 15:41:55 +05:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-07-28 09:47:13 +05:00
|
|
|
|
|
2021-09-17 15:58:01 +05:00
|
|
|
|
private static IXLRange GetCellsRange(IXLWorksheet sheet)
|
|
|
|
|
{
|
|
|
|
|
var firstTableCell = sheet.FirstCellUsed();
|
|
|
|
|
var lastTableCell = sheet.LastCellUsed();
|
|
|
|
|
var rngData = sheet.Range(firstTableCell.Address, lastTableCell.Address);
|
|
|
|
|
|
|
|
|
|
return rngData;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 09:47:13 +05:00
|
|
|
|
static void Main(/*string[] args*/)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var sourceExcelPaths = new List<string>
|
|
|
|
|
{
|
2021-09-17 15:41:55 +05:00
|
|
|
|
//@"D:\excels\excel1.xlsx",
|
|
|
|
|
//@"D:\excels\excel2.xlsx"
|
2021-09-16 14:34:32 +05:00
|
|
|
|
//@"D:\excels\e1.xlsx",
|
2021-09-17 15:58:01 +05:00
|
|
|
|
@"D:\excels\e2.xlsx",
|
|
|
|
|
@"D:\excels\e3.xlsx",
|
2021-09-17 15:41:55 +05:00
|
|
|
|
@"D:\excels\e4.XLSX",
|
|
|
|
|
@"D:\excels\e5.XLSX",
|
|
|
|
|
@"D:\excels\e6.XLSX"
|
2021-09-15 17:48:34 +05:00
|
|
|
|
};
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
const string resultExcelPath = @"D:\excels\result.xlsx";
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
using var resultExcelFile = new XLWorkbook();
|
2021-09-15 10:43:46 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
const int maxAllowedColumns = 256;
|
2021-09-15 10:43:46 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
foreach(var sourceExcelPath in sourceExcelPaths)
|
2021-09-15 10:43:46 +05:00
|
|
|
|
{
|
2021-09-17 15:41:55 +05:00
|
|
|
|
using var sourceExcelFile = new XLWorkbook(sourceExcelPath);
|
2021-09-15 17:48:34 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
foreach (var sheet in sourceExcelFile.Worksheets)
|
2021-09-15 10:43:46 +05:00
|
|
|
|
{
|
2021-09-17 15:58:01 +05:00
|
|
|
|
// Если в файле очень много колонок и многие из них пустые -
|
|
|
|
|
// предлагаем удалить их вручную или пытаемся копировать
|
|
|
|
|
// ячейки попорядку (не красиво). Красиво не получится.
|
|
|
|
|
if (sheet.Columns().Count() > maxAllowedColumns &&
|
2021-09-17 15:41:55 +05:00
|
|
|
|
sheet.Columns().Count() / sheet.ColumnsUsed().Count() > 5)
|
2021-09-15 17:48:34 +05:00
|
|
|
|
{
|
2021-09-17 15:41:55 +05:00
|
|
|
|
Console.WriteLine($"В файле {Path.GetFileName(sourceExcelPath)} " +
|
|
|
|
|
$"{sheet.Columns().Count() - sheet.ColumnsUsed().Count()} пустых колонок " +
|
|
|
|
|
$"без записей. \n Если продолжить выполнение, форматирование выходного" +
|
|
|
|
|
$"листа может быть нарушено. \n Для сохранения структуры документа " +
|
|
|
|
|
$"рекомендуется прервать выполнение и удалить неиспользуемые колонки. \n" +
|
|
|
|
|
$" Продолжить выполнение? (y/n)");
|
2021-09-15 16:37:17 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
var res = Console.ReadLine();
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
if (res != "y")
|
|
|
|
|
return;
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-17 15:58:01 +05:00
|
|
|
|
var rngData = GetCellsRange(sheet);
|
2021-09-16 14:34:32 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
RemovePicturesFromSheet(sheet);
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
var wsCopy = resultExcelFile.Worksheets.Add(sheet.Name);
|
|
|
|
|
wsCopy.Cell(1, 1).Value = rngData;
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
GC.Collect();
|
|
|
|
|
}
|
2021-09-17 15:58:01 +05:00
|
|
|
|
// Если колонок в файле немного, но среди них также есть пустые -
|
|
|
|
|
// проще удалить ненужные колонки и скопировать лист красиво.
|
|
|
|
|
else if (sheet.Columns().Count() < maxAllowedColumns &&
|
|
|
|
|
sheet.Columns().Count() / sheet.ColumnsUsed().Count() > 2)
|
|
|
|
|
{
|
|
|
|
|
RemovePicturesFromSheet(sheet);
|
|
|
|
|
|
|
|
|
|
var columnsToDelete = sheet.Columns().Skip(sheet.ColumnsUsed().Count());
|
|
|
|
|
|
|
|
|
|
foreach (var d in columnsToDelete)
|
|
|
|
|
d.Delete();
|
|
|
|
|
|
|
|
|
|
sheet.CopyTo(resultExcelFile, sheet.Name);
|
|
|
|
|
GC.Collect();
|
|
|
|
|
}
|
|
|
|
|
// Если по колонкам все хорошо, копируем лист красиво.
|
2021-09-17 15:41:55 +05:00
|
|
|
|
else
|
2021-09-16 14:34:32 +05:00
|
|
|
|
{
|
2021-09-17 15:41:55 +05:00
|
|
|
|
RemovePicturesFromSheet(sheet);
|
|
|
|
|
|
|
|
|
|
sheet.CopyTo(resultExcelFile, sheet.Name);
|
|
|
|
|
GC.Collect();
|
2021-09-16 14:34:32 +05:00
|
|
|
|
}
|
2021-09-15 10:43:46 +05:00
|
|
|
|
}
|
2021-09-15 12:00:46 +05:00
|
|
|
|
}
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-17 15:41:55 +05:00
|
|
|
|
resultExcelFile.SaveAs(resultExcelPath);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2021-04-08 17:54:02 +05:00
|
|
|
|
Console.WriteLine("Done. Press any key to quit.");
|
|
|
|
|
Console.ReadKey();
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|