SIGN UP MEMBER LOGIN:    
Blog

Connecting To Excel 2007 files via C#

Posted by Ibrahim Ersoy Blogs | Office Development Aug 12, 2007
Well I think this will help you in your company if you have problems connecting excel 2007 files.

First of all we are declaring the ODBC namespace which we will use an Excel DNS; 

using System.Data.Odbc;

then we will write these codes for connecting to a specific Excel file;

String strConn = @"Dsn=Excel Files;dbq=excel_file_path;defaultdir=excel_file_dir;driverid=1046;maxbuffersize=2048;pagetimeout=5";
            OdbcConnection objConn = new OdbcConnection(strConn);
            objConn.Open();
            OdbcDataAdapter adp = new OdbcDataAdapter("select * from [Sheet1$]", objConn);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            objConn.Close();

Thats all by using these codes you have got the datas you wanted to...

Cheers;)

 

share this blog :
post comment
 

Tesekkurler Ibrahim Bey

Posted by Ilkin East Jul 12, 2011

here theres an example which can help u too

try
                {

                    OleDbConnection baglanti = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +file+ ";Excel 12.0;HDR=YES;");
                    baglanti.Open();
                    OleDbDataAdapter adp = new OleDbDataAdapter("select * from [" + Table+ "]", baglanti);
                    DataSet ds2 = new DataSet();
                    adp.Fill(ds2);
                    dataGridView1.DataSource = ds2.Tables[0];
                }
                catch (StackOverflowException stack_ex2)
                {
                    MessageBox.Show("(2007 Excel file) Stack Overflowed!" + "\n" + stack_ex2.Message);

                }
                catch (OleDbException ex_oledb2)
                {
                    MessageBox.Show("An OleDb Error Thrown!" + "\n" + ex_oledb2.Message);

                }

Posted by Ibrahim Ersoy Aug 14, 2007