Click here to Skip to main content
13,351,050 members (67,830 online)
Click here to Skip to main content
Add your own
alternative version


92 bookmarked
Posted 28 Nov 2007

How to Read Microsoft Excel 2007 Using a Connection String in C#

, 28 Nov 2007
Rate this:
Please Sign up or sign in to vote.
This article explains how to connect to Microsoft Excel 2007 using a connection string and populate DataGridView
Screenshot - readExcel1.gif


This article explains how to connect to Microsoft Excel 2007 using ADO.NET Connection string and populate a DataGridView on a form.

Using the Code

First create a C# Windows application using Visual Studio. Add the following controls to the main form from the tool box.

  • DataViewGrid control (name as dgvExcelList)
  • BindingSource control (name as dataBindingSrc)
  • Button control (name as btnPopulate)

Now open Microsoft Excel 2007 and enter a few records. Save the file and close Excel.

I have already created a sample Excel 2007 file which is available in the project folder of the demo project. If you want, you can use it by copying to C:\. The name of the Excel file is Members.xlsx.

Copy the following codes into the btnPopulate_Click event and run the application. you will find the dgvExcelList is filled with all the entries in Sheet1 of the Excel file.

Note: If you want to use your own Excel file or save the Members.xlsx file in a different location other than C:\, change the file and path name from the connection string.


// You can change C:\Members.xlsx to any valid path 
// where the file is located.

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
    FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'">
				Data Source=C:\Members.xlsx;Extended
    FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 'Courier New'">
				Properties=""Excel 12.0;HDR=YES;"""; 
// if you don't want to show the header row (first row) in the grid
// use 'HDR=NO' in the string

string strSQL = "SELECT * FROM [Sheet1$]";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open(); // this will open an Excel file
OleDbCommand dbCommand = new OleDbCommand(strSQL,excelConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);

// create data table
DataTable dTable = new DataTable();


// bind the datasource
dataBingingSrc.DataSource = dTable;
// assign the dataBindingSrc to the DataGridView
dgvExcelList.DataSource = dataBingingSrc;

// dispose used objects

Your suggestions and comments are most welcome.


  • 28th November, 2007: Initial post 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Dhivehisoft
Maldives Maldives
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThis could be interesting here: Pin
Dietmar Schoder29-Jul-14 11:35
professionalDietmar Schoder29-Jul-14 11:35 
GeneralMy vote of 1 Pin
gs_virdi10-Oct-13 0:24
membergs_virdi10-Oct-13 0:24 
Question... Pin
aditya_prestige22-Dec-12 9:56
memberaditya_prestige22-Dec-12 9:56 
BugGetting Error : Pin
mansooralikhan5-May-12 22:39
membermansooralikhan5-May-12 22:39 
GeneralRe: Getting Error : Pin
taith14-Nov-12 6:02
membertaith14-Nov-12 6:02 
QuestionHow to Import Excel data to Ms access Database Pin
PrabakaranM8-Mar-12 3:34
memberPrabakaranM8-Mar-12 3:34 
Questioneeception arised after this.... Pin
tripti_kumbhat18-Feb-12 7:24
membertripti_kumbhat18-Feb-12 7:24 
GeneralMy vote of 5 Pin
Alisettar30-Dec-11 23:01
memberAlisettar30-Dec-11 23:01 
QuestionThank You Pin
freshonlineMax21-Oct-11 21:28
memberfreshonlineMax21-Oct-11 21:28 
Very Well
QuestionDo i need Access installed on my system for this? Pin
Member 453129822-Sep-10 1:03
memberMember 453129822-Sep-10 1:03 
QuestionRetieve the data from excel 2007 when the worksheet is open [modified] Pin
jaanu_simi17-Aug-10 0:57
memberjaanu_simi17-Aug-10 0:57 
GeneralMy vote of 5 Pin
didi212013-Aug-10 7:31
memberdidi212013-Aug-10 7:31 
GeneralAlien Code Pin
Wisam E. Mohammed25-Jun-10 7:49
memberWisam E. Mohammed25-Jun-10 7:49 
QuestionHow to support different versions of Excel Pin
metazone27-Jan-09 16:02
membermetazone27-Jan-09 16:02 
AnswerRe: How to support different versions of Excel Pin
CikaPero17-Jul-11 23:23
memberCikaPero17-Jul-11 23:23 
GeneralFound a bug in vista and VS 2008 Pin
newbieprogrammerguy24-Sep-08 17:05
membernewbieprogrammerguy24-Sep-08 17:05 
GeneralRe: Found a bug in vista and VS 2008 Pin
ashish diwakar22-Sep-09 20:00
memberashish diwakar22-Sep-09 20:00 
GeneralExcel 2007 - Reading more than 256 columns from a sheet using OLEDB Pin
Fad12320-May-08 4:51
memberFad12320-May-08 4:51 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pin
nishit.amin@successcraft.com16-Jun-09 5:04
membernishit.amin@successcraft.com16-Jun-09 5:04 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pin
okay2gonow22-Jun-09 10:57
memberokay2gonow22-Jun-09 10:57 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pin
stranger_From_Tepa6-Nov-09 13:24
memberstranger_From_Tepa6-Nov-09 13:24 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pin
stranger_From_Tepa10-Nov-09 14:10
memberstranger_From_Tepa10-Nov-09 14:10 
QuestionCan I read Opened Excel file continuosly? Pin
Member 458580414-Jan-08 10:59
memberMember 458580414-Jan-08 10:59 
GeneralRe: Can I read Opened Excel file continuosly? Pin
smramesh15-Jan-08 13:10
membersmramesh15-Jan-08 13:10 
QuestionMixed Field issue? Pin
EdwardQ28-Nov-07 7:30
memberEdwardQ28-Nov-07 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 28 Nov 2007
Article Copyright 2007 by Mihadh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid