Groups | Blog | Home
all groups > vb.net data > november 2006 >

vb.net data : Excel: Numerics are read as Null


Kjell Weding
11/17/2006 1:03:28 PM
Software: VB.NET 2.0 and Excel 2003

I have an Excel sheet with 140 records. 135 of the records have alphanumeric
data in fields.item(2) (see code below). 5 of the 140 cells in this coloumn
is however numeric.

When I read the records, the alphanumeric fileds is read OK, but the 5
records with numeric data is read as *Null*!!! Why????

How can I get my VB.NET program read the numeric value 5 as 5 or "5" and NOT
as Null????

cnnExcel.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; _
data source='C:\MySeet';Extended Properties=Excel 8.0;"
cnnExcel.Open()

Dim RS As New ADODB.Recordset()
sSQL = "SELECT * FROM [Sheet1$];"
RS.Open(sSQL, cnnExcel, ADODB.CursorTypeEnum.adOpenStatic, _
ADODB.LockTypeEnum.adLockOptimistic, 1)

RS.MoveFirst()
For I = 1 To RS.RecordCount
Debug.Print("" & RS.Fields.Item(2).Value)
RS.MoveNext()
Next

Kjell Weding, Norway
kjell@weding.no

Ken Tucker [MVP]
11/18/2006 11:08:01 AM
Hi,

I would start using ado.net you might have better results

Here is an example with a datareader to loop though the records

Imports System.Data.OleDb

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\kwlog.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim conn As New OleDbConnection(strConn)
Dim cmd As New OleDbCommand("Select * From [Sheet1$]", conn)
Dim dr As OleDbDataReader

conn.Open()
dr = cmd.ExecuteReader
Do While dr.Read
Trace.WriteLine(dr("KW").ToString)
Loop
dr.Close()
conn.Close()
End Sub
End Class

You can also load the records into a datatable the data is available in memory

Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\kwlog.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Dim conn As New OleDbConnection(strConn)
Dim da As New OleDbDataAdapter("Select * From [Sheet1$]", conn)
Dim dt As New DataTable

da.Fill(dt)

For Each dr As DataRow In dt.Rows
Trace.WriteLine(dr("kw").ToString)
Next


Ken
----------------------------

[quoted text, click to view]
Kjell Weding
11/21/2006 2:46:11 PM
Thank you.
This worked fine!
New knowledge makes a good day!
Kjell

"Ken Tucker [MVP]" <KenTuckerMVP@discussions.microsoft.com> skrev i melding
news:BE066ADB-054E-414F-9306-7111AD408FAE@microsoft.com...
[quoted text, click to view]

AddThis Social Bookmark Button