DD.WellWorkover.Cloud/ConsoleApp1/Program.cs

142 lines
4.8 KiB
C#
Raw Normal View History

//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<AsbCloudDbContext>()
// .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<ImageInfo> imagesInfos,
IXLWorksheet resultSheet)
{
foreach (var image in imagesInfos)
{
var stream = new MemoryStream();
stream.Write(image.Data, 0, image.Data.Length);
resultSheet.AddPicture(stream)
2021-09-20 15:12:02 +05:00
.WithPlacement(XLPicturePlacement.Move)
.WithSize(image.Width, image.Height)
.MoveTo(resultSheet.Cell(image.TopLeftCellAddress),
image.Left, image.Top);
}
2021-09-20 15:12:02 +05:00
return resultSheet;
}
private static void RemovePicturesFromSheet(IXLWorksheet sheet)
{
var filteredPics = sheet.Pictures.Select(p => p.Name).Distinct().ToList();
2021-09-20 15:12:02 +05:00
foreach (var n in filteredPics)
sheet.Pictures.Delete(n);
}
2021-07-28 09:47:13 +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*/)
{
var sourceExcelPaths = new List<string>
{
//@"D:\excels\excel1.xlsx",
//@"D:\excels\excel2.xlsx"
//@"D:\excels\e1.xlsx",
2021-09-20 15:12:02 +05:00
@"D:\excels\e2.xlsx",
@"D:\excels\e3.xlsx",
@"D:\excels\e4.XLSX",
@"D:\excels\e5.XLSX",
@"D:\excels\e6.XLSX"
};
2021-08-29 17:25:16 +05:00
const string resultExcelPath = @"D:\excels\result.xlsx";
2021-08-29 17:25:16 +05:00
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);
}
}
}
2021-08-29 17:25:16 +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-07 18:01:56 +05:00
}
}