Autofit in EPPlus

I cannot find a way to auto-adjust row height using the EPPlus Excel library. When I used Excel Interop, I could do it sheet.Rows.AutoFit(). I am viewing the interface using ILSpy, but so far I have not found anything useful. Is there a way around the problem, or am I missing something?

UPDATE : line 4 gives me problems:

enter image description here

I tried doing sheet.Row (4) .CustomHeight = false, but that didn't work ... I also tried setting the property before and after loading the contents of the cell, and it's still the same. It adjusts the line height to a different value, but this is not very good. Perhaps EPPlus has problems measuring line height? I know that I had a lot of problems with this when I messed up some old GDI + code in which I needed to measure strings ...

+3
source share
2 answers

, CustomHeight , , false. , Excel () . , false, , false.

, , , , EPPlus, Excel , . , , AutoFitColumn.

:

[TestMethod]
public void Row_Height_Test()
{
    //http://stackoverflow.com/questions/31496172/autofit-rows-in-epplus
    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]
    {
        new DataColumn("Col1", typeof (int)),
        new DataColumn("Col2", typeof (int)),
        new DataColumn("Col3", typeof (int))
    });


    for (var i = 0; i < 20; i++)
    {
        var row = datatable.NewRow();
        row[0] = i;
        row[1] = i * 10;
        row[2] = i * 100;
        datatable.Rows.Add(row);
    }

    var existingFile2 = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile2.Exists)
        existingFile2.Delete();

    using (var package = new ExcelPackage(existingFile2))
    {
        //Add the data
        var ws = package.Workbook.Worksheets.Add("Sheet1");
        ws.Cells.LoadFromDataTable(datatable, true);

        //CustomHeight is set to false by default on all rows and giving such a large font 
        //will cause it to autosize to a bigger height by Excel when first opened
        ws.Row(1).Style.Font.Size = 20;

        //Setting the height manually will automatically set CustomHeight to true
        //preventing excel from automatically setting the height
        ws.Row(2).Height = 30;
        ws.Row(2).Style.Font.Size = 20;

        //row 1 height will be around 26 when opened in Excel (but cannot know that now), 
        //row 2 height will be 30 as set above, 
        //row 3 height will be the DefaultHeight (usually 15) of the worksheet since it can fit the default font
        Console.WriteLine("{{{0} : {1}}}", ws.Row(1).Height, ws.Row(1).CustomHeight);
        Console.WriteLine("{{{0} : {1}}}", ws.Row(2).Height, ws.Row(2).CustomHeight);
        Console.WriteLine("{{{0} : {1}}}", ws.Row(3).Height, ws.Row(3).CustomHeight);

        //Save the file
        package.Save();
    }
}

:

{15 : False}
{30 : True}
{15 : False}

Excel rendered output

+7

, , : . :

( "" )

      // Increase the height of the header row by .5
      // (The height is already at 2 times the default value)
      wsDT.Row(1).Height *= 1.5;

      // Column index to Notes, Anniversary Month and Day
      //   add "1" since column index is relative to 0
      int colIdx = dt.Columns.IndexOf("Notes") + 1;

      // Set the column width 
      // This is a long text field - so width and word wrap are set
      wsDT.Column(colIdx).Width = 50;
      wsDT.Column(colIdx).Style.WrapText = true;

      // Set width of anniversary month column
      // Purpose here is to wrap header text 
      colIdx = dt.Columns.IndexOf("Lease Anniversary Month") + 1;
      wsDT.Column(colIdx).Width = 15;
      wsDT.Column(colIdx).Style.WrapText = true;

, ""

+4

All Articles