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,65 +73,16 @@ 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)
int index = 1; CopySheet(resultExcelFile, sheet);
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);
}
} }
} }
@ -137,6 +90,57 @@ namespace AsbCloudInfrastructure.Services
new SaveOptions { EvaluateFormulasBeforeSaving = true}); 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<ImageInfo> imagesInfos, private static IXLWorksheet CopyImagesToAnotherSheet(IEnumerable<ImageInfo> imagesInfos,
IXLWorksheet resultSheet) IXLWorksheet resultSheet)
{ {