Solved

Forcing Driver To Read Data From Excel

Posted on 2006-11-22
5
294 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
ID: 17995848
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
ID: 17995945
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
ID: 18029461
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
ID: 18030047
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
ID: 18032616
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

23 Experts available now in Live!

Get 1:1 Help Now