473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading Excel in VB.NET

Hi,

So I do actually know how to read excel into VB.NET using the following
approach:

With oConn2
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source="
& FilePath & ";Extended Properties=Excel 8.0;"
.Open()
End With

Worksheet = "Well Data"
strSQL = "SELECT * FROM [" & Worksheet & "$]"

With oCmd2
.Connection = oConn2
.CommandType = CommandType.Text
.CommandText = strSQL
End With

oDA2.SelectCommand = oCmd2
oDA2.Fill(oDS2)

This approach works great except for with the type of excel worksheet I
am dealing with now. This particular worksheet has a column which
contains numbers, dates, strings containing only text and strings
containing numbers and text. The first value it runs across is a
number and from that point on it tries to force everything in the
column to be a number, and if it contains text, then it simply returns
a DBNull value for that cell.

Is there something I can do to force the Data Adaptor to see this
column as simply text and to just leave it alone? Or is there another
approach to reading Excel that I could use?

Thanks!

Joy

Mar 22 '06 #1
6 7006
si***********@yahoo.com wrote:
Hi,

So I do actually know how to read excel into VB.NET using the following
approach:

With oConn2
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source="
& FilePath & ";Extended Properties=Excel 8.0;"
.Open()
End With

Worksheet = "Well Data"
strSQL = "SELECT * FROM [" & Worksheet & "$]"

With oCmd2
.Connection = oConn2
.CommandType = CommandType.Text
.CommandText = strSQL
End With

oDA2.SelectCommand = oCmd2
oDA2.Fill(oDS2)

This approach works great except for with the type of excel worksheet I
am dealing with now. This particular worksheet has a column which
contains numbers, dates, strings containing only text and strings
containing numbers and text. The first value it runs across is a
number and from that point on it tries to force everything in the
column to be a number, and if it contains text, then it simply returns
a DBNull value for that cell.

Is there something I can do to force the Data Adaptor to see this
column as simply text and to just leave it alone? Or is there another
approach to reading Excel that I could use?

Thanks!

Joy


You can open the excel sheet directly through interop.

Example:
Dim objExcel As New Excel.Application
Dim objWrkBk As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim objRng As Excel.Range
Dim strCol, strCell As String
Dim maxCol, maxRow As Integer
Dim iRow, iCol As Integer
maxRow = 2
maxCol = 2
objWrkBk = objExcel.Workbooks.Open("C:\test.xls")
objSht = objWrkBk.Worksheets(1)
objExcel.Visible = True
For iCol = 1 To maxCol
For iRow = 1 To maxRow
strCol = Chr(Asc(iRow) + 16)
strCell = strCol + iCol.ToString
objRng = objSht.Range(strCell)
MsgBox(objRng.Value)
Next
Next
You will need to make a refernce to the Excel, which is easy. Look at
the top of this article for how do that.

http://www.vbdotnetheaven.com/Code/Jul2003/2124.asp

Chris
Mar 22 '06 #2
Hey Thanks. That helps a lot. I checked out the link you sent me but
I'm still a bit confused. Where do I get this TlbImp? And in the
article it says to execute this line:

TlbImp Excel9.olb Excel.dll

But where is this executed? In the command line prompt or somewhere
else?

Thanks again.

Mar 22 '06 #3
Hi Joy,

you can specify a conversion on that field (or even all). For instance,
if it is the first one:

SELECT
iif(IsNull(w.F1), "", cstr(w.F1)),
w.F2
FROM
[Worksheet$] w
if it is the k-th field, you will clearly use FK where k is the ordinal
of the field (k=1,2,3...)

Let me know if this works for you...

-tommaso

PS.
- Can omit aliasing if you wish
- F1, F2, F3 , ... clearly are the automatic names assigned to the
column by the OLEDB driver. They are always so (in any case, if you
like generality, you could get them from the SchemaGuid)...

si***********@yahoo.com ha scritto:
Hi,

So I do actually know how to read excel into VB.NET using the following
approach:

With oConn2
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source="
& FilePath & ";Extended Properties=Excel 8.0;"
.Open()
End With

Worksheet = "Well Data"
strSQL = "SELECT * FROM [" & Worksheet & "$]"

With oCmd2
.Connection = oConn2
.CommandType = CommandType.Text
.CommandText = strSQL
End With

oDA2.SelectCommand = oCmd2
oDA2.Fill(oDS2)

This approach works great except for with the type of excel worksheet I
am dealing with now. This particular worksheet has a column which
contains numbers, dates, strings containing only text and strings
containing numbers and text. The first value it runs across is a
number and from that point on it tries to force everything in the
column to be a number, and if it contains text, then it simply returns
a DBNull value for that cell.

Is there something I can do to force the Data Adaptor to see this
column as simply text and to just leave it alone? Or is there another
approach to reading Excel that I could use?

Thanks!

Joy


Mar 22 '06 #4
si***********@yahoo.com wrote:
Hey Thanks. That helps a lot. I checked out the link you sent me but
I'm still a bit confused. Where do I get this TlbImp? And in the
article it says to execute this line:

TlbImp Excel9.olb Excel.dll

But where is this executed? In the command line prompt or somewhere
else?

Thanks again.


yes, it's an exe installed with visual studio. Use it at the command
prompt.

Chris
Mar 22 '06 #5
Thanks Chris,

Sorry if this is a redundant question but, what directory do I need to
be in when I execute the command? When I tried to execute it just at
the C drive, it returns an error.

Joy

Mar 23 '06 #6
si***********@yahoo.com wrote:
Thanks Chris,

Sorry if this is a redundant question but, what directory do I need to
be in when I execute the command? When I tried to execute it just at
the C drive, it returns an error.

Joy


I'd do a search for the file. There may be a "Visual Studio .Net
Command Prompt" in your program files for Visual Studio. That one will
probably have the paths setup.

Chris
Mar 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mail2atulmehta | last post by:
Hi, I do not know if this is right place for this, but i need some help. I have a text file, whose values are seprated by a delimiter. I want to open this file in excel, ( not import it) . I have...
1
by: Turner, GS \(Geoff\) | last post by:
> -----Original Message----- > From: siliconwafer > Posted At: 19 August 2005 15:20 > Posted To: c > Conversation: reading an excel file in C? > Subject: reading an excel file in C? > > >...
4
by: Phoebe. | last post by:
Hi, Good Day! Reading 1 excel file into a dataset is fine. How can I read multiple excel with the same data structure into 1 dataset? How can I append those data? Can someone help? Thanks in...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
2
by: sherifffruitfly | last post by:
Hi, I'm using an adaptation of excel-reading code that's all over the internet - I don't much like or understand it, but it has worked for me in the past.... beggars can't be choosers... : ...
5
by: SantaClaus | last post by:
Hi all, I'm reading an excel file in VB.net. It works fine if I don't give any condition in the 'where' claus. My code is Dim myDataset As New DataSet() Dim strConn As String =...
3
by: Bharathi | last post by:
Hi, I got strucked with reading date value from excel file using C#.NET. For Jan-2000 the value I am getting is 36526.0. For all other dates also I am getting some double value like this. ...
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.