简体   繁体   中英

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;

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;

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 .

try this, I hope this will help you

using Excel = Microsoft.Office.Interop.Excel;
var value1 =(ws.Cells[7, 4] as Excel.Range).Value;
operator    Example
Range       A1:A1
Union       A1,A1
Intersect   A1 A1

Which makes me think that in theory .Range should require 2 Range parameters as Application.Union and Application.Intersect do, but fortunately it accepts pretty much any string expression that works in the Excel Address bar. For example:

string address = ws.Range["offset(a1,1,2,3,4)"].Address[0, 0]; // "C2:F4"

Also, ws.Cells[7,4] is Range so .Range(Range) doesn't make much sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM