如何从VB.Net读取Excel单元格 [英] How to read Excel cell from VB.Net

查看:934
本文介绍了如何从VB.Net读取Excel单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Excel文件读取特定的细胞使用OLEDB连接用VB.NET?

How can I read specific cell from Excel file using OLEDB Connection with VB.NET?

你能告诉我试样code?

Can you show me sample code?

推荐答案

试试下面的C#code:

Try the following C# code:

Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & ExcelFilePath & "; " & _
"Extended Properties=Excel 8.0")

' Select the data from Sheet1 ([in-house$]) of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [in-house$]", MyConnection)

DS = New System.Data.DataSet
MyCommand.Fill(DS)
Dt = DS.Tables(0)
DataGrid1.DataSource = Dt

有关特定的电池试试这个(它会读取小区D6)。要注意的一点是,它没有使用OLEDB连接而是直接访问

For particular cell try this (it will read cell D6). The point to note is that it is not using OLEDB connection rather it is directly accessing.

命名空间所需的使用Microsoft.Office.Core;

加入引用从COM添加它的的Microsoft Office 12.0对象库

Add it by adding reference from COM to Microsoft Office 12.0 Object Library

Dim oApp As New Excel.Application
Dim oWBa As Excel.Workbook = oApp.Workbooks.Open("c:\Test.XLS")
Dim oWS As Excel.Worksheet = DirectCast(oWBa.Worksheets(1),
Excel.Worksheet)
oApp.Visible = False

Dim oRng As Excel.Range
oRng = oWS.Range("D6")
MsgBox(oRng.Value)

这篇关于如何从VB.Net读取Excel单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆