Here is my final piece of code if someone needs it. It also extends the width of the column a little bit after Autofit.
int i = 1;
while (i <= ws.UsedRange.Columns.Count)
{
try
{
ws.Columns[i].AutoFit();
}
catch
{
try
{
ws.Columns[i].AutoFit();
ws.Columns[i].ColumnWidth = ws.Columns[i].ColumnWidth + 5;
}
catch
{
ws.Columns[i].Width = 255;
}
}
i++;
}