2023-09-29 16:48:59 +05:00
|
|
|
|
using ClosedXML.Excel;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2024-02-07 09:40:00 +05:00
|
|
|
|
using System.Linq;
|
2023-09-29 16:48:59 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure;
|
|
|
|
|
|
2024-02-07 09:40:00 +05:00
|
|
|
|
public static class XLExtentions
|
2023-09-29 16:48:59 +05:00
|
|
|
|
{
|
2024-02-07 09:40:00 +05:00
|
|
|
|
public static IXLWorksheet GetWorksheet(this IXLWorkbook workbook, string sheetName) =>
|
|
|
|
|
workbook.Worksheets.FirstOrDefault(ws => string.Equals(ws.Name.Trim(), sheetName.Trim(), StringComparison.CurrentCultureIgnoreCase))
|
2024-02-12 17:25:18 +05:00
|
|
|
|
?? throw new FileFormatException(string.Format(XLMessageTemplates.NotFoundSheetTemplate, sheetName));
|
2024-02-07 09:40:00 +05:00
|
|
|
|
|
|
|
|
|
public static IXLCell SetCellValue<T>(this IXLCell cell, T value)
|
|
|
|
|
{
|
|
|
|
|
if (typeof(T) == typeof(DateTime))
|
|
|
|
|
cell.Style.DateFormat.Format = "DD.MM.YYYY HH:MM:SS";
|
|
|
|
|
|
|
|
|
|
cell.Value = XLCellValue.FromObject(value);
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IXLCell SetHyperlink(this IXLCell cell, string link)
|
|
|
|
|
{
|
|
|
|
|
cell.SetHyperlink(new XLHyperlink(link));
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T? GetCellValue<T>(this IXLCell cell)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (cell.IsEmpty() && default(T) == null)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
return cell.GetValue<T>();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2024-02-12 17:25:18 +05:00
|
|
|
|
var message = string.Format(XLMessageTemplates.ProblemDetailsTemplate, cell.Worksheet.Name, cell.Address.RowNumber,
|
2024-02-08 14:50:14 +05:00
|
|
|
|
cell.Address.ColumnNumber, "Содержит некорректное значение");
|
|
|
|
|
throw new FileFormatException(message);
|
2024-02-07 09:40:00 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-29 16:48:59 +05:00
|
|
|
|
}
|