EPPlus
XLSX
cell width
Excel automation
C# programming

How to set XLSX cell width with EPPlus?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In EPPlus, width is configured at the column level, not per individual cell. This is an important Excel constraint: a column has one width value shared by all cells in that column. Developers often search for "set cell width" when they actually need to set a column width, auto-fit content, or wrap text. EPPlus supports all of these patterns and works well for generated reports, exports, and automated templates. The practical approach is to set predictable widths for key columns, then optionally auto-fit data-driven columns after writing content.

Core Sections

Set fixed column width

Use the worksheet Column(index).Width property.

csharp
1using OfficeOpenXml;
2
3ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
4using var package = new ExcelPackage();
5var ws = package.Workbook.Worksheets.Add("Report");
6
7ws.Cells[1, 1].Value = "Customer Name";
8ws.Cells[1, 2].Value = "Total";
9
10ws.Column(1).Width = 28; // column A
11ws.Column(2).Width = 12; // column B

Width units are based on Excel character metrics, not pixels.

Auto-fit after writing values

Auto-fit computes widths from current cell contents.

csharp
1ws.Cells[2, 1].Value = "Acme Incorporated";
2ws.Cells[2, 2].Value = 15234.78;
3
4ws.Cells[ws.Dimension.Address].AutoFitColumns();

If you call auto-fit before populating data, results will be wrong or unchanged.

Handle long text with wrapping

Sometimes widening columns too much hurts readability. Use wrapping and row height adjustment.

csharp
ws.Column(1).Width = 30;
ws.Cells[2, 1].Style.WrapText = true;
ws.Row(2).Height = 40;

This is useful for comments, descriptions, and notes.

Configure ranges and styling together

You can combine width, alignment, and number formats for clean exports.

csharp
ws.Column(2).Style.Numberformat.Format = "#,##0.00";
ws.Column(2).Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Right;

Apply styling after creating the sheet structure to keep template logic centralized.

Save output safely

Always save after all formatting operations and validate with a sample open in Excel or LibreOffice.

csharp
package.SaveAs(new FileInfo("report.xlsx"));

Automated tests can verify generated file exists and key widths are set as expected.

Common Pitfalls

  • Trying to set width on a single cell instead of the whole column.
  • Calling AutoFitColumns before writing data, leading to incorrect widths.
  • Using extremely large fixed widths that break printable report layouts.
  • Assuming Excel width units are pixels and expecting exact visual matches.
  • Ignoring text wrapping for long fields and forcing unreadable truncation.

Verification Workflow

After implementing the main approach, run a short verification loop that proves behavior on realistic and adversarial inputs. Start with a small happy-path sample that should always pass, then add one edge case and one failure case that should be rejected or handled gracefully. Capture concrete outputs instead of relying on visual inspection alone. For operational code, record one measurable signal such as runtime, memory use, or error count so you can compare before and after future refactors.

Use this quick template during local development and CI:

text
11. Prepare deterministic sample input
22. Run expected-success scenario
33. Run expected-edge scenario
44. Run expected-failure scenario
55. Assert output schema and key values
66. Record one performance or reliability metric

This discipline catches most regressions caused by dependency upgrades, environment differences, or hidden assumptions in helper functions. It also makes handoffs easier because another engineer can reproduce behavior quickly without reverse-engineering your intent from source code alone.

Deployment Notes

Before rolling this pattern into production, add one small automated regression check tied to your most critical user path. Keep the check deterministic and fast, and run it on every dependency or configuration change. This extra guardrail catches subtle behavior drift that static review often misses, especially when environments differ between local machines and CI runners.

Summary

With EPPlus, column width is the correct control point for what many call "cell width." Set fixed widths for stable report layouts, use auto-fit selectively after data is written, and combine wrapping or formatting for long text. Keep width logic close to template generation code so spreadsheets remain readable and consistent across data volumes.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.