When AutoFit() is executed from an ExcelColumn that is not included in the worksheet dimension, produces an Arithmetic Overflow Exception at the Init() method called from the CellsStoreEnumerator Constructor.
```
public void AutoFitColumns()
{
this.AutoFitColumns(this._worksheet.DefaultColWidth);
}
/// <summary>
/// Set the column width from the content of the range.
/// Note: Cells containing formulas are ignored if no calculation is made.
/// Wrapped and merged cells are also ignored.
///
/// </summary>
///
/// <remarks>
/// This method will not work if you run in an environment that does not support GDI
/// </remarks>
/// <param name="MinimumWidth">Minimum column width</param>
public void AutoFitColumns(double MinimumWidth)
{
this.AutoFitColumns(MinimumWidth, double.MaxValue);
}
/// <summary>
/// Set the column width from the content of the range.
/// Note: Cells containing formulas are ignored if no calculation is made.
/// Wrapped and merged cells are also ignored.
///
/// </summary>
/// <param name="MinimumWidth">Minimum column width</param><param name="MaximumWidth">Maximum column width</param>
public void AutoFitColumns(double MinimumWidth, double MaximumWidth)
{
if (this._worksheet.Dimension == null)
return;
if (this._fromCol < 1 || this._fromRow < 1)
this.SetToSelectedRange();
Dictionary<int, Font> dictionary = new Dictionary<int, Font>();
bool doAdjustDrawings = this._worksheet._package.DoAdjustDrawings;
this._worksheet._package.DoAdjustDrawings = false;
int[,] drawingWidths = this._worksheet.Drawings.GetDrawingWidths();
int fromCol1 = this._fromCol > this._worksheet.Dimension._fromCol ? this._fromCol : this._worksheet.Dimension._fromCol;
int toCol1 = this._toCol < this._worksheet.Dimension._toCol ? this._toCol : this._worksheet.Dimension._toCol;
if (this.Addresses == null)
{
this.SetMinWidth(MinimumWidth, fromCol1, toCol1);
}
else
{
```
```
private void SetMinWidth(double minimumWidth, int fromCol, int toCol)
{
CellsStoreEnumerator<object> cellsStoreEnumerator = new CellsStoreEnumerator<object>(this._worksheet._values, 0, fromCol, 0, toCol);
```
```
public CellsStoreEnumerator(CellStore<T> cellStore, int StartRow, int StartCol, int EndRow, int EndCol)
{
this._cellStore = cellStore;
this._startRow = StartRow;
this._startCol = StartCol;
this._endRow = EndRow;
this._endCol = EndCol;
this.Init();
}
internal void Init()
{
this.minRow = this._startRow;
this.maxRow = this._endRow;
this.minColPos = this._cellStore.GetPosition(this._startCol);
if (this.minColPos < 0)
this.minColPos = ~this.minColPos;
this.maxColPos = this._cellStore.GetPosition(this._endCol);
if (this.maxColPos < 0)
this.maxColPos = ~this.maxColPos - 1;
this.row = this.minRow;
this.colPos = this.minColPos - 1;
int length = this.maxColPos - this.minColPos + 1;
this.pagePos = new int[length];
this.cellPos = new int[length];
for (int index = 0; index < length; ++index)
{
this.pagePos[index] = -1;
this.cellPos[index] = -1;
}
}
```
```
public void AutoFitColumns()
{
this.AutoFitColumns(this._worksheet.DefaultColWidth);
}
/// <summary>
/// Set the column width from the content of the range.
/// Note: Cells containing formulas are ignored if no calculation is made.
/// Wrapped and merged cells are also ignored.
///
/// </summary>
///
/// <remarks>
/// This method will not work if you run in an environment that does not support GDI
/// </remarks>
/// <param name="MinimumWidth">Minimum column width</param>
public void AutoFitColumns(double MinimumWidth)
{
this.AutoFitColumns(MinimumWidth, double.MaxValue);
}
/// <summary>
/// Set the column width from the content of the range.
/// Note: Cells containing formulas are ignored if no calculation is made.
/// Wrapped and merged cells are also ignored.
///
/// </summary>
/// <param name="MinimumWidth">Minimum column width</param><param name="MaximumWidth">Maximum column width</param>
public void AutoFitColumns(double MinimumWidth, double MaximumWidth)
{
if (this._worksheet.Dimension == null)
return;
if (this._fromCol < 1 || this._fromRow < 1)
this.SetToSelectedRange();
Dictionary<int, Font> dictionary = new Dictionary<int, Font>();
bool doAdjustDrawings = this._worksheet._package.DoAdjustDrawings;
this._worksheet._package.DoAdjustDrawings = false;
int[,] drawingWidths = this._worksheet.Drawings.GetDrawingWidths();
int fromCol1 = this._fromCol > this._worksheet.Dimension._fromCol ? this._fromCol : this._worksheet.Dimension._fromCol;
int toCol1 = this._toCol < this._worksheet.Dimension._toCol ? this._toCol : this._worksheet.Dimension._toCol;
if (this.Addresses == null)
{
this.SetMinWidth(MinimumWidth, fromCol1, toCol1);
}
else
{
```
```
private void SetMinWidth(double minimumWidth, int fromCol, int toCol)
{
CellsStoreEnumerator<object> cellsStoreEnumerator = new CellsStoreEnumerator<object>(this._worksheet._values, 0, fromCol, 0, toCol);
```
```
public CellsStoreEnumerator(CellStore<T> cellStore, int StartRow, int StartCol, int EndRow, int EndCol)
{
this._cellStore = cellStore;
this._startRow = StartRow;
this._startCol = StartCol;
this._endRow = EndRow;
this._endCol = EndCol;
this.Init();
}
internal void Init()
{
this.minRow = this._startRow;
this.maxRow = this._endRow;
this.minColPos = this._cellStore.GetPosition(this._startCol);
if (this.minColPos < 0)
this.minColPos = ~this.minColPos;
this.maxColPos = this._cellStore.GetPosition(this._endCol);
if (this.maxColPos < 0)
this.maxColPos = ~this.maxColPos - 1;
this.row = this.minRow;
this.colPos = this.minColPos - 1;
int length = this.maxColPos - this.minColPos + 1;
this.pagePos = new int[length];
this.cellPos = new int[length];
for (int index = 0; index < length; ++index)
{
this.pagePos[index] = -1;
this.cellPos[index] = -1;
}
}
```