forked from ddrilling/AsbCloudServer
Added images copying
This commit is contained in:
parent
e33ba7207b
commit
d9f53e51cf
@ -17,16 +17,31 @@ namespace ConsoleApp1
|
|||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
private static void CopyImages(IXLWorksheet sourceSheet, IXLWorksheet resultSheet)
|
private static IXLWorksheet CopyImagesToAnotherSheet(IXLWorksheet sourceSheet, IXLWorksheet resultSheet)
|
||||||
{
|
{
|
||||||
foreach (var picture in sourceSheet.Pictures)
|
foreach (var picture in sourceSheet.Pictures)
|
||||||
{
|
{
|
||||||
var newImage = resultSheet.AddPicture(picture.ImageStream)
|
resultSheet.AddPicture(picture.ImageStream)
|
||||||
.WithPlacement(XLPicturePlacement.FreeFloating)
|
.WithPlacement(XLPicturePlacement.Move)
|
||||||
.WithSize(picture.Width, picture.Height)
|
.WithSize(picture.Width, picture.Height)
|
||||||
.MoveTo(resultSheet.Cell(picture.TopLeftCell.Address),
|
.MoveTo(resultSheet.Cell(picture.TopLeftCell.Address),
|
||||||
picture.Left, picture.Top);
|
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)
|
private static IXLRange GetCellsRange(IXLWorksheet sheet)
|
||||||
@ -38,17 +53,36 @@ namespace ConsoleApp1
|
|||||||
return rngData;
|
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*/)
|
static void Main(/*string[] args*/)
|
||||||
{
|
{
|
||||||
var sourceExcelPaths = new List<string>
|
var sourceExcelPaths = new List<string>
|
||||||
{
|
{
|
||||||
//@"D:\excels\excel1.xlsx",
|
//@"D:\excels\excel1.xlsx",
|
||||||
//@"D:\excels\excel2.xlsx"
|
//@"D:\excels\excel2.xlsx"
|
||||||
//@"D:\excels\e1.xlsx",
|
@"D:\excels\e1.xlsx",
|
||||||
//@"D:\excels\e2.xlsx",
|
@"D:\excels\e2.xlsx",
|
||||||
//@"D:\excels\e3.xlsx",
|
@"D:\excels\e3.xlsx",
|
||||||
//@"D:\excels\e4.XLSX",
|
@"D:\excels\e4.XLSX",
|
||||||
//@"D:\excels\e5.XLSX",
|
@"D:\excels\e5.XLSX",
|
||||||
@"D:\excels\e6.XLSX"
|
@"D:\excels\e6.XLSX"
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -84,7 +118,6 @@ namespace ConsoleApp1
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
|
var resultSheet = resultExcelFile.Worksheets.Add(sheet.Name);
|
||||||
//resultSheet.Cell(1, 1).Value = rngData;
|
|
||||||
|
|
||||||
var rngData = GetCellsRange(sheet);
|
var rngData = GetCellsRange(sheet);
|
||||||
|
|
||||||
@ -98,7 +131,7 @@ namespace ConsoleApp1
|
|||||||
resultSheet.Column(i).Width = sheet.Column(i).Width;
|
resultSheet.Column(i).Width = sheet.Column(i).Width;
|
||||||
}
|
}
|
||||||
|
|
||||||
CopyImages(sheet, resultSheet);
|
CopyImagesToAnotherSheet(sheet, resultSheet);
|
||||||
|
|
||||||
GC.Collect();
|
GC.Collect();
|
||||||
}
|
}
|
||||||
@ -113,18 +146,14 @@ namespace ConsoleApp1
|
|||||||
foreach (var d in columnsToDelete)
|
foreach (var d in columnsToDelete)
|
||||||
d.Delete();
|
d.Delete();
|
||||||
|
|
||||||
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
CopySheetToFile(sheet, resultExcelFile);
|
||||||
|
|
||||||
CopyImages(sheet, resultSheet);
|
|
||||||
|
|
||||||
GC.Collect();
|
GC.Collect();
|
||||||
}
|
}
|
||||||
// Если по колонкам все хорошо, копируем лист красиво.
|
// Если по колонкам все хорошо, копируем лист красиво.
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var resultSheet = sheet.CopyTo(resultExcelFile, sheet.Name);
|
CopySheetToFile(sheet, resultExcelFile);
|
||||||
|
|
||||||
CopyImages(sheet, resultSheet);
|
|
||||||
|
|
||||||
GC.Collect();
|
GC.Collect();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user