Click here to Skip to main content
Click here to Skip to main content

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

, 28 Nov 2007 CPOL
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

Introduction

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.

Code

// 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();

dataAdapter.Fill(dTable);

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

// dispose used objects
dTable.Dispose()
dataAdapter.Dispose();
dbCommand.Dispose();
excelConnection.Close();
excelConnection.Dispose();

Your suggestions and comments are most welcome.

History

  • 28th November, 2007: Initial post 

License

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

Share

About the Author

Mihadh
Software Developer (Senior) Dhivehisoft
Maldives Maldives
No Biography provided

Comments and Discussions

 
QuestionThis could be interesting here: PinprofessionalDietmar Schoder29-Jul-14 11:35 
GeneralMy vote of 1 Pinmembergs_virdi10-Oct-13 0:24 
Question... Pinmemberaditya_prestige22-Dec-12 9:56 
BugGetting Error : Pinmembermansooralikhan5-May-12 22:39 
GeneralRe: Getting Error : Pinmembertaith14-Nov-12 6:02 
QuestionHow to Import Excel data to Ms access Database PinmemberPrabakaranM8-Mar-12 3:34 
Hai sir,
 
I have finish my import coding, but one error should be continue, how to solve that error...pls help me sir,
 
Error:
 
The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
Questioneeception arised after this.... Pinmembertripti_kumbhat18-Feb-12 7:24 
GeneralMy vote of 5 PinmemberAlisettar30-Dec-11 23:01 
QuestionThank You PinmemberfreshonlineMax21-Oct-11 21:28 
QuestionDo i need Access installed on my system for this? PinmemberMember 453129822-Sep-10 1:03 
QuestionRetieve the data from excel 2007 when the worksheet is open [modified] Pinmemberjaanu_simi17-Aug-10 0:57 
GeneralMy vote of 5 Pinmemberdidi212013-Aug-10 7:31 
GeneralAlien Code PinmemberWisam E. Mohammed25-Jun-10 7:49 
QuestionHow to support different versions of Excel Pinmembermetazone27-Jan-09 16:02 
AnswerRe: How to support different versions of Excel PinmemberCikaPero17-Jul-11 23:23 
GeneralFound a bug in vista and VS 2008 Pinmembernewbieprogrammerguy24-Sep-08 17:05 
GeneralRe: Found a bug in vista and VS 2008 Pinmemberashish diwakar22-Sep-09 20:00 
GeneralExcel 2007 - Reading more than 256 columns from a sheet using OLEDB PinmemberFad12320-May-08 4:51 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmembernishit.amin@successcraft.com16-Jun-09 5:04 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmemberokay2gonow22-Jun-09 10:57 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmemberstranger_From_Tepa6-Nov-09 13:24 
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmemberstranger_From_Tepa10-Nov-09 14:10 
QuestionCan I read Opened Excel file continuosly? PinmemberMember 458580414-Jan-08 10:59 
GeneralRe: Can I read Opened Excel file continuosly? Pinmembersmramesh15-Jan-08 13:10 
QuestionMixed Field issue? PinmemberEdwardQ28-Nov-07 7:30 
AnswerRe: Mixed Field issue? PinmemberSarafian29-Nov-07 2:52 
GeneralRe: Mixed Field issue? PinmemberMKauffman29-Nov-07 7:34 
GeneralRe: Mixed Field issue? PinmemberMihadh30-Nov-07 6:42 
GeneralRe: Mixed Field issue? PinmemberNair Machaan9-Dec-07 18:23 
AnswerRe: Mixed Field issue? Pinmemberstevechurcher30-Apr-08 14:04 
GeneralRe: Mixed Field issue? PinmemberMihadh27-Nov-08 10:54 
QuestionRe: Mixed Field issue? Pinmemberabhi_chadha13-Jan-09 14:12 
AnswerRe: Mixed Field issue? Pinmembercurro2k7-Mar-09 0:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 28 Nov 2007
Article Copyright 2007 by Mihadh
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid