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-09-20 12:10:15 +05:00
|
|
|
|
using ClosedXML.Excel.Drawings;
|
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-21 13:39:03 +05:00
|
|
|
|
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; }
|
|
|
|
|
}
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-09-21 13:39:03 +05:00
|
|
|
|
private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable<ImageInfo> imagesInfos,
|
2021-09-21 12:10:15 +05:00
|
|
|
|
IXLWorksheet resultSheet)
|
2021-09-17 15:41:55 +05:00
|
|
|
|
{
|
2021-09-21 13:39:03 +05:00
|
|
|
|
foreach (var image in imagesInfos)
|
2021-09-20 12:10:15 +05:00
|
|
|
|
{
|
2021-09-21 13:39:03 +05:00
|
|
|
|
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)
|
2021-09-21 13:39:03 +05:00
|
|
|
|
.WithSize(image.Width, image.Height)
|
|
|
|
|
.MoveTo(resultSheet.Cell(image.TopLeftCellAddress),
|
|
|
|
|
image.Left, image.Top);
|
2021-09-20 12:10:15 +05:00
|
|
|
|
}
|
2021-09-20 15:12:02 +05:00
|
|
|
|
|
|
|
|
|
return resultSheet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void RemovePicturesFromSheet(IXLWorksheet sheet)
|
|
|
|
|
{
|
2021-09-21 12:10:15 +05:00
|
|
|
|
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-09-17 15:41:55 +05:00
|
|
|
|
}
|
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-21 12:10:15 +05:00
|
|
|
|
//@"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",
|
2021-09-17 15:41:55 +05:00
|
|
|
|
@"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-21 12:10:15 +05:00
|
|
|
|
using var resultExcelFile = new XLWorkbook(XLEventTracking.Disabled);
|
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-21 12:10:15 +05:00
|
|
|
|
using var sourceExcelFile = new XLWorkbook(sourceExcelPath, XLEventTracking.Disabled);
|
2021-09-15 17:48:34 +05:00
|
|
|
|
|
2021-09-20 12:10:15 +05:00
|
|
|
|
Console.WriteLine($"Добавляется файл {Path.GetFileName(sourceExcelPath)}");
|
2021-09-21 12:10:15 +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-21 13:39:03 +05:00
|
|
|
|
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();
|
|
|
|
|
|
2021-09-21 12:10:15 +05:00
|
|
|
|
if (sheet.Columns().Count() > maxAllowedColumns)
|
2021-09-15 17:48:34 +05:00
|
|
|
|
{
|
2021-09-20 12:10:15 +05:00
|
|
|
|
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
|
|
|
|
|
|
2021-09-17 15:58:01 +05:00
|
|
|
|
var rngData = GetCellsRange(sheet);
|
2021-09-16 14:34:32 +05:00
|
|
|
|
|
2021-09-20 12:10:15 +05:00
|
|
|
|
rngData.CopyTo(resultSheet.Cell(1, 1));
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-21 12:10:15 +05:00
|
|
|
|
var lastRowWithData = rngData.LastRowUsed().RangeAddress
|
|
|
|
|
.LastAddress.RowNumber;
|
2021-09-20 12:10:15 +05:00
|
|
|
|
|
|
|
|
|
for (int i = 1; i < lastRowWithData; i++)
|
|
|
|
|
{
|
|
|
|
|
resultSheet.Row(i).Height = sheet.Row(i).Height;
|
|
|
|
|
resultSheet.Column(i).Width = sheet.Column(i).Width;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:39:03 +05:00
|
|
|
|
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
|
2021-09-17 15:58:01 +05:00
|
|
|
|
}
|
2021-09-17 15:41:55 +05:00
|
|
|
|
else
|
2021-09-21 13:39:03 +05:00
|
|
|
|
{
|
|
|
|
|
RemovePicturesFromSheet(sheet);
|
|
|
|
|
|
|
|
|
|
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
|
|
|
|
|
|
|
|
|
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
|
|
|
|
|
}
|
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-21 13:39:03 +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
|
|
|
|
}
|
|
|
|
|
}
|