Wednesday, December 4, 2013

Read Excel 2010 64 bit File on a 64 Bit Machine using OleDB Provider in C#

In the forums where I am a contributor, here's a tip on how to read an excel 2010 64/32 bit file using C#. The Operating System of my machine is Windows 8 64 bit. So, here are the steps.
1. Download AccessDatabaseEngine_x64.exe
For 32 bit excel on windows 8 64 bit machines, download AccessDatabaseEngine.exe
2. Install it in my laptop.
3. Restart my laptop.
3. Change Active Config of Visual Studio Solution to Debug|Any CPU.
For 32 bit excel, choose Debug|X86 4. Clean and Rebuild The Solution.
Code:
Code:
     private void Form1_Load(object sender, EventArgs e)  
     {  
       try  
       {  
         System.Data.OleDbOleDbConnection MyConnection;  
         System.DataDataSet DtSet;  
         System.Data.OleDbOleDbDataAdapter MyCommand;  
         MyConnection = new System.Data.OleDbOleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='c:\\temp\\test.xlsx';Extended Properties=Excel 12.0;");  
         MyCommand = new System.Data.OleDbOleDbDataAdapter("select * from [Sheet1$]", MyConnection);  
         //MyCommand.TableMappings.Add("Table", "TestTable");  
         DtSet = new System.DataDataSet();  
         MyCommand.Fill(DtSet);  
         dataGridView1.DataSource = DtSet.Tables[0];  //load excel file to datagridview
         MyConnection.Close();  
       }  
       catch (Exception ex)  
       {  
       }  
     }  
Run my program.
Cheers!

Greg

0 comments:

Post a Comment