简体   繁体   English

ClosedXML.Excel 中的自动调整列

[英]Autofit column in ClosedXML.Excel

I understand that the question stupid and from FAQ, but i cant set auto width in excel columns (using ClosedXML.Excel library)我知道这个问题很愚蠢并且来自常见问题解答,但是我无法在 excel 列中设置自动宽度(使用 ClosedXML.Excel 库)

my code:我的代码:

var wb = new XLWorkbook();
var wsDep = wb.Worksheets.Add("MyWorksheet");
wsDep.Columns("A").AdjustToContents();
wsDep.Columns("B1").AdjustToContents();
wsDep.Columns().AdjustToContents();

but nothing changes.但没有任何变化。 how can i set auto width columns with ClosedXML.Excel library??如何使用 ClosedXML.Excel 库设置自动宽度列?

您的单元格没有任何内容。因此AdjustToContents()不会产生任何效果。

You need to give the range of the columns.您需要给出列的范围。
Example: wsDep.Columns("A","H").AdjustToContents();示例: wsDep.Columns("A","H").AdjustToContents();
I hope it will be work.我希望它会起作用。

Adding to an old post in case someone comes across this one like I did.添加到旧帖子中,以防有人像我一样遇到这个帖子。 My fix was to use the worksheet.Columns().AdjustToContents();我的解决方法是使用worksheet.Columns().AdjustToContents(); but I had to add it after I wrote out all of the data.但我必须在写出所有数据后添加它。 Initially I was setting it when I was setting up all of my column headers and colors during up creation of the worksheet and it wasn't working.最初,当我在创建工作表期间设置所有列标题和颜色时,我正在设置它,但它不起作用。 I then came across this thread and it made me think that it was adjusting the contents at the point that I was calling the AdjustToContents which in my case have anything more than the column headers at that point.然后我遇到了这个线程,它让我认为它在我调用 AdjustToContents 时正在调整内容,在我的情况下,它在那个时候除了列标题之外还有其他任何东西。 I moved it to the end after writing out all of my data and it worked as expected and actually makes sense as to what it was doing.在写出我的所有数据后,我把它移到最后,它按预期工作,实际上它在做什么是有意义的。

wsDep.Columns().AdjustToContents();

您应该在代码部分的末尾编写此代码,因为它仅在您在代码末尾编写时才起作用。

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

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