简体   繁体   English

C#Excel范围管理

[英]C# Excel range management

I'm dealing with excel files in a C# application. 我正在处理C#应用程序中的excel文件。

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() : 因为您无法将Range()传递给单个Cells()属性,所以您需要使用Cells() 2个参数进行设置:

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 ws.Range[]的第一个参数应该是A1样式表示法

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. 这让我觉得理论上.Range应该需要2个Range参数作为Application.UnionApplication.Intersect来做,但幸运的是它几乎接受任何在Excel地址栏中工作的字符串表达式。 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. 另外, ws.Cells[7,4]Range所以.Range(Range)没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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