Automatic column width in EPPlus

How to make columns automatically width when texts in columns are long?

I am using this code

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

None of these methods work.

Are there any ways to make it work?

Note: some of my texts use Unicode.

+125
c # epplus
Mar 26 '13 at 16:55
source share
10 answers

Use AutoFitColumns , but you must specify the cells, I accept the whole worksheet:

Vb.net

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

FROM#

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

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

+232
Mar 26 '13 at 17:00
source share

I used this code with version 3.1.3.0 of EPPlus and it works:

 worksheet.Column(1).AutoFit(); 

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

Obviously, you should call this method after filling the columns .

+38
May 28 '13 at 15:52
source share

I know this is an old question, but I am using the code below and it seems to be directly related to what you were trying 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(); } 
+15
Jun 30 '16 at 14:20
source share

I know it's a little late, but I had the same problem today. If you have a worksheet.DefaultColWidth worksheet .DefaultColWidth, it will not work. I deleted this row and added Worksheet.cells.AutoFitColumns(); and it works now.

+10
Sep 18 '15 at 10:18
source share

I just want to point out that you can fit cells without specifying a range, just be sure to call it after you have formatted all the columns, etc .:

 worksheet.Cells.AutoFitColumns() 
+9
Oct 25 '18 at 16:36
source share

This works just fine for me.

Try:

 ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1"); wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns(); ExcelPkg.SaveAs(); 
+5
Jul 02 '18 at 9:13
source share

You will need to calculate the width. There is no autosave function in the library that will work the way you plan.

Autofitcolumn will not work with wrapped text and formula cells.

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

+3
Mar 26 '13 at 17:03
source share

When using worksheet.Column(1).AutoFit(0); AutoFit () did not perform the trick.

+2
Jul 23 '13 at 7:09
source share

I use this and work well.

 Dim objExcel As New ExcelPackage Dim Sheet As ExcelWorksheet = objExcel.Workbook.Worksheets.Add("SheetName") Sheet.Cells("B1:BN").AutoFitColumns() 
+1
Dec 05 '13 at 20:21
source share

Be careful, you must do this before saving, otherwise it will not work.

0
Feb 19 '19 at 11:05
source share



All Articles