154

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.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Pengan
  • 1,813
  • 4
  • 15
  • 16

10 Answers10

284

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.

Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 14
    Important thing is here that you need to call this method after filling the worksheet because Worksheet.Dimension property will return null if there's no data in the worksheet. – Arkadiusz Kałkus Jan 23 '15 at 10:22
  • @Landeeyo Thanks it helps for me. It is important that setting width after filling the content. – Lanka Apr 10 '15 at 08:58
  • 4
    AutoFitColumns method was not working for me when i was providing no parameters, when i add min and max width to it, it works perfectly. – Zeeshan Ajmal Apr 29 '15 at 09:53
  • 4
    This absolutely the correct answer - Pangan should mark it as such. – GDB Nov 21 '15 at 14:10
  • 2
    If you have calculated cells you must call `Worksheet.Calculate()` first so that the value for those cells can be determined. – MikeH Jan 07 '19 at 19:45
  • Important thing if you are running your code in linux based docker container and don't install manually the System.Drawing package the AutoFitColumns call will maybe causing an Exception. – totesz09 Oct 05 '22 at 05:50
45

I have used this code with the version 3.1.3.0 of EPPlus and it is working:

worksheet.Column(1).AutoFit();

where a worksheet is the variable referencing the worksheet I have created in my code (not a class with a static method!).

Obviously you have to call this method after you have filled the columns.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Daniele Armanasco
  • 7,289
  • 9
  • 43
  • 55
28

Just wanted to point out you can fit cells with out specifying the range, just make sure to call this after you've formatted all columns etc:

worksheet.Cells.AutoFitColumns()
johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 1
    This worked for me. Do remember to call it after you've added the data. When I called it before data was added nothing happened. – mortb Nov 06 '19 at 11:38
16

I know this is an old question, but I use the code below and it seems to directly address what you have tried to do.

using (var xls = new ExcelPackage())
{
    var ws = xls.Workbook.Worksheets.Add("Some Name");

    //**Add Column Names to worksheet!**
    //**Add data to worksheet!**

    const double minWidth = 0.00;
    const double maxWidth = 50.00;

    ws.Cells.AutoFitColumns(minWidth, maxWidth);

    return pkg.GetAsByteArray();
}
TrailTrackers
  • 161
  • 1
  • 3
12

I know is a little bit late but I've had the same problem today. If you have a worksheet.DefaultColWidthdefined, it won't work. I've removed that line and added Worksheet.cells.AutoFitColumns(); and it works now.

wonea
  • 4,783
  • 17
  • 86
  • 139
Jose
  • 1,857
  • 1
  • 16
  • 34
  • 5
    `sheet.Cells.AutoFitColumns()` is simpler, thanks for the tip! In ClosedXml, you do `sheet.Columns().AdjustToContents()` – nawfal Jun 26 '16 at 07:03
6

It's working just fine for me.

Try:

ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");
wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();
ExcelPkg.SaveAs();
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
leiit
  • 97
  • 1
  • 8
5

The .NET Core as a successor of .NET doesn't support anymore the function autofit cells with EPPplus library.

worksheet.Cells.AutoFitColumns();

or

worksheet.Column(1).AutoFit();

causes exception:

"System.Drawing is not supported on this platform."

The System.Drawing assembly is dependent on GDI and Windows specific libraries which have to be replaced by another solution. The solution for this issue is to me unknown.

Ondrej Rozinek
  • 563
  • 8
  • 14
3

You will need to calculate the width. There is no autosizing function in the library that will work as you intend.

Autofitcolumn will not work with wrapped text and cells with formulas.

Look at http://epplus.codeplex.com/discussions/218294?ProjectName=epplus for examples of how you can solve the problem.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
ffffff01
  • 5,068
  • 11
  • 52
  • 61
  • http://epplus.codeplex.com/discussions/218294?ProjectName=epplus I cannot find any working solution for this problem. – Pengan Mar 28 '13 at 06:22
3

Had to use worksheet.Column(1).AutoFit(0); AutoFit() wasn't doing the trick.

xtds
  • 2,453
  • 2
  • 19
  • 12
1

I use this and is working well.

Dim objExcel As New ExcelPackage
Dim Sheet As ExcelWorksheet = objExcel.Workbook.Worksheets.Add("SheetName")
Sheet.Cells("B1:BN").AutoFitColumns()
Jhonny Nina
  • 579
  • 5
  • 5