Hello!
I'm using the code below to import an Excel datasheet into an ado.net
table. The problem is, when I
get to the point where I want to iterate the recods, it always thinks
that there is 999 records in the table! If the table (worksheet) is
larger than that, then it returns an equally fictious number (always
way more).

Does anybody have any idea what I'm doing wrong? I'ts an excel 2003
spreadsheet that I'm reading from. Thanks in advance!


Sub readSheet()
Dim excelConn As New System.Data.OleDb.OleDbConnection


excelConn.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\MySpreadSheet.xls;"

& _
"Extended Properties=""Excel
8.0;HDR=Yes;"""
excelConn.Open()
Dim da As New OleDbDataAdapter("Select * From [SheetName$]",
excelConn)
Dim escapeExit As Boolean = False
Dim ds As DataSet = New DataSet()
Dim dc As DataColumn = Nothing
Dim dt As DataTable = Nothing
Dim rec As String = ""
Dim i As Integer = 0
Dim x As Integer = 0
da.Fill(ds)
dt = excelConn.GetSchema


Dim dr As DataRow
While Not Me.bwWorker.CancellationPending
'HERE'S THE PROBLEM: IT ALWAYS DECIDES THAT THERE'S 999
RECORDS!!!
For Each dr In ds.Tables(0).Rows
rec = ""
For x = 0 To UBound(dr.ItemArray)
rec = rec & " " & dr.ItemArray(x).ToString
Next
logIt(txtOutput.Text & ControlChars.CrLf & rec)
'Next
Next
End While
excelConn.Close()
excelConn.Dispose()


End Sub

Re: Bad record count importing excel spreadsheet to DataAdapter? by Paul

Paul
Wed Nov 22 11:44:33 CST 2006

On 22 Nov 2006 05:46:25 -0800, "Bmack500" <brett.mack@gmail.com> wrote:

¤ Hello!
¤ I'm using the code below to import an Excel datasheet into an ado.net
¤ table. The problem is, when I
¤ get to the point where I want to iterate the recods, it always thinks
¤ that there is 999 records in the table! If the table (worksheet) is
¤ larger than that, then it returns an equally fictious number (always
¤ way more).
¤
¤ Does anybody have any idea what I'm doing wrong? I'ts an excel 2003
¤ spreadsheet that I'm reading from. Thanks in advance!
¤
¤
¤ Sub readSheet()
¤ Dim excelConn As New System.Data.OleDb.OleDbConnection
¤
¤
¤ excelConn.ConnectionString =
¤ "Provider=Microsoft.Jet.OLEDB.4.0;" & _
¤ "Data Source=C:\MySpreadSheet.xls;"
¤
¤ & _
¤ "Extended Properties=""Excel
¤ 8.0;HDR=Yes;"""
¤ excelConn.Open()
¤ Dim da As New OleDbDataAdapter("Select * From [SheetName$]",
¤ excelConn)
¤ Dim escapeExit As Boolean = False
¤ Dim ds As DataSet = New DataSet()
¤ Dim dc As DataColumn = Nothing
¤ Dim dt As DataTable = Nothing
¤ Dim rec As String = ""
¤ Dim i As Integer = 0
¤ Dim x As Integer = 0
¤ da.Fill(ds)
¤ dt = excelConn.GetSchema
¤
¤
¤ Dim dr As DataRow
¤ While Not Me.bwWorker.CancellationPending
¤ 'HERE'S THE PROBLEM: IT ALWAYS DECIDES THAT THERE'S 999
¤ RECORDS!!!
¤ For Each dr In ds.Tables(0).Rows
¤ rec = ""
¤ For x = 0 To UBound(dr.ItemArray)
¤ rec = rec & " " & dr.ItemArray(x).ToString
¤ Next
¤ logIt(txtOutput.Text & ControlChars.CrLf & rec)
¤ 'Next
¤ Next
¤ End While
¤ excelConn.Close()
¤ excelConn.Dispose()
¤
¤
¤ End Sub

So what sort of data is in the extra rows? Are they blank?

AFAIK, the Excel driver simply returns what is in the Range of the Worksheet. You may need to limit
the rows returned by specifying column criteria in your SQL statement.


Paul
~~~~
Microsoft MVP (Visual Basic)

Re: Bad record count importing excel spreadsheet to DataAdapter? by Bmack500

Bmack500
Mon Nov 27 12:07:41 CST 2006

Yes, the rest of the worksheet is completely blank. How do modify the
SQL statement to not include Null / Blank rows?

Paul Clement wrote:
> On 22 Nov 2006 05:46:25 -0800, "Bmack500" <brett.mack@gmail.com> wrote:
>
> =A4 Hello!
> =A4 I'm using the code below to import an Excel datasheet into an ado.net
> =A4 table. The problem is, when I
> =A4 get to the point where I want to iterate the recods, it always thinks
> =A4 that there is 999 records in the table! If the table (worksheet) is
> =A4 larger than that, then it returns an equally fictious number (always
> =A4 way more).
> =A4
> =A4 Does anybody have any idea what I'm doing wrong? I'ts an excel 2003
> =A4 spreadsheet that I'm reading from. Thanks in advance!
> =A4
> =A4
> =A4 Sub readSheet()
> =A4 Dim excelConn As New System.Data.OleDb.OleDbConnection
> =A4
> =A4
> =A4 excelConn.ConnectionString =3D
> =A4 "Provider=3DMicrosoft.Jet.OLEDB.4.0;" & _
> =A4 "Data Source=3DC:\MySpreadSheet.x=
ls;"
> =A4
> =A4 & _
> =A4 "Extended Properties=3D""Excel
> =A4 8.0;HDR=3DYes;"""
> =A4 excelConn.Open()
> =A4 Dim da As New OleDbDataAdapter("Select * From [SheetName$]",
> =A4 excelConn)
> =A4 Dim escapeExit As Boolean =3D False
> =A4 Dim ds As DataSet =3D New DataSet()
> =A4 Dim dc As DataColumn =3D Nothing
> =A4 Dim dt As DataTable =3D Nothing
> =A4 Dim rec As String =3D ""
> =A4 Dim i As Integer =3D 0
> =A4 Dim x As Integer =3D 0
> =A4 da.Fill(ds)
> =A4 dt =3D excelConn.GetSchema
> =A4
> =A4
> =A4 Dim dr As DataRow
> =A4 While Not Me.bwWorker.CancellationPending
> =A4 'HERE'S THE PROBLEM: IT ALWAYS DECIDES THAT THERE'S 999
> =A4 RECORDS!!!
> =A4 For Each dr In ds.Tables(0).Rows
> =A4 rec =3D ""
> =A4 For x =3D 0 To UBound(dr.ItemArray)
> =A4 rec =3D rec & " " & dr.ItemArray(x).ToString
> =A4 Next
> =A4 logIt(txtOutput.Text & ControlChars.CrLf & rec)
> =A4 'Next
> =A4 Next
> =A4 End While
> =A4 excelConn.Close()
> =A4 excelConn.Dispose()
> =A4
> =A4
> =A4 End Sub
>
> So what sort of data is in the extra rows? Are they blank?
>
> AFAIK, the Excel driver simply returns what is in the Range of the Worksh=
eet. You may need to limit
> the rows returned by specifying column criteria in your SQL statement.
>=20
>=20
> Paul
> ~~~~
> Microsoft MVP (Visual Basic)


Re: Bad record count importing excel spreadsheet to DataAdapter? by Paul

Paul
Tue Nov 28 08:24:56 CST 2006

On 27 Nov 2006 10:07:41 -0800, "Bmack500" <brett.mack@gmail.com> wrote:

¤ Yes, the rest of the worksheet is completely blank. How do modify the
¤ SQL statement to not include Null / Blank rows?

Select * From [SheetName$] WHERE ColName IS NOT NULL


Paul
~~~~
Microsoft MVP (Visual Basic)