Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Retrieving Data from a Microsoft Excel Workbook

using System; using System.Data; using System.Data.OleDb;     class Program     {         static void Main(string[] args)         {             string oledbConnectString = "Provider=Microsoft.ACE.OLEDB.12.0;" +                 @"Data Source=..\Category.xlsx;" +                 "Extended Properties=\"Excel 12.0;HDR=YES\";";             string commandText = "SELECT CategoryID, CategoryName, " +                 "Description FROM [Sheet1$]";             OleDbConnection connection = new OleDbConnection(oledbConnectString);             OleDbCommand command = new OleDbCommand(commandText, connection);             connection.Open();             OleDbDataReader dr = command.ExecuteReader( );             while (dr.Read()){                 Console.WriteLine(dr["CategoryID"]);                 Console.WriteLine(dr["CategoryName"]);                 Console.WriteLine(dr["Description"]);             }             connection.Close( );         }     }