Re: Reading an Excel file in C#

"Gregory A. Brewer" <[email protected]> Fri, 13 Apr 2007 16:57:27 -0400
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
I am using the oledb provider to read data from excel, but it should be
similar.  It looks like you may not be assigning your connection to your
command object, but there is not enough code to tell.

string connectionString =
                string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source = {0}; Extended Properties=Excel 8.0;", info.FileName);

            OleDbConnection objConn = new
OleDbConnection(connectionString);

            using (objConn)
            {
                objConn.Open();

                OleDbCommand objCmdSelect = new
OleDbCommand(string.Format("SELECT * FROM [{0}$]", info.Sheet),
objConn);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
                objAdapter1.SelectCommand = objCmdSelect;
                excelData = new DataSet();
                objAdapter1.Fill(excelData);
            }


Gregory A Brewer
Managing Member
Vowire LLC
(P) 810-229-2400
(F) 810-229-7791
http://www.vowire.com
mailto:[email protected]



-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Meguiar,
Richie
Sent: Friday, April 13, 2007 4:32 PM
To: [email protected]
Subject: [DOTNET-CX] Reading an Excel file in C#

I'm trying to read in an excel file with no luck..  Here is the code I'm
using to setup the connection and commandtext..  Any ideas what I'm
doing wrong?

 

 

string ConnectionString = @"Driver={Microsoft Excel Driver
(*.xls)};DBQ=c:\test.xls";

Microsoft.Data.Odbc.OdbcConnection conn = new
Microsoft.Data.Odbc.OdbcConnection(ConnectionString);

conn.Open(); 

Microsoft.Data.Odbc.OdbcCommand cmd = new
Microsoft.Data.Odbc.OdbcCommand();

cmd.CommandText = "select * from Sheet1";

 


Confidentiality Notice: The information contained in this message is

intended only for the use of the addressee, and may be confidential

and/or privileged. If the reader of this message is not the intended

recipient, or the employee or agent responsible to deliver it to the

intended recipient, you are hereby notified that any dissemination,

distribution or copying of this communication is strictly prohibited.

If you have received this communication in error, please notify the

sender immediately.

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com