This project has moved. For the latest updates, please go here.

Last used cell in a row/column

May 10, 2011 at 3:26 PM
Edited May 10, 2011 at 3:26 PM

Hi,

 

Is there a way to find the last used cell in a row or column? Similar to range.end(xltoleft) ?

 

Thanks,

 

Matt

Coordinator
May 10, 2011 at 6:26 PM

Hi,

Not for a specific row or column within the worksheet, but you can use the Worksheet.Dimension.End.Row or Worksheet.Dimension.End.Column properties to get the bottom right cell.

Jan

Aug 2, 2012 at 4:49 AM

Hi,

 

It gives a Null pointer exception if the file is a new created file

 

For example:

using (ExcelPackage package = new ExcelPackage(newFile)) {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("POP");
                //Add the headers

                int row = worksheet.Dimension.Start.Row;
                int col = worksheet.Dimension.Start.Column;

 

Do you know whats the reason?

Editor
Aug 2, 2012 at 12:08 PM

I think it is by design. I see only two options in this case: return [1,1] or return null. If we choose to return [1,1], we would never know if there is only one cell in our sheet or it is empty.

 

 

 

Aug 2, 2012 at 12:39 PM

Yes, but I feel null would have been a better option rather throwing an exception. What do you think?

Editor
Aug 2, 2012 at 12:54 PM
Edited Aug 2, 2012 at 12:54 PM

EPPlus doesnt throw exception, only return null, your code are throwing exception because you don´t test if Dimension is null.

 

        /// <summary>
        /// Dimension address for the worksheet. 
        /// Top left cell to Bottom right.
        /// If the worksheet has no cells, null is returned
        /// </summary>
        public ExcelAddressBase Dimension
        {
            get
            {
                if (_cells.Count > 0)
                {
                    return new ExcelAddressBase((_cells[0] as ExcelCell).Row, _minCol, (_cells[_cells.Count - 1] as ExcelCell).Row, _maxCol);
                }
                else
                {
                    return null;
                }
            }
        }
Aug 2, 2012 at 2:13 PM

ok Thank you. This solves my problem :)