Auto column width in EPPlus

前端 未结 10 1308
心在旅途
心在旅途 2020-12-23 00:31

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\'
         


        
相关标签:
10条回答
  • 2020-12-23 00:39

    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.

    0 讨论(0)
  • 2020-12-23 00:41

    It's working just fine for me.

    Try:

    ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");
    wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();
    ExcelPkg.SaveAs();
    
    0 讨论(0)
  • 2020-12-23 00:50

    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.

    0 讨论(0)
  • 2020-12-23 00:52

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

    0 讨论(0)
  • 2020-12-23 00:53

    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.

    0 讨论(0)
  • 2020-12-23 00:54

    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.

    0 讨论(0)
提交回复
热议问题