Fixed columns and rows sizes in result file

This commit is contained in:
KharchenkoVV 2021-09-20 12:10:15 +05:00
parent 9ccde70b98
commit e33ba7207b

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Collections.Generic;
using System.Linq;
using ClosedXML.Excel;
using ClosedXML.Excel.Drawings;
namespace ConsoleApp1
{
@ -16,17 +17,16 @@ namespace ConsoleApp1
class Program
{
private static void RemovePicturesFromSheet(IXLWorksheet sheet)
private static void CopyImages(IXLWorksheet sourceSheet, IXLWorksheet resultSheet)
{
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);
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);
}
}
private static IXLRange GetCellsRange(IXLWorksheet sheet)
@ -45,10 +45,10 @@ namespace ConsoleApp1
//@"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\e2.xlsx",
//@"D:\excels\e3.xlsx",
//@"D:\excels\e4.XLSX",
//@"D:\excels\e5.XLSX",
@"D:\excels\e6.XLSX"
};
@ -62,13 +62,14 @@ namespace ConsoleApp1
{
using var sourceExcelFile = new XLWorkbook(sourceExcelPath);
Console.WriteLine($"Добавляется файл {Path.GetFileName(sourceExcelPath)}");
foreach (var sheet in sourceExcelFile.Worksheets)
{
// Если в файле очень много колонок и многие из них пустые -
// предлагаем удалить их вручную или пытаемся копировать
// ячейки попорядку (не красиво). Красиво не получится.
if (sheet.Columns().Count() > maxAllowedColumns &&
sheet.Columns().Count() / sheet.ColumnsUsed().Count() > 5)
sheet.Columns().Count() / sheet.ColumnsUsed().Count() > 3)
{
Console.WriteLine($"В файле {Path.GetFileName(sourceExcelPath)} " +
$"{sheet.Columns().Count() - sheet.ColumnsUsed().Count()} пустых колонок " +
@ -82,12 +83,22 @@ namespace ConsoleApp1
if (res != "y")
return;
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
//resultSheet.Cell(1, 1).Value = rngData;
var rngData = GetCellsRange(sheet);
RemovePicturesFromSheet(sheet);
rngData.CopyTo(resultSheet.Cell(1, 1));
var wsCopy = resultExcelFile.Worksheets.Add(sheet.Name);
wsCopy.Cell(1, 1).Value = rngData;
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;
}
CopyImages(sheet, resultSheet);
GC.Collect();
}
@ -96,22 +107,25 @@ namespace ConsoleApp1
else if (sheet.Columns().Count() < maxAllowedColumns &&
sheet.Columns().Count() / sheet.ColumnsUsed().Count() > 2)
{
RemovePicturesFromSheet(sheet);
var columnsToDelete = sheet.Columns().Skip(sheet.ColumnsUsed().Count());
var columnsToDelete = sheet.Columns()
.Skip(sheet.ColumnsUsed().Count());
foreach (var d in columnsToDelete)
d.Delete();
sheet.CopyTo(resultExcelFile, sheet.Name);
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
CopyImages(sheet, resultSheet);
GC.Collect();
}
// Если по колонкам все хорошо, копируем лист красиво.
else
{
RemovePicturesFromSheet(sheet);
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
CopyImages(sheet, resultSheet);
sheet.CopyTo(resultExcelFile, sheet.Name);
GC.Collect();
}
}