Skip to content

AutoFitColumns with AutoFilter allocates insufficient width for dropdown arrow (header text collides with button) #2486

Description

@lievendf

Environment / EPPlus version / Spreadsheet application

Windows / 8.7.0 / Excel

Description

When applying AutoFitColumns() on a range with AutoFilter enabled, the calculated column widths are too narrow for columns whose width is determined by the header text. When opened in Excel, the header text sits directly against the left edge of the filter dropdown button with 0px margin/padding.

Image

Cause

In OfficeOpenXml.Core.AutofitHelper:

private const double AutoFilterArrowWidthPixels = 15d;
  1. On Windows 10/11 (96 DPI / 100% scaling), the native Excel filter dropdown arrow button is exactly 17 pixels wide.
  2. Native Excel AutoFit also includes inner cell padding / text margin between the text end and the button.
  3. Because EPPlus only adds 15px / fontWidth, the resulting column is narrower than native Excel AutoFit.

Steps to Reproduce

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.SystemDrawing.Text;

ExcelPackage.License.SetNonCommercialPersonal("NonCommercial");

using (var package = new ExcelPackage(new FileInfo("autofilter_autofit_test.xlsx")))
{
    package.Settings.TextSettings.PrimaryTextMeasurer = new SystemDrawingTextMeasurer();
    package.Workbook.DefaultThemeVersion = 166925; // Calibri

    var ws = package.Workbook.Worksheets.Add("AutoFilterTest");

    ws.Cells["A1"].Value = "Text";
    ws.Cells["B1"].Value = "Number";
    ws.Cells["C1"].Value = "decField";
    ws.Cells["D1"].Value = "dateField";
    ws.Cells["E1"].Value = "logField";
    ws.Cells["A1:E1"].Style.Font.Bold = true;

    ws.Cells["A2"].Value = "Row 1 Alpha";
    ws.Cells["B2"].Value = 42;
    ws.Cells["C2"].Value = 123.45;
    ws.Cells["D2"].Value = "01/15/2026";
    ws.Cells["E2"].Value = "yes";

    ws.Cells["A3"].Value = "Row 2 Beta";
    ws.Cells["B3"].Value = 99;
    ws.Cells["C3"].Value = 678.90;
    ws.Cells["D3"].Value = "02/20/2026";
    ws.Cells["E3"].Value = "no";

    ws.Cells["A1:E3"].AutoFilter = true;
    ws.Cells["A1:E3"].AutoFitColumns(2.0);

    package.Save();
}

Expected Behavior

Column width should reserve space for both the 17px arrow button and some padding, more closely matching native Excel AutoFit.

Suggested Fix

In AutofitHelper.cs, increase AutoFilterArrowWidthPixels from 15d to at least 17d or even a bit more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions