Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Excel range management

I'm dealing with excel files in a C# application.

I'm wondering why this code doesn't work:

var value1 = ws.Range[ws.Cells[7,4]].Value;

For now I found that this works fine:

int i = 7;
var value1 = ws.Range["D" + i.ToString()].Value;
like image 984
Zag Avatar asked Oct 26 '25 15:10

Zag


2 Answers

Because you cant pass to a Range() a single Cells() Property, you will need to set it with 2 parameters of Cells():

var value1 = ws.Range[ws.Cells[7,4],ws.Cells[7,4]].Value;

or (use strict the Cells Property):

var value1 =  ws.Cells[7,4].Value;
like image 162
Jonathan Applebaum Avatar answered Oct 29 '25 03:10

Jonathan Applebaum


The first parameter of ws.Range[] should be in in A1-style notation

To access

ws.Cells[7,4]

you can try this code:

var value1 = ws.Range["D7"].Value;

And check out this.

like image 42
S.Dav Avatar answered Oct 29 '25 03:10

S.Dav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!