Files
jdescopingtool/NEW/src/JdeScoping.ExcelIO/Formatting/ColumnFormatter.cs
T
Joseph Doherty 26ff8d9b4f Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/),
new .NET 10 Blazor solution (NEW/), OpenSpec specifications,
documentation, and project configuration.
2026-01-02 07:43:29 -05:00

55 lines
1.6 KiB
C#

using ClosedXML.Excel;
using JdeScoping.ExcelIO.Attributes;
namespace JdeScoping.ExcelIO.Formatting;
/// <summary>
/// Column width and number format utilities.
/// </summary>
public static class ColumnFormatter
{
/// <summary>
/// Applies column formatting based on attribute settings.
/// </summary>
/// <param name="column">The column to format.</param>
/// <param name="attr">The output column attribute with settings.</param>
public static void ApplyColumnFormat(IXLColumn column, OutputColumnAttribute attr)
{
// Set number format
column.Style.NumberFormat.Format = attr.Format;
// Handle wrap text
if (attr.WrapText)
{
column.Style.Alignment.WrapText = true;
}
// Handle width
if (attr.WrapText && !attr.AutoWidth)
{
// Wrapped columns with fixed width skip auto-fit
column.Width = attr.Width;
}
else if (attr.AutoWidth)
{
column.AdjustToContents();
column.Width *= ExcelFormats.DataPaddingFactor;
}
else
{
column.Width = attr.Width;
}
}
/// <summary>
/// Auto-fits a column with the specified padding factor.
/// </summary>
/// <param name="column">The column to auto-fit.</param>
/// <param name="paddingFactor">The padding factor to apply (e.g., 1.15 for 15% padding).</param>
public static void AutoFitWithPadding(IXLColumn column, double paddingFactor)
{
column.AdjustToContents();
column.Width *= paddingFactor;
}
}