Files
PIbd-32_BuslaevRoman_KOP/ReportPlugin.ExcelBigText/BigTextExcelReport.cs
2025-10-21 13:17:55 +04:00

41 lines
1.5 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 ClosedXML.Excel;
using System;
using System.Collections.Generic;
using ComponentOrientedPlatform.Abstractions;
namespace ReportPlugin.ExcelBigText;
public sealed class BigTextExcelReport : IReportDocumentWithContextTextsContract
{
public string DocumentFormat => "xlsx";
public Task CreateDocumentAsync(string filePath, string header, List<string> paragraphs)
{
if (string.IsNullOrWhiteSpace(filePath)) throw new ArgumentNullException(nameof(filePath));
if (string.IsNullOrWhiteSpace(header)) throw new ArgumentNullException(nameof(header));
if (paragraphs is null) throw new ArgumentNullException(nameof(paragraphs));
if (paragraphs.Count == 0) throw new ArgumentOutOfRangeException(nameof(paragraphs));
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet("Текст");
ws.Cell(1, 1).Value = header;
ws.Range(1, 1, 1, 1).Merge();
ws.Cell(1, 1).Style.Font.SetBold().Font.SetFontSize(18);
ws.Cell(1, 1).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
int row = 3;
foreach (var p in paragraphs)
{
if (p is null) throw new ArgumentNullException(nameof(paragraphs), "В списке абзацев есть null.");
ws.Cell(row, 1).Value = p;
ws.Cell(row, 1).Style.Alignment.WrapText = true;
row++;
}
ws.Column(1).Width = 120;
wb.SaveAs(filePath);
return Task.CompletedTask;
}
}