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
Matthias

@Matthias_Winzer,

See the following sample code for your reference. It will give you selected (range of) cell(s) in the first worksheet in the workbook:
e.g.
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();
            while(rangeIterator.MoveNext())
            {
                Cell cell = (Cell)rangeIterator.Current;
                Console.WriteLine(cell.Name);
                //your code goes here.
            }
           }  

Hope, this helps a bit.

Hello Amjad Sahi,

Thank you, that code works fine for us.

Matthias

@Matthias_Winzer,

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.