Search code examples
c#.netexcelxlsclosedxml

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)

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??


Solution

  • Your cells don't have any contents. Therefore AdjustToContents() won't have any effect.