diff --git a/AsbCloudInfrastructure/Services/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgramService.cs index 783a34d7..298c9666 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgramService.cs @@ -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,65 +73,16 @@ 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; - int index = 1; - - if (resultExcelFile.Worksheets.Contains(newSheetName)) - { - var suffix = $"_{index++}"; - if (newSheetName.Length + suffix.Length >= 31) - newSheetName = sheet.Name.Substring(0, (31 - suffix.Length)); - newSheetName += suffix; - } - - 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(newSheetName); - - var rngData = GetCellsRange(sheet); - - rngData.CopyTo(resultSheet.Cell(1, 1)); - - var lastRowWithData = rngData.LastRowUsed().RangeAddress - .LastAddress.RowNumber; - - for (int i = 1; i < lastRowWithData; i++) - { - resultSheet.Row(i).Height = sheet.Row(i).Height; - resultSheet.Column(i).Width = sheet.Column(i).Width; - } - - CopyImagesToAnotherSheet(imagesInfos, resultSheet); - } - else - { - RemovePicturesFromSheet(sheet); - - var resultSheet = sheet.CopyTo(resultExcelFile, newSheetName); - - CopyImagesToAnotherSheet(imagesInfos, resultSheet); - } + if(sheet.Visibility == XLWorksheetVisibility.Visible) + CopySheet(resultExcelFile, sheet); } } @@ -137,6 +90,57 @@ namespace AsbCloudInfrastructure.Services new SaveOptions { EvaluateFormulasBeforeSaving = true}); } + private static void CopySheet(XLWorkbook workbookDst, IXLWorksheet sheetSrc) + { + var newSheetName = sheetSrc.Name; + int index = 1; + + if (workbookDst.Worksheets.Contains(newSheetName)) + { + var suffix = $"_{index++}"; + if (newSheetName.Length + suffix.Length >= 31) + newSheetName = sheetSrc.Name.Substring(0, (31 - suffix.Length)); + newSheetName += suffix; + } + + var imagesInfos = sheetSrc.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(); + + IXLWorksheet resultSheet; + + if (sheetSrc.Columns().Count() > maxAllowedColumns) + { + resultSheet = workbookDst.Worksheets.Add(newSheetName); + + var rngData = GetCellsRange(sheetSrc); + + rngData.CopyTo(resultSheet.Cell(1, 1)); + + var lastRowWithData = rngData.LastRowUsed().RangeAddress + .LastAddress.RowNumber; + + for (int i = 1; i < lastRowWithData; i++) + { + resultSheet.Row(i).Height = sheetSrc.Row(i).Height; + resultSheet.Column(i).Width = sheetSrc.Column(i).Width; + } + } + else + { + RemovePicturesFromSheet(sheetSrc); + resultSheet = sheetSrc.CopyTo(workbookDst, newSheetName); + } + CopyImagesToAnotherSheet(imagesInfos, resultSheet); + } + private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable imagesInfos, IXLWorksheet resultSheet) {