Auto column width in EPPlus

Pengan picture Pengan · Mar 26, 2013 · Viewed 95k times · Source

How to make columns to be auto width when texts in columns are long?

I use this code

 Worksheet.Column(colIndex).AutoFitColumn() 'on all columns'
 Worksheet.cells.AutoFitColumns()
 Worksheet.Column(colIndex).BestFit = True  'on all columns'

None of these methods are working

Are there any ways to make it work?

Note: Some of my texts use Unicode.

Answer

Tim Schmelter picture Tim Schmelter · Mar 26, 2013

Use AutoFitColumns, but you have to specify the cells, i assume the entire worksheet:

VB.NET

Worksheet.Cells(Worksheet.Dimension.Address).AutoFitColumns()

C#

Worksheet.Cells[Worksheet.Dimension.Address].AutoFitColumns();

Please note you need to call this method after filling the worksheet.