C#: Getting the number of rows/columns with ExcelPackage

后端 未结 9 627
执念已碎
执念已碎 2021-02-03 18:59

I need to read and write data from an Excel spreadsheet. Is there a method to finding out how many rows/columns a certain worksheet has using ExcelPackage? I have the following

9条回答
  •  野性不改
    2021-02-03 19:36

    Epplus does not have support for usedrange but you can have it using usedrange.cs Assuming that you have downloaded latest EPPlus source code, Make changes to Worksheet.cs : make the original one partial. and then create separate cs file named UsedRange.cs paste the code below in it and compile.

    namespace OfficeOpenXml
    {
       using System;
       using System.Collections.Generic;
       using System.Text;
       using OfficeOpenXml.Style;
       using System.Data;
    /// 
    /// This class provides easy access to used range objects such as
    /// UsedRows, UsedColumns, UsedCells, UsedRow, UsedColumn etc.
    /// Authored by Mukesh Adhvaryu
    /// 
    public sealed class UsedRange : ExcelRange,IEnumerable
    {
        #region local variables
        int elementIndex=-1, cursor=-1, position=-1;
        UsedRangeElement element, parentElement;
        public const long MaxCells =(long) ExcelPackage.MaxRows *  
    (long)ExcelPackage.MaxColumns;
        #endregion
    
        #region constructors
        /// 
        /// this constructor is private because its accessibility outside can cause mess
        /// 
        /// 
        /// 
        /// 
        /// 
        UsedRange(ExcelWorksheet sheet, UsedRangeElement element, int elementIndex, int cursor)
            : base(sheet)
        {
            this.element = element;
            switch (element)
            {
                case UsedRangeElement.Rows:
                case UsedRangeElement.Columns:
                case UsedRangeElement.Cells:
                    parentElement = UsedRangeElement.Range;
                    break;
                case UsedRangeElement.Row:
                    parentElement = UsedRangeElement.Rows;
                    break;
                case UsedRangeElement.Column:
                    parentElement = UsedRangeElement.Columns;
                    break;
                case UsedRangeElement.Cell:
                    parentElement = UsedRangeElement.Cells;
                    break;
                case UsedRangeElement.RowCell:
                    parentElement = UsedRangeElement.Row;
                    break;
                case UsedRangeElement.ColumnCell:
                    parentElement = UsedRangeElement.Column;
                    break;
                default:
                    parentElement = 0;
                    break;
            }
            this.elementIndex = elementIndex;
            this.cursor = cursor;
            SetRange();
        }
    
        /// 
        /// this constructor is private because its accessibility outside can cause mess
        /// 
        /// 
        /// 
        /// 
        UsedRange(ExcelWorksheet sheet, UsedRangeElement element, int elementIndex)
            : this(sheet, element, elementIndex, -1) { }
    
        /// 
        /// this constructor is private because its accessibility outside can cause mess
        /// 
        /// 
        /// 
        UsedRange(ExcelWorksheet sheet, UsedRangeElement element)
            : this(sheet, element, -1, -1) { }
    
        /// 
        /// this constructor used only to create cellcollection range
        /// since cellindex can be very large long value considering rows * columns =no of cells in worksheet
        /// this constructor is private because its accessibility outside can cause mess
        /// 
        /// 
        /// 
        UsedRange(ExcelWorksheet sheet, long cellIndex)
            : base(sheet)
        {
            this.element = UsedRangeElement.Cell;
            this.parentElement = UsedRangeElement.Cells;
            CellToAddress(cellIndex);
            SetRange();
        }
        #endregion
    
        #region indexers & properties
        /// 
        /// Returns element at a given index 
        /// 
        /// 
        /// 
        public UsedRange this[int index]
        {
            get
            {
                if (index >= Count || index < 0) throw new IndexOutOfRangeException();
                switch (element)
                {
                    case UsedRangeElement.Rows:
                        ValidateRow(index);
                        return new UsedRange(_worksheet, UsedRangeElement.Row, index);
                    case UsedRangeElement.Columns:
                        ValidateCol(index);
                        return new UsedRange(_worksheet, UsedRangeElement.Column, index);
                    case UsedRangeElement.Cells:
                        ValidateCell(index);
                        return new UsedRange(_worksheet, index);
                    case UsedRangeElement.Row:
                        return new UsedRange(_worksheet, UsedRangeElement.RowCell, elementIndex, index);
                    case UsedRangeElement.Column:
                        return new UsedRange(_worksheet, UsedRangeElement.ColumnCell, elementIndex, index);
                    default:
                        return this;
                }
            }
        }
    
        /// 
        /// Returns particular Cell at a given index
        /// 
        /// 
        /// 
        public UsedRange this[long index]
        {
            get
            {
                ValidateCell(index);
                return new UsedRange(_worksheet, index);
            }
        }
    
        /// 
        /// Returns count of elements in this collection
        /// 
        public int Count
        {
            get
            {
                switch (element)
                {
                    case UsedRangeElement.Rows:
                    case UsedRangeElement.Column:
                        return _toRow - _fromRow + 1;
                    case UsedRangeElement.Columns:
                    case UsedRangeElement.Row:
                        return _toCol - _fromCol + 1;
                    case UsedRangeElement.Cells:
                    case UsedRangeElement.Range:
                        return (_toRow - _fromRow + 1) * (_toCol - _fromCol + 1);
                    default:
                        return 1;
                }
            }
        }
    
        /// 
        /// Returns type of this element collection
        /// 
        public UsedRangeElement Element
        {
            get { return element; }
        }
    
        /// 
        /// Returns parent type of element this collection
        /// 
        public UsedRangeElement ParentElement
        {
            get { return parentElement; }
        }
        #endregion
    
        #region private methods
        /// 
        /// Validates row index for row collection
        /// added by mukesh
        /// 
        /// 
        private void ValidateRow(int Row)
        {
            if (Row < 0 || Row > ExcelPackage.MaxRows)
            {
                throw (new ArgumentException("Row out of range"));
            }
        }
    
        /// 
        /// Validates column index for column collection
        /// added by mukesh
        /// 
        /// 
        private void ValidateCol(int Col)
        {
            if (Col < 0 || Col > ExcelPackage.MaxColumns)
            {
                throw (new ArgumentException("Column out of range"));
            }
        }
    
        /// 
        /// Validates cell index for cell collection
        /// added by mukesh
        /// 
        /// 
        private void ValidateCell(long Cell)
        {
            if (Cell <0 || Cell > UsedRange.MaxCells)
            {
                throw (new ArgumentException("Cell out of range"));
            }
    
        }
    
        /// 
        /// converts cell index into a point consists of row and column index.
        /// added by mukesh
        /// 
        /// 
        private void CellToAddress(long Cell)
        {
            long rc = ((_worksheet._cells[_worksheet._cells.Count - 1] as ExcelCell).Row
                        - (_worksheet._cells[0] as ExcelCell).Row) + 1;
            long cc = _worksheet._maxCol - _worksheet._minCol + 1;
            elementIndex = (int)(Cell / cc) + 1;
            cursor = (int)(Cell % cc) + 1;
        }
    
        /// 
        /// This method is added by mukesh
        /// 
        /// 
        /// Excel Range Object
        /// 
        ExcelRange SetRange()
        {
            switch (element)
            {
                case UsedRangeElement.Rows:
                case UsedRangeElement.Columns:
                case UsedRangeElement.Cells:
                    return this[(_worksheet._cells[0] as ExcelCell).Row, _worksheet._minCol,
                    (this._worksheet._cells[_worksheet._cells.Count - 1] as ExcelCell).Row,
                    _worksheet._maxCol];
    
                case UsedRangeElement.Row:
                    return this[elementIndex + 1, _worksheet._minCol, elementIndex + 1, _worksheet._maxCol];
    
                case UsedRangeElement.Column:
                    return this[(_worksheet._cells[0] as ExcelCell).Row, elementIndex + 1,
                    (_worksheet._cells[_worksheet._cells.Count - 1] as ExcelCell).Row, elementIndex + 1];
                case UsedRangeElement.RowCell:
                case UsedRangeElement.Cell:
                    return this[elementIndex + 1, cursor + 1];
                case UsedRangeElement.ColumnCell:
                    return this[cursor + 1, elementIndex + 1];
                default:
                    return this;
            }
        }
        #endregion
    
        #region internal static methods
        /// 
        /// these static methods will be used to return row collection from worksheet
        /// added by mukesh
        /// 
        /// 
        /// 
        internal static UsedRange RowCollection(ExcelWorksheet sheet)
        {
            return new UsedRange(sheet, UsedRangeElement.Rows);
        }
    
        /// 
        /// these static methods will be used to return column collection from worksheet
        /// added by mukesh
        /// 
        /// 
        /// 
        internal static UsedRange ColumnCollection(ExcelWorksheet sheet)
        {
            return new UsedRange(sheet, UsedRangeElement.Columns);
        }
    
        /// 
        /// these static methods will be used to return cell collection from worksheet
        /// added by mukesh
        /// 
        /// 
        /// 
        internal static UsedRange CellCollection(ExcelWorksheet sheet)
        {
            return new UsedRange(sheet, UsedRangeElement.Cells);
        }
        #endregion
    
        #region ienumerable implementation
        public new IEnumerator GetEnumerator()
        {
            position = -1;
            for (int i = 0; i < Count; i++)
            {
                ++position;
                yield return this[i];
            }
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        #endregion
    
        /// 
        /// Determine Type of Used range element. 
        /// Being used to return RowCollection, ColumnCollection, CellCollection or single Row, Column or Cell
        /// added by mukesh
        /// 
        public enum UsedRangeElement
        {
            Range, Rows, Columns, Cells,
            Row, Column, Cell, RowCell, ColumnCell
        }
    }
    
    public sealed partial class ExcelWorksheet : XmlHelper
    {
        /// 
        /// Provides access to a range of used rows
        ///   
        public UsedRange UsedRows
        {
            get
            {
                return UsedRange.RowCollection(this);
            }
        }
        /// 
        /// Provides access to a range of used columns. added by mukesh
        ///   
        public UsedRange UsedColumns
        {
            get
            {
                return UsedRange.ColumnCollection(this);
            }
        }
        /// 
        /// Provides access to a range of used cells. added by mukesh
        ///   
        public UsedRange UsedCells
        {
            get
            {
                return UsedRange.CellCollection(this);
            }
        }
        /// 
        /// UsedRange object of the worksheet. added by mukesh
        /// this range contains used Top left cell to Bottom right.
        /// If the worksheet has no cells, null is returned
        /// 
    }
    }
    

提交回复
热议问题