简体   繁体   English

使用 ClosedXML 如何根据内容调整行高?

[英]Using ClosedXML how to adjust row height to content?

I create cell with text.我用文本创建单元格。 After that I set WrapText property and column width.之后我设置 WrapText 属性和列宽。

var cell = worksheet.Cell("A1");
cell.Style.Alignment.WrapText = true;
cell.SetValue("This is very long text");
worksheet.Column(1).Width = 10;
worksheet.Rows().AdjustToContents();

The text has been moved by words, but row height is not changed.文本已按单词移动,但行高未更改。 How to adjust row height to cell content?如何根据单元格内容调整行高?

There are many ways to achieve this. 有很多方法可以实现这一目标。

Don't use wrap or shrink properties on cell values rather include this line just before saving your excel 不要在单元格值上使用换行或收缩属性,而是在保存excel之前包含此行

ws.Columns().AdjustToContents();

Another way is to make use of Allignment property 另一种方法是使用Allignment属性

 IXLRange titleRange = ws.Range("B2:AA2");
        titleRange.Cells().Style
            .Alignment.SetWrapText(true); // Its single statement

Hope it helps!! 希望能帮助到你!!

It works when you remove the worksheet.Rows().AdjustToContents(); 它删除worksheet.Rows().AdjustToContents();时有效worksheet.Rows().AdjustToContents(); .

Autofitting sometimes needs more of a trial and error approach ... 自动调整有时需要更多的试错法...

The following code worked for me. 以下代码对我有用。

IXLRange contents = ws.Range("A1:A50");
contents.Style.Alignment.WrapText = true;

您还可以在特定范围的单元格上调整ToTteContents。

worksheet.Columns(2, 20).AdjustToContents();

I think this is a bug of ClosedXML.我认为这是 ClosedXML 的错误。 enter link description here在此处输入链接描述

ws.Row(1).AdjustToContents();
ws.Row(1).ClearHeight();

In my case it works.就我而言,它有效。 Version: 0.95.4版本:0.95.4

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

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