DD.WellWorkover.Cloud/AsbCloudInfrastructure/XLExtentions.cs

47 lines
1.3 KiB
C#
Raw Normal View History

using ClosedXML.Excel;
using System;
using System.IO;
using System.Linq;
namespace AsbCloudInfrastructure;
public static class XLExtentions
{
public static IXLWorksheet GetWorksheet(this IXLWorkbook workbook, string sheetName) =>
workbook.Worksheets.FirstOrDefault(ws => string.Equals(ws.Name.Trim(), sheetName.Trim(), StringComparison.CurrentCultureIgnoreCase))
?? throw new FileFormatException(string.Format(XLMessageTemplates.NotFoundSheetTemplate, sheetName));
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
{
var message = string.Format(XLMessageTemplates.ProblemDetailsTemplate, cell.Worksheet.Name, cell.Address.RowNumber,
cell.Address.ColumnNumber, "Содержит некорректное значение");
throw new FileFormatException(message);
}
}
}