如何最好地使用VSTO从excel获取单元格值? [英] How to best fetch a cell value from excel using VSTO?
问题描述
我正在尝试将单元格从excel导入csharp,但不确定将其读入的最佳变量类型是什么.
I am trying to get cells from excel into csharp but not sure what's the best variable type to read it into.
如果我将变量设为字符串,并且单元格值是双精度值,则会出现解析错误.如果我将变量设置为double,则当单元格为字符串时,它将无法正常工作.
If I make the variable a string and the cell value is a double I get a parse error. If I make the variable double then when the cell is a string it wont work.
这是我正在运行的代码:
Here's the code I am running:
try
{
string i = Globals.Sheet1.Cells[7, 7].Value;
double num;
if (i == null) return;
if (double.TryParse(i, out num))
{
...
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
推荐答案
将其制成一个对象,然后从单元格中获取值后找出正确的类型.
Make it an object, then find out the right type after you have gotten the value out of the cell.
我不知道VSTO,但是在Excel Interop程序集中,有一个Value2
和一个Text属性,它们都返回对象,并且可以通过多态转换为正确的类型. VSTO不提供这些吗?
I don't know about VSTO, but in the Excel Interop assembly, there were a Value2
and a Text property which both returned object, and could be casted through polymorphism to the correct type. Doesn't VSTO provide those?
这篇关于如何最好地使用VSTO从excel获取单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!