Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofit Row Height of Merged Cell in EPPlus

Tags:

c#

excel

epplus

I'm using EPPlus and C# and trying to autosize/autofit the height of a row to accommodate the height needed to show all of the contents of a merged cell with text wrapping. However no matter what I try the text always truncates. Since I'm repeating this process with various text sizes on various worksheets, I don't want to hard code the row height (except to enforce a minimum height for the row). If possible I'd like to do this within EPPlus/C#.

With the cells A2:E2 merged and WrapText = true:

Cell with Text Truncated

enter image description here

Here's what it should look like with desired Cell Height

enter image description here

Here's my relevant and short C# code

Int32 intToCol;
intToCol = 5;
eppWorksheet.Cells[2, 1, 2, intToCol].Merge = true;
eppWorksheet.Cells[2, 1].Style.WrapText = true; 
//Check if at the minimum height. If not, resize the row
if (eppWorksheet.Row(2).Height < 35.25)
{
    eppWorksheet.Row(2).Height = 35.25;
}

I've looked at Autofit rows in EPPlus and it didn't seem to directly answer my question unless I'm reading it wrong.

like image 857
cmbarnett87 Avatar asked Jan 13 '17 16:01

cmbarnett87


People also ask

How do you AutoFit row height in merged cells?

When a group of cells in a row are merged and the text wraps to two or more lines, double-clicking the row border just to the left of column A (or Format>Row>Autofit) auto-heights the row to one line of text. Either of these actions should heighten the row to fit the text in the merged cells.

Why is the AutoFit row height not working?

Cause. In Excel, you cannot use the AutoFit feature on a column that contains a cell merged with cells in other columns. Likewise, you cannot use AutoFit on a row that contains a cell merged with cells in other rows.

How do I resize a merged cell?

To do this, position your mouse pointer over the bottom of the row until a double arrowed pointer appears. Then hold down the left mouse button and drag downward. Release the left mouse button when the row is the desired height.

How do I make row height fit text in Excel?

On the Home tab, in the Cells group, click Format. Under Cell Size, do one of the following: To automatically adjust the row height, click AutoFit Row Height. To specify a row height, click Row Height, and then type the row height that you want in the Row height box.


Video Answer


2 Answers

I have used a workaround for this and I a had print area A:Q.

  1. I copied merged cells value to column z.
  2. set width of column z to merge cells width.
  3. Then set auto row height true in format.
  4. Hide the z column.
  5. Set print area A:Q

Cons: There are duplicate data. But we are okay since report is printing and not print z column.

Pros: Row height works correctly not like calculation method.

like image 97
cdev Avatar answered Oct 11 '22 08:10

cdev


Here is the solution in a reusable method. Pass in the text value, font used for the cell, summed width of the columns merged, and receive back the row height. Set the row height with the result.

Use of Method

eppWorksheet.Row(2).Height = MeasureTextHeight(cell.Value, cell.Style.Font, [enter the SUM of column widths A-E]);

Reuseable Method

    public double MeasureTextHeight(string text, ExcelFont font, double width)
    {
        if (text.IsNullOrEmpty()) return 0.0;
        var bitmap = _bitmap ?? (_bitmap = new Bitmap(1, 1));
        var graphics = _graphics ?? (_graphics = Graphics.FromImage(bitmap));

        var pixelWidth = Convert.ToInt32(width * 7);  //7 pixels per excel column width
        var fontSize = font.Size * 1.01f;
        var drawingFont = new Font(font.Name, fontSize);
        var size = graphics.MeasureString(text, drawingFont, pixelWidth, new StringFormat { FormatFlags = StringFormatFlags.MeasureTrailingSpaces });

        //72 DPI and 96 points per inch.  Excel height in points with max of 409 per Excel requirements.
        return Math.Min(Convert.ToDouble(size.Height) * 72 / 96, 409);
    }
like image 31
Ben Gripka Avatar answered Oct 11 '22 08:10

Ben Gripka