Reputation: 89
I'm trying to increase the height of rows after auto fitting them. For example: first row height is 15
I autofit them by:
//The first row
var start = sheet.Dimension.Start;
//The last row
var end = sheet.Dimension.End;
for (int row = start.Row; row <= end.Row; row++)
{
//This makes Excel auto fit the row but content
sheet.Row(row).CustomHeight = true;
}
So the first row height is now :30 Then, I want to increase each row size by adding this code in the loop
double x = sheet.Row(row).Height;
sheet.Row(row).Height = 1.25 * x;
The Problem is Excel doing the auto fitting when opening the Excel file so EPPlus doesn't get the updated row height information but the old infomation.
So the row height is now 18.75 (15 * 1.25) instead of 37.5 (30 * 1.25)
Upvotes: 0
Views: 834
Reputation: 769
You cannot. As you say "Excel doing the auto fitting when opening the Excel file" - so that is long after EPPlus has been working on that file. All you do (and can do) is to tell Excel to adjust the height accordingly.
(That is different with columns: EPPlus can perform sheet.Column(column).AutoFit();
and set the width to an appropriate value - but not for row heights, unfortunately. Mayby this is added in the new, commercial version of EPPlus?)
I think your option is do some experimenting and try to calculate and set the height yourself in EPPlus.
Or you run a VBA Script in your Excel File to do the job - this is no problem, it goes like this:
With Sheets("Formular").Range(Cells(14, 1), Cells(14 + i - 1, 4))
.EntireRow.AutoFit
.VerticalAlignment = xlTop
For Each r In .Rows
r.RowHeight = r.Height + 5
Next r
End With
This might not be an option for you, of course, when you are not allowed to run Macros in your environment.
Upvotes: 1