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