forked from ddrilling/AsbCloudServer
Added images copying
This commit is contained in:
parent
1d54c1679e
commit
ddcb90870f
@ -17,16 +17,31 @@ namespace ConsoleApp1
|
||||
|
||||
class Program
|
||||
{
|
||||
private static void CopyImages(IXLWorksheet sourceSheet, IXLWorksheet resultSheet)
|
||||
private static IXLWorksheet CopyImagesToAnotherSheet(IXLWorksheet sourceSheet, IXLWorksheet resultSheet)
|
||||
{
|
||||
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);
|
||||
resultSheet.AddPicture(picture.ImageStream)
|
||||
.WithPlacement(XLPicturePlacement.Move)
|
||||
.WithSize(picture.Width, picture.Height)
|
||||
.MoveTo(resultSheet.Cell(picture.TopLeftCell.Address),
|
||||
picture.Left, picture.Top);
|
||||
}
|
||||
|
||||
return resultSheet;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private static IXLRange GetCellsRange(IXLWorksheet sheet)
|
||||
@ -38,17 +53,36 @@ namespace ConsoleApp1
|
||||
return rngData;
|
||||
}
|
||||
|
||||
// Изображения не переносятся при вызове метода sheet.CopyTo(). Падает с exception.
|
||||
// Для этого сначала все изображения переносятся на временный лист "PicsCopy",
|
||||
// затем удаляются из копируемого листа, копируемый лист копируется в лист-результат
|
||||
// и после этого из temp листа "PicsCopy" в лист-результат также копируются изображения.
|
||||
private static void CopySheetToFile(IXLWorksheet currentSheet, XLWorkbook resultExcelFile)
|
||||
{
|
||||
var picturesCopySheet = resultExcelFile.AddWorksheet("PicsCopy");
|
||||
|
||||
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>
|
||||
{
|
||||
//@"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\e1.xlsx",
|
||||
@"D:\excels\e2.xlsx",
|
||||
@"D:\excels\e3.xlsx",
|
||||
@"D:\excels\e4.XLSX",
|
||||
@"D:\excels\e5.XLSX",
|
||||
@"D:\excels\e6.XLSX"
|
||||
};
|
||||
|
||||
@ -84,7 +118,6 @@ namespace ConsoleApp1
|
||||
return;
|
||||
|
||||
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
|
||||
//resultSheet.Cell(1, 1).Value = rngData;
|
||||
|
||||
var rngData = GetCellsRange(sheet);
|
||||
|
||||
@ -98,7 +131,7 @@ namespace ConsoleApp1
|
||||
resultSheet.Column(i).Width = sheet.Column(i).Width;
|
||||
}
|
||||
|
||||
CopyImages(sheet, resultSheet);
|
||||
CopyImagesToAnotherSheet(sheet, resultSheet);
|
||||
|
||||
GC.Collect();
|
||||
}
|
||||
@ -113,18 +146,14 @@ namespace ConsoleApp1
|
||||
foreach (var d in columnsToDelete)
|
||||
d.Delete();
|
||||
|
||||
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
||||
|
||||
CopyImages(sheet, resultSheet);
|
||||
CopySheetToFile(sheet, resultExcelFile);
|
||||
|
||||
GC.Collect();
|
||||
}
|
||||
// Если по колонкам все хорошо, копируем лист красиво.
|
||||
else
|
||||
{
|
||||
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
||||
|
||||
CopyImages(sheet, resultSheet);
|
||||
CopySheetToFile(sheet, resultExcelFile);
|
||||
|
||||
GC.Collect();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user