epplus:将列的wraptext样式设置为true后,如何获得行的高度?

时间:2013-09-03 14:09:23

标签: epplus

在我设置了一个列的WrapText = true后,我想看看该行的新高度是什么(即文本是否换行,有多少行)。似乎行的Height属性未更新。

        ExcelPackage pkg = new ExcelPackage();
        ExcelWorksheet sheet = pkg.Workbook.Worksheets.Add("Test");

        // height is 15.0
        double heightBefore = sheet.Row(1).Height;


        sheet.Cells[1, 1].Value = "Now is the time for all good men to come to the aid of their country";

        ExcelColumn col = sheet.Column(1);

        // this will resize the width to 60
        col.AutoFit();

        if (col.Width > 50)
        {
            col.Width = 50;

            // this is just a style property, and doesn't actually execute any recalculations
            col.Style.WrapText = true;
        }

        // so this is still 15.0.  How do I get it to compute what the size will be?
        double heightAfter = sheet.Row(1).Height;

        // open the xls, and the height is 30.0
        pkg.SaveAs(new System.IO.FileInfo("text.xlsx"));

实际上,搜索Height属性(或底层字段_height)表明它只是由属性setter设置,并且似乎根本不是基于其他任何东西(如行中的内容)设置的。

关于如何获得连续刷新高度的任何想法?

由于

1 个答案:

答案 0 :(得分:7)

我注意到EPPlus的一般模式是它生成文档的框架,只需要最少量的信息。然后,当您打开文件时,Excel会填写剩余的XML结构,这就是您在打开EPPlus生成的文档后始终必须保存文件的原因。

对于您的问题,我假设Excel在您打开Excel文件后更新行高,因此EPPlus不会有更新的行高信息。我并不完全确定该库不支持此功能,但与您一样,我无法找到获取更新值的方法。

然而,一种解决方法可能只是计算自您知道文本长度和列宽以来的值:

ExcelPackage pkg = new ExcelPackage();
ExcelWorksheet sheet = pkg.Workbook.Worksheets.Add("Test");

// height is 15.0
double heightBefore = sheet.Row(1).Height;

var someText = "Now is the time for all good men to come to the aid of their country. Typewriters were once ground-breaking machines.";
sheet.Cells[1, 1].Value = someText;

ExcelColumn col = sheet.Column(1);
ExcelRow row = sheet.Row(1);

// this will resize the width to 60
col.AutoFit();

if (col.Width > 50)
{
    col.Width = 50;

    // this is just a style property, and doesn't actually execute any recalculations
    col.Style.WrapText = true;
}

// calculate the approximate row height and set the value;
var lineCount = GetLineCount(someText, (int)col.Width);
row.Height = heightBefore * lineCount;

// open the xls, and the height is 45.0
pkg.SaveAs(new System.IO.FileInfo("text.xlsx"));

以下是计算行数的方法:

private int GetLineCount(String text, int columnWidth)
{
    var lineCount = 1;
    var textPosition = 0;

    while (textPosition <= text.Length)
    {
        textPosition = Math.Min(textPosition + columnWidth, text.Length);
        if (textPosition == text.Length)
            break;

        if (text[textPosition - 1] == ' ' || text[textPosition] == ' ')
        {
            lineCount++;
            textPosition++;
        }
        else
        {
            textPosition = text.LastIndexOf(' ', textPosition) + 1;

            var nextSpaceIndex = text.IndexOf(' ', textPosition);
            if (nextSpaceIndex - textPosition >= columnWidth)
            {
                lineCount += (nextSpaceIndex - textPosition) / columnWidth;
                textPosition = textPosition + columnWidth;
            }
            else
                lineCount++;
        }
    }

    return lineCount;
}

要记住的一件事是Excel的最大行高为409.5,因此您需要确保列宽不会太窄以至于达到此限制。

另外,我注意到的另一件事是您使用EPPlus手动设置的列宽实际上并未将列设置为预期值。例如,如果将列宽设置为50,您会注意到实际的列宽设置为49.29,因此您可能也希望将其列入。