EPPlus
auto column width
C# library
spreadsheet manipulation
Excel automation

Auto column width in EPPlus

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In EPPlus, “auto column width” usually means calling AutoFitColumns() after you have written values and styles into the worksheet. EPPlus then measures the content and adjusts the width so text is visible without manual width settings. The important part is when you call it and what limitations you expect from server-side width calculation.

The Basic Call

The most common pattern is:

csharp
1using OfficeOpenXml;
2using System.IO;
3
4ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
5
6using var package = new ExcelPackage();
7var worksheet = package.Workbook.Worksheets.Add("Report");
8
9worksheet.Cells["A1"].Value = "Name";
10worksheet.Cells["B1"].Value = "Department";
11worksheet.Cells["A2"].Value = "Alexandra";
12worksheet.Cells["B2"].Value = "Platform Engineering";
13
14worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
15
16package.SaveAs(new FileInfo("report.xlsx"));

This tells EPPlus to inspect the populated range and size the columns based on the cell contents.

Auto-Fit Specific Columns

You do not have to auto-fit the whole used range. You can target a column or a smaller range:

csharp
worksheet.Column(1).AutoFit();
worksheet.Column(2).AutoFit();

or:

csharp
worksheet.Cells["A:B"].AutoFitColumns();

That is useful when some columns should stay fixed while others expand automatically.

Call It After Writing Values and Styles

Auto-fit should generally run after:

  • you populate the cells
  • you apply number formats
  • you apply fonts or style changes that affect width

For example:

csharp
1worksheet.Cells["A1"].Value = "Amount";
2worksheet.Cells["A2"].Value = 12345.67;
3worksheet.Cells["A2"].Style.Numberformat.Format = "#,##0.00";
4
5worksheet.Column(1).AutoFit();

If you call auto-fit too early, EPPlus measures incomplete content and the resulting width may be too small.

Set Minimum and Maximum Widths

AutoFitColumns can take bounds so columns do not become absurdly narrow or absurdly wide:

csharp
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(10, 40);

That means:

  • no auto-fitted column narrower than 10
  • no auto-fitted column wider than 40

This is especially helpful for exports with user-generated text such as comments or descriptions.

Practical Example With a Table

csharp
1using OfficeOpenXml;
2using System.IO;
3
4ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
5
6using var package = new ExcelPackage();
7var ws = package.Workbook.Worksheets.Add("Employees");
8
9ws.Cells["A1"].Value = "Id";
10ws.Cells["B1"].Value = "Name";
11ws.Cells["C1"].Value = "Role";
12
13ws.Cells["A2"].Value = 1;
14ws.Cells["B2"].Value = "Maya Chen";
15ws.Cells["C2"].Value = "Senior Developer";
16
17ws.Cells["A3"].Value = 2;
18ws.Cells["B3"].Value = "Noah Patel";
19ws.Cells["C3"].Value = "Product Manager";
20
21using (var header = ws.Cells["A1:C1"])
22{
23    header.Style.Font.Bold = true;
24}
25
26ws.Cells["A:C"].AutoFitColumns();
27
28package.SaveAs(new FileInfo("employees.xlsx"));

This gives a readable sheet without manually calculating widths yourself.

What Auto-Fit Is Not

Auto-fit is a measurement approximation based on what EPPlus can infer from the workbook content. It is not the same thing as opening the file in Excel and letting Excel render the sheet on screen with the exact installed fonts and UI environment.

In practice, that means auto-fit is usually good enough, but not always pixel-perfect.

This matters more when:

  • you use unusual fonts
  • cells contain wrapped text
  • merged cells are involved
  • formulas display values that differ from raw inputs

Common Pitfalls

The biggest pitfall is calling AutoFitColumns() before the worksheet actually contains the final values and styles.

Another common issue is assuming it will perfectly handle every layout case, especially merged cells and very long wrapped content. Auto-fit helps, but it is not a full layout engine.

People also sometimes call it on worksheet.Dimension before any cells exist. If nothing has been written yet, worksheet.Dimension can be null, so guard against that in generic export helpers.

Finally, extremely long text can produce very wide columns. Use min and max bounds if you want the sheet to stay readable.

Summary

  • Use AutoFitColumns() after writing your data and styles.
  • You can auto-fit the whole used range or only selected columns.
  • Call auto-fit late enough that number formats and fonts are already applied.
  • Use width bounds when needed to prevent extreme column sizes.
  • Expect a useful approximation, not perfect visual parity with Excel rendering in every edge case.

Course illustration
Course illustration

All Rights Reserved.