We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Get selected cells and cell areas in the Excel worksheet in Code-Behind using C#.NET

Hello Aspose-Team,

How is it possible to get the selected cell or cell-area in Code-Behind?

For example: I select the cells A2 - B4, I want as an result the first top cell and the last bottom cell.

Our Version is: Aspose.Cells 20.6.6

Thanks in Advance for your help


See the following sample code for your reference. It will give you selected (range of) cell(s) in the first worksheet in the workbook:
Sample code:

Workbook workbook = new Workbook("e:\\test2\\Bk_selectcells1.xlsx");
           Worksheet worksheet = workbook.Worksheets[0];
           Cells cells = worksheet.Cells;

           ArrayList selectedRanges = worksheet.GetSelectedRanges();

           foreach (object obj in selectedRanges)
            Range sRange = (Range)obj;
            Console.WriteLine(sRange.RefersTo);//print the range area if you want.
            IEnumerator rangeIterator = sRange.GetEnumerator();
                Cell cell = (Cell)rangeIterator.Current;
                //your code goes here.

Hope, this helps a bit.

Hello Amjad Sahi,

Thank you, that code works fine for us.



Good to know that the suggested code segment works for your needs. In the event of further queries or issue, feel free to write us back.