Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
922 views
in Technique[技术] by (71.8m points)

.net - How to read Excel cell from VB.Net

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

Can you show me sample code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Namespace required using Microsoft.Office.Core;

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...