473,840 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using ODBC to read from Excel - mixed format columns

I currently am working on a VB.NET application that imports data from an
Excel spreadsheet that the customer filled in.
I used the following code to use ODBC to read the data from the spreadsheet,
and return it in a datatable.

Public Function ReadExcelDT(ByV al aFileName As String) As DataTable
' Open the Excel Spreadsheet, and using ODBC, read it into a DataTable for
later processing
Dim odbcConnectionS tring As String = _
"Driver={Micros oft Excel Driver (*.xls)};Driver Id=790;" & _
"Dbq=" & aFileName & ";"
Dim odbcConn As New OdbcConnection( odbcConnectionS tring)
Dim odbcCmd As New OdbcCommand("SE LECT * FROM [Sheet1$]", odbcConn)
Dim odbcAdapter As New OdbcDataAdapter (odbcCmd)
Dim Dt As New DataTable
odbcConn.Open()
odbcAdapter.Fil l(Dt)

odbcConn.Close( )
Return Dt
End Function
The problem: some of my columns are mixed format. In other words, some
fields are numeric, while others are alphabetic. My understanding of the way
that it works, it makes it's best "guess" what that format is supposed to
be, and all fields that are not in that format return only as DBNull values.
I also understand that the work-around for this is to enable something
called "import mode" in the connection string, so it would import everything
as text (including the numeric fields).
Unfortunately, I've had zero luck getting that working with ODBC. The only
code I have found on the internet (including Microsoft's site) regarding
enabling "import mode" is either using ADO or DAO, and both of those are
code in VB6.
The way I look at it, I have three choices:

1) Figure out how to enable "import mode" in ODBC
2) Figure out how to rewrite this code to use ADO or DAO (bearing in mind
that this is VB.NET)
3) Write my own Fill command that will "manually" read through the Excel
Spreadsheet (of course, in order to do that, I'll need to figure out how to
do that exactly).
Can anyone help?
Thanks in advance!

-Scott
Nov 23 '05 #1
2 16910
Scott,

I dont know if this is working on ODBC
"IMEX=1;" tells the driver to always read "intermixed " data columns as text

See for this
www.connectionstrings.com

Or

http://www.carlprothman.net/Default.aspx?tabid=81

I hope this helps,

Cor
Nov 23 '05 #2
Kittipong Methaprayoon
1 New Member
After spending a while trying to find the solution for this problem. I managed to get around the problem using OldDb data adapter instead of Odbc. This is VB.NET code I suggest:

Dim ConnectionStrin g As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & FileName & ";Extended Properties=""Ex cel 8.0;HDR=Yes;IME X=1"""
Dim Con As New OleDbConnection (ConnectionStri ng)
Dim CmdSelect As New OleDbCommand("S ELECT * FROM [MAIN$]", Con)
Dim Adapter As New OleDbDataAdapte r(CmdSelect)

More information can be found from

http://msdn2.microsoft.com/en-us/library/ms254500.aspx
http://support.microsoft.com/default...b;EN-US;257819
http://www.codeguru.com/cpp/data/mfc...cle.php/c8299/

Best of Luck :)
Jun 25 '06 #3

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

Similar topics

1
3297
by: Fons Dijkstra | last post by:
Hello, I'm using the mx.ODBC.Windows package in order to read/write a MSAccess database. Everything works fine apart from the DATE format handling. I'm using the default "datetimeformat" (i.e. DATETIME_DATETIMEFORMAT) as suggested. The date columns in the MSAccess database have "Short Date" format. When I read a DATE item everything works fine, like:
4
11314
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 Using this,I am first extracting data from given excel file into a temporary DataTable. After making some operations on that DataTable (like splitting one column into two), I am saving the data into actual
2
10705
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It appears to be in the OLEDB driver also. My connection was: conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" & "Extended Properties='Text;HDR=NO;FMT=Delimited'"
14
5870
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It appears to be in the OLEDB driver also. My connection was: conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" & "Extended Properties='Text;HDR=NO;FMT=Delimited'"
3
3765
by: | last post by:
Hello, I have a routine in my Windows application that exports the contents of a datagrid to Excel. It is modeled closely after the HowTo example on MSDN: http://tinyurl.com/5g2jm. Depending on the number of rows/columns in the datagrid, it can be extremely slow (several minutes to populate a 1000x50 spreadsheet). That is understandable, looking at the number of steps to copy over just one row.
3
9932
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel (SP1) installed. The error is: System.Runtime.InteropServices.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC. at Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination, Object Link) at...
0
2042
by: genojoe | last post by:
I have a 4 column, 6 row Excel sheet with all cells formatted for text: 1 1 a a 2 2 b b 3 3 c c a 4 d 1 b 5 e 2 c 6 f 3 I have the following schema.ini:
0
3679
by: vinidimple | last post by:
Hi i have a serious problem while i was working in Excel.I want to fetch columns from an excel worksheet and i need to compare it with an sql querry fields,so i tried to open an excle worksheet first.. Unfortunately my code was throwing error like
6
12368
by: =?Utf-8?B?LnBhdWwu?= | last post by:
how can i write to an excel .xls file using odbc? i've read in several places that its possible but i can't find an example. i have managed to read an excel file using odbc
0
9860
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10601
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10300
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7838
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4072
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.