Cell from ExcelRange

This problem completely puzzled me.

I have an Excel document that loads in order. It has rows, columns and data, and I want to iterate over rows. But EPPLus is strange.

I take the second line:

ExcelRange range1 = worksheet.Cells[2, worksheet.Dimension.Start.Column, 2, worksheet.Dimension.End.Column]; 

Which gives me {A2:D2} Splendid! so far so good, but then I want the first cell of the row:

 ExcelRange range2 = range1[1,1]; 

Which gives me {A1} and degrade matter, the value of range1 also changed to {A1} instead of the selected line.

How can I solve this problem and take ExcelRange from ExcelRange?

This completely puzzles me .... thanks for anyhelp

+5
source share
2 answers

If you look at the code behind the ExcelRange index, you will see that get actually set the base address (nested else ):

 public ExcelRange this[string Address] { get { if (_worksheet.Names.ContainsKey(Address)) { if (_worksheet.Names[Address].IsName) { return null; } else { base.Address = _worksheet.Names[Address].Address; } } else { base.Address = Address; } _rtc = null; return this; } } 

Why they did it this way, I'm not sure (I assume this is his implementation detail). But this explains why a link to another address changes the selected range. So, as Benex said, you need to make a direct link from the Cells Worksheet collection.

0
source

I had the same problem to get the correct starting cell:

 var range2 = worksheet.Cells[range1.Start.Row, range1.Start.Column]; 

And the same for the bottom right cell:

 var range3 = worksheet.Cells[range1.End.Row, range1.End.Column]; 
+2
source

All Articles