[英]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;
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.Union
和Application.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.