给定单元格地址= Sheet1:A1,如何获取值

时间:2011-01-17 11:51:34

标签: c# vsto excel-2007

我的地址为= Sheet1:A1或= Sheet1:A1:B5(输入)

它有一些价值。如何使用Excel VSTO和C#

获取它

2 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解,但通常您不需要在VSTO中使用工作表名称前缀。但是,您确实需要对工作表的引用。有点像这样:

Excel.Worksheet ws;

//... get a reference to the worksheet

// you can get the value of a single cell into an object variable.
var rangeA1 = ws.get_Range("A1", Type.Missing);
var myValue = rangeA1.Value2;

// you can also pull out the value of a multicell range into a 2D array.
var rangeA1B5 = ws.get_Range("A1", "B5");
var myArrayOfValues = (object[,])rangeA1B5.Value2;

答案 1 :(得分:0)

我只是解析字符串:

using System.Text.RegularExpressions;

object _ = Type.Missing;
Excel.Workbook wb = ...; // get reference to workbook
string addr = "=Sheet1!A1:B5";
Match m = Regex.Match(addr, "^\=(?<sheet>[^!]+)!(?<cell>.+)$");
object value = wb.Worksheets[m.Groups["sheet"].Value].get_Range(m.Groups["cell"].Value, _).get_Value(_);

可能会错过一些演员阵容。