VB.net Excel.worksheet().cells().Value

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23004274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:29:30  来源:igfitidea点击:

VB.net Excel.worksheet().cells().Value

vb.netexcelvb.net-2010

提问by Niarah

Trying to write information in Excel worksheet But have some strange problems ;) i looked info on Google but no result.. So plz help )

试图在 Excel 工作表中写入信息但有一些奇怪的问题 ;) 我在谷歌上查看信息但没有结果..所以请帮忙)

I Add reference Microsoft.excel 14.0 Object Library and also Import Microsoft.Office.interop

我添加参考 Microsoft.excel 14.0 Object Library 并导入 Microsoft.Office.interop

I need to get Value from specific cell so i use command Checker = shXL.cells(1,1). And here i don't have Value.... i only got this (Equal / GetHashCode / GetType / toString) So question is WHY i don't have (.Value) for .cells and where is an Error

我需要从特定单元格获取值,所以我使用命令 Checker = shXL.cells(1,1)。在这里我没有 Value .... 我只有这个 (Equal / GetHashCode / GetType / toString) 所以问题是为什么我没有 .cells 的 (.Value) 以及错误在哪里

        Dim appXL As Excel.Application
        Dim wbXL As Excel.Workbook
        Dim wbsXL As Excel.Workbooks
        Dim shXL As Excel.Worksheet
        Dim Checker As Integer

        appXL = CreateObject("excel.application")
        appXL.Visible = True

        wbsXL = appXL.Workbooks
        wbXL = wbsXL.Open("D:\Некорректные Переключения\Base.xlsx", , , , 12121)
        shXL = wbXL.ActiveSheet
        Checker = shXL.Cells(1, 1).value()

Best Regards and THX!

最好的问候和 THX!

回答by SysDragon

That's because .Cells()returns an object.

那是因为.Cells()返回一个对象。

You can try converting it to a Excel Cellobject in another step, or you can try this (for example):

您可以尝试Cell在另一个步骤中将其转换为 Excel对象,或者您可以尝试以下操作(例如):

shXL.Range("A2").Value

With conversion will be:

转换将是:

Dim xRng As Excel.Range = CType(shXL.Cells(3,3), Excel.Range)
Dim val As Object = xRng.Value()

回答by MrBlue

With Excel interop, a lot of the time a return value will be in the form of an Objectso you need to cast to the correct type to get it's actual properties.

使用 Excel 互操作,很多时候返回值将采用 an 的形式,Object因此您需要强制转换为正确的类型以获取其实际属性。

So do something like this (my VB is rusty so may not be completely correct)...

所以做这样的事情(我的VB生锈所以可能不完全正确)......

Dim rng as Excel.Range

rng = CType(shXL.Cells(1, 1), Excel.Range)
Checker = rng.Value

Note: I've separated it out into two lines because it's important not use double-dot references with Office interop (e.g., Worksheet.Cell.Value) because you end up with objects you can't release, which will cause issues with Excel not closing properly.

注意:我将它分成两行,因为重要的是不要在 Office 互操作中使用双点引用(例如 Worksheet.Cell.Value),因为您最终会得到无法释放的对象,这会导致 Excel 出现问题没有正确关闭。

Note2: the .Valueproperty also returns as an object so you'll probably want to cast that too

注意2:该.Value属性也作为对象返回,因此您可能也想对其进行转换