Auto column width in EPPlus

前端 未结 10 1309
心在旅途
心在旅途 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:54

    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();
    }
    
    0 讨论(0)
  • 2020-12-23 00:56

    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.

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

    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()
    
    0 讨论(0)
  • 2020-12-23 01:01

    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()
    
    0 讨论(0)
提交回复
热议问题