DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DrillTestReport/DrillTestReportMakerService.cs
Степанов Дмитрий 32a0678a56 Рефакторинг работы с excel
1. Использование новых методов расширения
2. Удалил неиспользуемый генератор файла DrillingProgramMaker
3. Поправлено название листа в ProcessMapReportTemplate
2024-02-07 09:33:00 +03:00

80 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data.DrillTestReport;
using ClosedXML.Excel;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services.DrillTestReport
{
public class DrillTestReportMakerService : IReportMakerService<DrillTestReportDataDto>
{
private readonly string templateName = "DrillTestReportTemplate.xlsx";
private readonly string sheetName = "Лист1";
private readonly int startRowNumber = 8;
public async Task<Stream> MakeReportAsync(DrillTestReportDataDto report, CancellationToken cancellationToken)
{
using var excelTemplateStream = await Assembly.GetExecutingAssembly().GetTemplateCopyStreamAsync(templateName, cancellationToken);
using var workbook = new XLWorkbook(excelTemplateStream);
AddToWorkbook(workbook, report);
MemoryStream memoryStream = new MemoryStream();
workbook.SaveAs(memoryStream, new SaveOptions { });
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
private void AddToWorkbook(XLWorkbook workbook, DrillTestReportDataDto report)
{
var drillTests = report.Data.Params;
if (!drillTests.Any())
return;
var sheet = workbook.GetWorksheet(sheetName);
sheet.Cell(4, 2).SetCellValue(report.Caption);
sheet.Cell(5, 2).SetCellValue(report.Date);
var stepWithMaxDepthSpeed = drillTests.MaxBy(p => p.DepthSpeed)!.Step;
var startDepth = report.Data.DepthStart;
var startDate = report.Data.TimeStampStart;
var rowNumber = startRowNumber;
foreach (var drillTestEntity in drillTests)
{
var endDepth = startDepth + (drillTestEntity.DepthDrillStep ?? 0);
var endDateTime = startDate.AddSeconds(drillTestEntity.TimeDrillStep ?? 0);
sheet.Cell(rowNumber, 2).SetCellValue(startDepth);
sheet.Cell(rowNumber, 3).SetCellValue(endDepth);
sheet.Cell(rowNumber, 4).SetCellValue(drillTestEntity.DepthDrillStep);
sheet.Cell(rowNumber, 5).SetCellValue(drillTestEntity.Workload);
sheet.Cell(rowNumber, 6).SetCellValue(drillTestEntity.Speed);
sheet.Cell(rowNumber, 7).SetCellValue(startDate.DateTime);
sheet.Cell(rowNumber, 8).SetCellValue(endDateTime.DateTime);
sheet.Cell(rowNumber, 9).SetCellValue(Math.Round((drillTestEntity.TimeDrillStep ?? 0) / (60 * 60), 2));
sheet.Cell(rowNumber, 10).SetCellValue(drillTestEntity.DepthSpeed);
if (drillTestEntity.Step == stepWithMaxDepthSpeed)
{
var currentCells = sheet.Row(rowNumber).Cells(1, 10);
currentCells.Style.Fill.BackgroundColor = XLColor.Yellow;
}
startDepth = endDepth;
startDate = endDateTime;
rowNumber++;
}
}
}
}