Solved

Forcing Driver To Read Data From Excel

Posted on 2006-11-22
5
283 Views
Last Modified: 2013-12-25
I regularly use the OLEDB and ODBC connections to access Excel. The problem I always run into is that unless the user has a registry change accessing mixed content in rows is problematic.

Example connection: "string Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
Registry setting: [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel] located registry REG_DWORD "TypeGuessRows"

Is there a way to force Excel to read the contents of a sheet as text or more accurately to scan all rows for the content type via the driver. I can't change the content of the XL sheet or force a registry key change on some users and this solution needs to be across all machines. Maxscanrows=0 has no effect on the connection string and Microsoft themselves say that it hasn't worked since ADO 2.1

Any ideas?

0
Comment
Question by:Code_Mania
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 250 total points
Comment Utility
Alas, the problem you describe is an inherit flaw in the Excel OLEDB driver.  Because Excel, unlike databases, can have different rows with different types, you get problems.  The TypeGuessRows registry key can help sometimes, but not always.  Forcing columns to be text (which you can do by putting text values in) doesn't work either, because the OLEDB driver then returns nulls for anything that is stored as a number in Excel.  The only reliable ways I found to deal with this issue is to A) Export the spreadsheet into a CSV file, or B) open the Excel spreadsheet with the Excel APIs and use them to read the data.
0
 
LVL 16

Expert Comment

by:JohnBPrice
Comment Utility
oops, I forgot
C) Make Excel convert everything into text, I have a little gadget that does this.  It is a macro I sometimes use to force zip codes into text.  I open a spreadsheet, highlight the data I want to be text, and then run this macro.  It replaces any value with the Excel =T("blah") function.  It is coded specifically for zip codes, so you might have to change it some.

Sub MakeZipStrings()
    'make all the selected values into formulas like =T("15212") to fake Excel into reporting them as string.
    Dim w As Excel.Worksheet
    Dim r As Integer
    Dim c As Integer
    Dim SelRange As Excel.Range
   
    If TypeName(Application.Selection) <> "Range" Then
        MsgBox "select the cells to modify"
        Exit Sub
    End If

    Set SelRange = Application.Selection
    For r = 1 To SelRange.Rows.Count
        For c = 1 To SelRange.Columns.Count
            'If Left(SelRange.Cells(r, c).Formula, 3) <> "=T(" Then
                If SelRange.Cells(r, c).NumberFormat <> "General" Then
                    SelRange.Cells(r, c).NumberFormat = "General"
                End If
                If Len(SelRange.Cells(r, c)) = 9 And InStr(1, SelRange.Cells(r, c), "-") <= 0 Then
                    SelRange.Cells(r, c).Formula = "=T(""" & Left(Replace(SelRange.Cells(r, c).Text, "'", ""), 5) & "-" & Mid(Replace(SelRange.Cells(r, c).Text, "'", ""), 6) & """)"
                ElseIf Len(SelRange.Cells(r, c)) = 4 Then 'missing leading 0
                    SelRange.Cells(r, c).Formula = "=T(""" & Format(Replace(SelRange.Cells(r, c).Text, "'", ""), "00000") & """)"
                Else
                    SelRange.Cells(r, c).Formula = "=T(""" & Replace(SelRange.Cells(r, c).Text, "'", "") & """)"
                End If
                'The replace is in case someone put' in front
            'End If
        Next c
    Next r
           
    MsgBox "Done"
   
End Sub

0
 

Author Comment

by:Code_Mania
Comment Utility
Thanks John, sadly though I need to use the driver to do the work as I can't guarantee that the client has Excel on their machine.
0
 
LVL 16

Expert Comment

by:JohnBPrice
Comment Utility
Can you pre-process the spreadsheet and save it as a .csv file?  The text OLEDB client will handle it then.
0
 

Author Comment

by:Code_Mania
Comment Utility
Again no, generally information is fed down from a third party in an Excel sheet. The third party outputs the sheet in a fixed format, in my case trades in futures, options etc. bank transactions and balances, currency exchange rates. These are defined standards from the organisations that provide the feed.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now