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.
Cause
In OfficeOpenXml.Core.AutofitHelper:
private const double AutoFilterArrowWidthPixels = 15d;
- On Windows 10/11 (96 DPI / 100% scaling), the native Excel filter dropdown arrow button is exactly 17 pixels wide.
- Native Excel AutoFit also includes inner cell padding / text margin between the text end and the button.
- 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.
Environment / EPPlus version / Spreadsheet application
Windows / 8.7.0 / Excel
Description
When applying
AutoFitColumns()on a range withAutoFilterenabled, 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.Cause
In
OfficeOpenXml.Core.AutofitHelper:15px / fontWidth, the resulting column is narrower than native Excel AutoFit.Steps to Reproduce
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, increaseAutoFilterArrowWidthPixelsfrom15dto at least17dor even a bit more.