forked from ddrilling/AsbCloudServer
Changed Images collection storage from temp sheet to memory
This commit is contained in:
parent
5d21d841a2
commit
68eca5fdef
@ -14,19 +14,32 @@ namespace ConsoleApp1
|
||||
// .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(IXLWorksheet sourceSheet,
|
||||
private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable<ImageInfo> imagesInfos,
|
||||
IXLWorksheet resultSheet)
|
||||
{
|
||||
foreach (var picture in sourceSheet.Pictures)
|
||||
foreach (var image in imagesInfos)
|
||||
{
|
||||
resultSheet.AddPicture(picture.ImageStream)
|
||||
var stream = new MemoryStream();
|
||||
stream.Write(image.Data, 0, image.Data.Length);
|
||||
|
||||
resultSheet.AddPicture(stream)
|
||||
.WithPlacement(XLPicturePlacement.Move)
|
||||
.WithSize(picture.Width, picture.Height)
|
||||
.MoveTo(resultSheet.Cell(picture.TopLeftCell.Address),
|
||||
picture.Left, picture.Top);
|
||||
.WithSize(image.Width, image.Height)
|
||||
.MoveTo(resultSheet.Cell(image.TopLeftCellAddress),
|
||||
image.Left, image.Top);
|
||||
}
|
||||
|
||||
return resultSheet;
|
||||
@ -49,27 +62,6 @@ namespace ConsoleApp1
|
||||
return rngData;
|
||||
}
|
||||
|
||||
// Изображения не переносятся при вызове метода sheet.CopyTo(). Падает с exception.
|
||||
// Для этого сначала все изображения переносятся на временный лист "PicsCopy",
|
||||
// затем удаляются из копируемого листа, копируемый лист копируется в лист-результат
|
||||
// и после этого из temp листа "PicsCopy" в лист-результат также копируются изображения.
|
||||
// Temp лист удаляется.
|
||||
private static void CopySheetToFile(IXLWorksheet currentSheet, XLWorkbook resultExcelFile)
|
||||
{
|
||||
var picturesCopySheet = resultExcelFile.AddWorksheet("PicsCopy");
|
||||
var copy = currentSheet.Pictures.Select(p => new { buff = p.ImageStream, });
|
||||
|
||||
CopyImagesToAnotherSheet(currentSheet, picturesCopySheet);
|
||||
|
||||
RemovePicturesFromSheet(currentSheet);
|
||||
|
||||
var resultSheet = currentSheet.CopyTo(resultExcelFile, currentSheet.Name);
|
||||
|
||||
CopyImagesToAnotherSheet(picturesCopySheet, resultSheet);
|
||||
|
||||
picturesCopySheet.Delete();
|
||||
}
|
||||
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
var sourceExcelPaths = new List<string>
|
||||
@ -98,6 +90,17 @@ namespace ConsoleApp1
|
||||
|
||||
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);
|
||||
@ -115,14 +118,20 @@ namespace ConsoleApp1
|
||||
resultSheet.Column(i).Width = sheet.Column(i).Width;
|
||||
}
|
||||
|
||||
CopyImagesToAnotherSheet(sheet, resultSheet);
|
||||
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
|
||||
}
|
||||
else
|
||||
CopySheetToFile(sheet, resultExcelFile);
|
||||
{
|
||||
RemovePicturesFromSheet(sheet);
|
||||
|
||||
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
||||
|
||||
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resultExcelFile.SaveAs(resultExcelPath, true, true);
|
||||
resultExcelFile.SaveAs(resultExcelPath);
|
||||
|
||||
Console.WriteLine("Done. Press any key to quit.");
|
||||
Console.ReadKey();
|
||||
|
Loading…
Reference in New Issue
Block a user