Reading excel file in Vista x64
-
I'm trying to read an Excel file in C# Visual Studio 2008 using oldDb. My connection gets opened successfully, but oldDb adapter doesn't fill the datatable. Here is my code:
private void openExcel()
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""C:\MyLocation\myFile.xls"";Extended Properties=""Excel 12.0;HDR=NO;""";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open();string strSQL = "SELECT \* FROM \[Sheet1$\]"; OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand); // create data table DataTable dTable = new DataTable(); dataAdapter.Fill(dTable); aListBoxControl.DataSource = dTable; // dispose used objects dTable.Dispose(); dataAdapter.Dispose(); dbCommand.Dispose(); excelConnection.Close(); excelConnection.Dispose(); }
Could anyone please help me find this? Thanks in advance.
-
I'm trying to read an Excel file in C# Visual Studio 2008 using oldDb. My connection gets opened successfully, but oldDb adapter doesn't fill the datatable. Here is my code:
private void openExcel()
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""C:\MyLocation\myFile.xls"";Extended Properties=""Excel 12.0;HDR=NO;""";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open();string strSQL = "SELECT \* FROM \[Sheet1$\]"; OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand); // create data table DataTable dTable = new DataTable(); dataAdapter.Fill(dTable); aListBoxControl.DataSource = dTable; // dispose used objects dTable.Dispose(); dataAdapter.Dispose(); dbCommand.Dispose(); excelConnection.Close(); excelConnection.Dispose(); }
Could anyone please help me find this? Thanks in advance.
It's probably because your app is being compiled for "AnyCPU" instead of x86. There are no 64-bit drivers for this, and since you cannot mix 64 adn 32 bit code in the same process, you have to force your app to compile for x86 instead of AnyCPU.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
It's probably because your app is being compiled for "AnyCPU" instead of x86. There are no 64-bit drivers for this, and since you cannot mix 64 adn 32 bit code in the same process, you have to force your app to compile for x86 instead of AnyCPU.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Thanks for the fast reply. It is x86. Moreover, dbCommand.ExecuteReader() does read the data.
First, you do not have to open the connection first when using a DataAdapter. The DA will open and close the connection itself. Second, instead of filling a DataTable, change this to use a DataSet instead. I've never done this with an Excel sheet, so there may be something funky going on that I don't know about.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...