nit some refactoring in DrillingProgramService

This commit is contained in:
Фролов 2021-10-18 16:23:58 +05:00
parent 64d6d8c8f8
commit 6e9ab38d47

View File

@ -17,6 +17,8 @@ namespace AsbCloudInfrastructure.Services
private const int idFileCategoryDrillingProgramItems = 13;
private const int idFileCategoryDrillingProgram = 14;
private const int maxAllowedColumns = 256;
public DrillingProgramService(IFileService fileService, IWellService wellService)
{
this.fileService = fileService;
@ -71,28 +73,37 @@ namespace AsbCloudInfrastructure.Services
{
var resultExcelFile = new XLWorkbook(XLEventTracking.Disabled);
const int maxAllowedColumns = 256;
var filteredFileNames = excelFilesNames.Distinct();
foreach (var excelFileName in filteredFileNames)
{
using var sourceExcelFile = new XLWorkbook(excelFileName, XLEventTracking.Disabled);
using var workbookSrc = new XLWorkbook(excelFileName, XLEventTracking.Disabled);
foreach (var sheet in sourceExcelFile.Worksheets)
foreach (var sheet in workbookSrc.Worksheets)
{
var newSheetName = sheet.Name;
if(sheet.Visibility == XLWorksheetVisibility.Visible)
CopySheet(resultExcelFile, sheet);
}
}
resultExcelFile.SaveAs(resultExcelPath,
new SaveOptions { EvaluateFormulasBeforeSaving = true});
}
private static void CopySheet(XLWorkbook workbookDst, IXLWorksheet sheetSrc)
{
var newSheetName = sheetSrc.Name;
int index = 1;
if (resultExcelFile.Worksheets.Contains(newSheetName))
if (workbookDst.Worksheets.Contains(newSheetName))
{
var suffix = $"_{index++}";
if (newSheetName.Length + suffix.Length >= 31)
newSheetName = sheet.Name.Substring(0, (31 - suffix.Length));
newSheetName = sheetSrc.Name.Substring(0, (31 - suffix.Length));
newSheetName += suffix;
}
var imagesInfos = sheet.Pictures.Select(p => new ImageInfo
var imagesInfos = sheetSrc.Pictures.Select(p => new ImageInfo
{
Id = p.Id,
Data = p.ImageStream.GetBuffer(),
@ -103,11 +114,13 @@ namespace AsbCloudInfrastructure.Services
Top = p.Top
}).ToList();
if (sheet.Columns().Count() > maxAllowedColumns)
{
var resultSheet = resultExcelFile.Worksheets.Add(newSheetName);
IXLWorksheet resultSheet;
var rngData = GetCellsRange(sheet);
if (sheetSrc.Columns().Count() > maxAllowedColumns)
{
resultSheet = workbookDst.Worksheets.Add(newSheetName);
var rngData = GetCellsRange(sheetSrc);
rngData.CopyTo(resultSheet.Cell(1, 1));
@ -116,26 +129,17 @@ namespace AsbCloudInfrastructure.Services
for (int i = 1; i < lastRowWithData; i++)
{
resultSheet.Row(i).Height = sheet.Row(i).Height;
resultSheet.Column(i).Width = sheet.Column(i).Width;
resultSheet.Row(i).Height = sheetSrc.Row(i).Height;
resultSheet.Column(i).Width = sheetSrc.Column(i).Width;
}
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
}
else
{
RemovePicturesFromSheet(sheet);
var resultSheet = sheet.CopyTo(resultExcelFile, newSheetName);
RemovePicturesFromSheet(sheetSrc);
resultSheet = sheetSrc.CopyTo(workbookDst, newSheetName);
}
CopyImagesToAnotherSheet(imagesInfos, resultSheet);
}
}
}
resultExcelFile.SaveAs(resultExcelPath,
new SaveOptions { EvaluateFormulasBeforeSaving = true});
}
private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable<ImageInfo> imagesInfos,
IXLWorksheet resultSheet)