繁体   English   中英

自动拟合后获取excel行的高度,然后增加它 Epplus

[英]Getting the height of excel row after auto-fitting and then increase it Epplus

我试图在自动拟合它们后增加行的高度。 例如:第一行高度为 15

我通过以下方式自动调整它们:

            //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;               
          }

所以第一行高度现在是:30 然后,我想通过在循环中添加这段代码来增加每一行的大小

            double x = sheet.Row(row).Height;
            sheet.Row(row).Height = 1.25 * x;

问题是 Excel 在打开 Excel 文件时进行自动拟合,因此 EPPlus 不会获得更新的行高信息,而是旧信息。

所以行高现在是 18.75 (15 * 1.25) 而不是 37.5 (30 * 1.25)

你不能。 正如您所说的“Excel 在打开 Excel 文件时进行自动拟合” - 所以在 EPPlus 处理该文件很久之后。 您所做的(并且可以做的)就是告诉 Excel 相应地调整高度。

(这与列不同:EPPlus 可以执行sheet.Column(column).AutoFit();并将宽度设置为适当的值 - 但不幸的是,不适用于行高。也许这是在新的商业版本 EPPlus 中添加的?)

我认为您的选择是做一些实验,并尝试在 EPPlus 中自己计算和设置高度。

或者您在 Excel 文件中运行 VBA 脚本来完成这项工作 - 这没问题,它是这样的:

    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

当然,当您不允许在您的环境中运行宏时,这可能不是您的选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM