Home » excel » c# – Excel – referenced values via OleDB from .Net client

c# – Excel – referenced values via OleDB from .Net client

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to read an Excel file (.xls, I think Excel 2003 compatible) via OleDB, but it fails to get the values for referenced fields.

This is my current test code (please note, this is just part of the class):

Private m_conn As OleDbConnection
Public Sub New(ByVal fileName As String)
    Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", fileName)
    m_conn = New OleDbConnection(connString)
    m_conn.Open()
End Sub
Public Sub GetSheet(ByVal sheet As String)
    Dim query As String = String.Format("SELECT * FROM [{0}]", sheet)
    Using cmd As OleDbCommand = m_conn.CreateCommand()
        cmd.CommandText = query
        Dim ds As New DataSet()
        Dim a As New OleDbDataAdapter(cmd)
        Using rdr As OleDbDataReader = cmd.ExecuteReader()
            While rdr.Read()
                Debug.WriteLine(rdr.Item(0).ToString())
            End While
        End Using
    End Using
End Sub

But if the value is a reference (something like =+'MySheetName'!K37), I just get a DBNull from the call to rdr.Item(0).

I can get around this by automating Excel instead, but would prefer not to have to use Excel automation so wondering if anyone knows how to do it.

How to&Answers:

Not the whole class involved but you will get the idea. This isn’t excel automation, rather Microsoft Access oledb drivers reading the excel file, so its much faster.

You have to have registry setting correct and determine which version of Excel your reading and you have to install some drivers. Note: Office 2010 drivers can be downloaded from Microsoft but I’m not sure if they also install the drivers required for older editions of MS Office files, aka I have only installed it on my SQL server where I had older oledb drivers for MS Office loaded.

Project Resources:

Excel2007ConnectionWithHeader Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=”Excel 12.0;HDR=Yes”;
Excel2007ConnectionWithHeaderAllText Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=”Excel 12.0;HDR=Yes;IMEX=1″;
Excel2007ConnectionWithoutHeader Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=”Excel 12.0;HDR=No”;
Excel2007ConnectionWithoutHeaderAllText Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=”Excel 12.0;HDR=No;IMEX=1″;
ExcelAutomationError Unknow Excel Automation error ‘{0}’: {1}. {2}
ExcelConnectionWithHeader Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=”Excel 8.0;HDR=Yes”;
ExcelConnectionWithHeaderAllText Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=”Excel 8.0;HDR=Yes;IMEX=1″;
ExcelConnectionWithoutHeader Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=”Excel 8.0;HDR=No”;
ExcelConnectionWithoutHeaderAllText Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=”Excel 8.0;HDR=No;IMEX=1″;
ExcelException “Error reading excel data file ‘{0}’.
Message:
{1}”
ExcelOledbException “Oledb error reading excel data file ‘{0}’.
Message:
{1}”
ExcelPasswordException Could not decrypt secure Excel file.
ExcelPasswordRestriction The excel contains a password that restricts the workbook from being read. Please remove the workbook password from ‘{0}’. And then retry.
ExcelSelectStatement SELECT * FROM [{0}]