This project has moved and is read-only. For the latest updates, please go here.

AutoFitColumns ignores content width of wrapped text


I am trying to programmatically insert values separated by newlines, then autofit the cell width to the longest value. In Excel 2007, this can be done by:

Type in: 1234567890(Alt-Enter)123456(Alt-Enter)7890
Set wrap-text to false
Format->AutoFit Column Width
Set wrap-text to true
Format->AutoFit Column Width

But with the code:

ws.Cells[1, 1].Value = "1234567890\n123456\n7890";
ws.Cells[1, 1].Style.WrapText = false;
ws.Cells[1, 1].AutoFitColumns();
ws.Cells[1, 1].Style.WrapText = true;
ws.Cells[1, 1].AutoFitColumns();

The last AutoFitColumns() shrinks the cell down to it's minimum width, no matter how long each string inside it is, wrapping strings incorrectly,. Without the call, the cell is as wide as it was with every string on the same line, which is what I'm stuck with at the moment.