diff --git a/EPPlus-4.0.3/ExcelCellBase.cs b/EPPlus-4.0.3/ExcelCellBase.cs --- a/EPPlus-4.0.3/ExcelCellBase.cs +++ b/EPPlus-4.0.3/ExcelCellBase.cs @@ -480,17 +480,15 @@ bool fixedRow, fixedCol; return GetRowCol(address, out row, out col, throwException, out fixedRow, out fixedCol); } - internal static bool GetRowCol(string address, out int row, out int col, bool throwException, out bool fixedRow, out bool fixedCol) - { + internal static bool GetRowCol(string address, out int row, out int col, bool throwException, out bool fixedRow, out bool fixedCol) { bool colPart = true; - string sRow = "", sCol = ""; + int colStartIx = 0; + int colLength = 0; col = 0; + row = 0; fixedRow = false; fixedCol = false; - if (address.IndexOf(':') > 0) //If it is a mult-cell address use - { - address = address.Substring(0, address.IndexOf(':')); - } + if (address.EndsWith("#REF!")) { row = 0; @@ -501,71 +499,56 @@ int sheetMarkerIndex = address.IndexOf('!'); if (sheetMarkerIndex >= 0) { - address = address.Substring(sheetMarkerIndex + 1); + colStartIx = sheetMarkerIndex + 1; } - address = address.ToUpper(CultureInfo.InvariantCulture); - for (int i = 0; i < address.Length; i++) + for (int i = colStartIx; i < address.Length; i++) { - if ((address[i] >= 'A' && address[i] <= 'Z') && colPart && sCol.Length <= 3) + char c = address[i]; + if (colPart && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && colLength <= 3) { - sCol += address[i]; + col *= 26; + col += ((int)c) - 64; + colLength++; } - else if (address[i] >= '0' && address[i] <= '9') + else if (c >= '0' && c <= '9') { - sRow += address[i]; + row *= 10; + row += ((int)c) - 48; colPart = false; } - else if (address[i] == '$') + else if (c == '$') { - if (i == 0) - fixedCol = true; - else + if (i == colStartIx) + { + colStartIx++; + fixedCol = true; + } + else + { + colPart = false; fixedRow = true; + } + } + else if (c == ':') + { + break; } else { + row = 0; + col = 0; if (throwException) { throw (new Exception(string.Format("Invalid Address format {0}", address))); } else { - row = 0; - col = 0; return false; } } } - - // Get the column number - if (sCol != "") - { - col = GetColumn(sCol); - } - else - { - col = 0; - int.TryParse(sRow, out row); - return row>0; - } - // Get the row number - if (sRow == "") //Blank, fullRow - { - //if (throwException) - //{ - // throw (new Exception(string.Format("Invalid Address format {0}", address))); - //} - //else - //{ - row = 0; - return col > 0; - //} - } - else - { - return int.TryParse(sRow, out row); - } + return row != 0 || col != 0; } private static int GetColumn(string sCol) diff --git a/EPPlus-4.0.3/ExcelRangeBase.cs b/EPPlus-4.0.3/ExcelRangeBase.cs --- a/EPPlus-4.0.3/ExcelRangeBase.cs +++ b/EPPlus-4.0.3/ExcelRangeBase.cs @@ -58,7 +58,7 @@ /// /// A range of cells /// - public class ExcelRangeBase : ExcelAddress, IExcelCell, IDisposable, IEnumerable, IEnumerator + public class ExcelRangeBase : ExcelAddress, IExcelCell, IEnumerable { /// /// Reference to the worksheet @@ -2757,69 +2757,84 @@ CellsStoreEnumerator cellEnum; public IEnumerator GetEnumerator() { - Reset(); - return this; + return new ExcelRangeBaseEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { - Reset(); - return this; + return new ExcelRangeBaseEnumerator(this); } /// /// The current range when enumerating /// - public ExcelRangeBase Current - { - get - { - return new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column)); - } - } + public class ExcelRangeBaseEnumerator : IEnumerator { + private CellsStoreEnumerator _cellEnum; + private int _enumAddressIx = -1; + private ExcelRangeBase _range; + private ExcelRangeBase _current; - /// - /// The current range when enumerating - /// - object IEnumerator.Current - { - get - { - return ((object)(new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column)))); - } - } - - int _enumAddressIx = -1; - public bool MoveNext() - { - if (cellEnum.Next()) - { - return true; - } - else if (_addresses!=null) - { - _enumAddressIx++; - if (_enumAddressIx < _addresses.Count) - { - cellEnum = new CellsStoreEnumerator(_worksheet._values, - _addresses[_enumAddressIx]._fromRow, - _addresses[_enumAddressIx]._fromCol, - _addresses[_enumAddressIx]._toRow, - _addresses[_enumAddressIx]._toCol); - return MoveNext(); - } - else - { - return false; + /// + /// The current range when enumerating + /// + public ExcelRangeBase Current { + get { + return _current; } } - return false; - } - public void Reset() - { - _enumAddressIx = -1; - cellEnum = new CellsStoreEnumerator(_worksheet._values, _fromRow, _fromCol, _toRow, _toCol); + /// + /// The current range when enumerating + /// + object IEnumerator.Current { + get { + return _current; + } + } + + public ExcelRangeBaseEnumerator(ExcelRangeBase range) { + this._range = range; + Reset(); + } + public bool MoveNext() { + if (_cellEnum.Next()) { + _current._fromCol = _cellEnum.Column; + _current._fromRow = _cellEnum.Row; + _current._toCol = _cellEnum.Column; + _current._toRow = _cellEnum.Row; + _current.Address = GetAddress(_current._fromRow, _current._fromCol); + return true; + } + else if (_range._addresses != null) { + _enumAddressIx++; + if (_enumAddressIx < _range._addresses.Count) { + _cellEnum = new CellsStoreEnumerator(_range._worksheet._values, + _range._addresses[_enumAddressIx]._fromRow, + _range._addresses[_enumAddressIx]._fromCol, + _range._addresses[_enumAddressIx]._toRow, + _range._addresses[_enumAddressIx]._toCol); + return MoveNext(); + } + else { + return false; + } + } + return false; + } + + public void Reset() { + _enumAddressIx = -1; + _cellEnum = new CellsStoreEnumerator(_range._worksheet._values, _range._fromRow, _range._fromCol, _range._toRow, _range._toCol); + _current = new ExcelRangeBase(_range._worksheet, ExcelAddressBase.GetAddress(_cellEnum.Row, _cellEnum.Column)); + } + + public void Dispose() { + if (_cellEnum != null) { + _cellEnum.Dispose(); + _cellEnum = null; + } + } + } //private void GetNextIndexEnum(int fromRow, int fromCol, int toRow, int toCol) diff --git a/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs b/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs --- a/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs +++ b/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs @@ -150,10 +150,7 @@ LoadFields(); - using (var r=sheet.Cells[address.Address]) - { - r.Clear(); - } + sheet.Cells[address.Address].Clear(); } private void init() {