Reputation: 399
I have a vb program that reads data from an excel sheet and displays it in a datagridview. The problem with this program is that it is able to read the data and display it only when the excel file is opened but when the excel file is not opened, it gives an error report that
System.Data.OleDb.OleDbException: External table is not in the expected format.
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at mForm.Form1.Button1_Click(Object sender, EventArgs e) in D:\myWorld\projects\coffee\mForm\mForm\Form1.vb:line 16
Is there a way to be able to read from this file even when it is not opened? Here is the code I use to get the data and put in the datagridview:
`Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection _
("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\Staff Contracts.xlsx; Extended Properties=Excel 8.0")
MyCommand = New System.Data.OleDb.OleDbDataAdapter _
("select * from [Staff Contracts$A3:K14]", MyConnection)
MyCommand.TableMappings.Add("Table", "TestTable")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
DataGridView1.DataSource = DtSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try`
Upvotes: 0
Views: 4913
Reputation: 21014
It looks like you are using an Excel 2003 connection string for an Excel 2007 file. Try using a connection string like this:
var connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=""Excel 12.0;HDR=YES"""
I use this code to read excel files server side (no need for Microsoft Office Install):
Public Function getData(selectCmd As String, filePath as string) As System.Data.DataTable
Dim dataTable As New DataTable()
Using conn As New OleDbConnection(connString)
Dim adapter As New OleDbDataAdapter(selectCmd, conn)
adapter.Fill(dataTable)
End Using
Return dataTable
End Function
Upvotes: 1