Files
radzen-blazor/Radzen.Blazor/Documents/Spreadsheet/ColumnWidthConversion.cs
Atanas Korchev da70790c3e Measure auto fit column width against the rendered text
Replaces the character-count width estimate with Excel-faithful auto fit:
the displayed text (number format applied to the evaluated value) of the
column's populated cells is measured with canvas.measureText using the
active theme's cell font, so formulas, number formats, bold and
conditional formatting fit correctly in any theme. Empty columns keep
their width, merged cells are ignored, hidden rows are included and the
width is clamped to Excel's 255 character limit. The behavior was
verified against Excel via COM automation.

Auto fitted columns are tracked per column and persisted as bestFit in
XLSX; manual resize and undo clear the flag.
2026-07-05 17:27:51 +03:00

17 lines
560 B
C#

using System;
namespace Radzen.Documents.Spreadsheet;
#nullable enable
// XLSX column widths are in characters of the Normal font's maximum digit width (7px per character).
internal static class ColumnWidthConversion
{
// Excel caps column width at 255 characters; 255 * 7 converts back to exactly 255.
public const double MaxWidthInPixels = 255 * 7;
public static double PixelsToChars(double pixels) => Math.Round(pixels / 7.0, 8);
public static double CharsToPixels(double chars) => (256 * chars + Math.Truncate(128 / 7.0)) / 256 * 7;
}