【问题标题】:epplus: How do I get a row's height after setting column's wraptext style to true?epplus:将列的 wraptext 样式设置为 true 后,如何获取行的高度?
【发布时间】:2020-09-17 01:11:42
【问题描述】:

设置列的 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)的搜索表明它仅由属性设置器设置,并且似乎从未基于其他任何内容(如行中的内容)进行设置。

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

谢谢

【问题讨论】:

    标签: epplus


    【解决方案1】:

    我注意到 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,因此您可能也需要将其考虑在内。

    【讨论】:

    • 你几乎在这里找到了答案。您将遇到的问题与 Excel 选择在空格(或连字符等)上换行有关。由于它不会在 50 个字符处完全中断,因此您可能会在最后得到一个额外的行(在上面的示例中选择 16 而不是 50)。如果我有时间,我会尝试编写一个例程来执行此操作,但这可能并不简单。
    • 啊,是的,你是对的,对不起,我完全错过了。我会看看我能想出什么,但是是的,它可能不会很漂亮。
    • Soo,就像您说的那样,尝试弄清楚 Excel 将如何处理您的包装文本并不是一件容易的事。但我在想,如果你能真正接近计算值并手动设置行高怎么办?这样你就可以控制价值是什么。我现在正在用可能的解决方案更新答案。
    • GetLineCount 如果文本中没有空格,则进入无限循环。将 else 之后的行替换为以下 var tryNextSpaceIndex = text.LastIndexOf(' ', textPosition) + 1; if (tryNextSpaceIndex > textPosition) textPosition = tryNextSpaceIndex;
    • @Chris 谢谢!我正在敲我的头想知道发生了什么事。这是一个很好的解释!
    猜你喜欢
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多