Problems reading an Excel from VB .NET

DevilAkuma

Active member
Joined
Oct 24, 2005
Messages
37
Programming Experience
Beginner
Hello!

I've a little problem when I try to read an excel file. The problem is that the system eats one file of data from the excel! I think that it's waiting to read and excel with headers, but my file doesn't have headers... All is valid dates!

I read the file with this:

VB.NET:
objOleConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " &
         "data source=" & path & "; Extended Properties=Excel 8.0;")   
 objDataAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [" & Sheet1 & "$]  ", objOleConnection)
       objDataSet = New System.Data.DataSet
       objDataAdapter.Fill(objDataSet)
        For Each objRow As DataRow In objDataSet.Tables(0).Rows
              Dim a As String = objRow.Item(0)
         Next
         objOleConnection.Close()


The first string readed in a is the A2 cell in my Excel...l Why!?!?

Thanks in advance
 
That's the problem, the first row of data is being used as column/field headers. Either add a row of headers (easiest) or you will need to pre-define the dataset prior to reading in the data. Then be a little more specific about where you read in the data and where you want it put.
 
More or less, I've solutionated the problem. I've created a little function that opens my Excel, and inserts headers :)

But now I've a another mess... Can I know (before trying to read/write my file) if the Excel that I want to execute is already opened?

Thanks in advance
 
There are several different ways to do it. I think what you really want to research is error handeling. Lots of options here... your open code in a Try/catch statement? Look around a bit and you'll find something you like.
 
Yes!

Actually, I'm doing it now with a Try/catch, but... Is there any attribute that can tell me if its opened and who is the owner before the exception is launched?

Thank in advance!
 
Back
Top