如何包装合并的Excel内容超出指定的长度? [英] How to wrap merged Excel content beyond a specified length?

查看:117
本文介绍了如何包装合并的Excel内容超出指定的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel电子表格中有一列,其内容分散(合并)在四行中,如下所示:

I have a column in my Excel spreadsheet whose contents I spread (merge) over four rows like so:

private void AddDescription(String desc)
{
    int curDescriptionBottomRow = _curDescriptionTopRow + 3;
    var range = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[curDescriptionBottomRow, ITEMDESC_COL]];
    range.Merge();

    range.Font.Bold = true;
    range.VerticalAlignment = XlVAlign.xlVAlignCenter;
    range.Value2 = desc;
}

稍后我会自动调整列的宽度,使其足以显示所有内容:

I later Autofit the columns to be just wide enough to show all content:

_xlSheet.Columns.AutoFit();

问题在于,如果合并后的内容中只有一个或几个异常值",而这些异常值比其他内容长得多,那么我想将它们包装起来.在我认为内容/文本过长的情况下,如何将其分成两行(因为要换行)?

The problem is that if there is just one or a few "outliers" among the merged content that is considerably longer than the others, I want to wrap these. How can I, in the case of what I deem excessively long content/text, split that over two lines (because it to wrap)?

我尝试添加以下内容:

range.ColumnWidth = 144;
range.WrapText = true;

...这样代码就可以了:

...so that the code was then:

private void AddDescription(String desc)
{
    int curDescriptionBottomRow = _curDescriptionTopRow + 3;
    var range = _xlSheet.Range[_xlSheet.Cells[_curDescriptionTopRow, ITEMDESC_COL], _xlSheet.Cells[curDescriptionBottomRow, ITEMDESC_COL]];
    range.Merge();

    range.Font.Bold = true;
    range.ColumnWidth = 42;
    range.WrapText = true;
    range.VerticalAlignment = XlVAlign.xlVAlignCenter;
    range.Value2 = desc;
}

...但是此修复程序无法解决任何问题.

...but this fix fixes nothing.

您如何知道在什么时候换行?

How do you tell it at what point to wrap the text?

推荐答案

将范围的WrapText设置为true确实可以,在某种程度上,当您手动减小列的宽度时,文本会自动换行(但只有这样).

Setting WrapText to true for a range does work, in a way - when you manually reduce the width of the column, the text wraps (but only then).

最有效的方法是将自动换行与自动换行合并,方法是在 调用AutoFit之后为其指定精确宽度,以自动取回一列:

What worked for me was a combination of that word wrapping, and a taking back of the AutoFitting of one column by specifying a precise width for it after calling AutoFit:

private static readonly int WIDTH_FOR_ITEM_DESC_COL = 42;
. . .
_xlSheet.Columns.AutoFit();
// The AutoFitting works pretty well, but one column needs to be manually tweaked due to potentially over-long Descriptions
((Range)_xlSheet.Cells[1, 1]).EntireColumn.ColumnWidth =  WIDTH_FOR_ITEM_DESC_COL;

这篇关于如何包装合并的Excel内容超出指定的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆