Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading excel file in Vista x64

Reading excel file in Vista x64

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kmuthuk
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • K kmuthuk

      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.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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...

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        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...

        K Offline
        K Offline
        kmuthuk
        wrote on last edited by
        #3

        Thanks for the fast reply. It is x86. Moreover, dbCommand.ExecuteReader() does read the data.

        D 1 Reply Last reply
        0
        • K kmuthuk

          Thanks for the fast reply. It is x86. Moreover, dbCommand.ExecuteReader() does read the data.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups