Click here to Skip to main content
6,634,665 members and growing! (16,922 online)
Email Password   helpLost your password?
Database » Database » General     Beginner

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

By Mihadh

This article explains how to connect to Microsoft Excel 2007 using a connection string and populate DataGridView
C# 1.0, C# 2.0.NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, .NET 3.0, .NET 3.5, ADO.NET, WinForms, VS2005, DBA, Dev
Version:2 (See All)
Posted:28 Nov 2007
Views:52,017
Bookmarked:55 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
6 votes for this article.
Popularity: 3.37 Rating: 4.33 out of 5

1

2
1 vote, 16.7%
3
1 vote, 16.7%
4
4 votes, 66.7%
5
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mihadh


Member

Location: Maldives Maldives

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralHow to support different versions of Excel Pinmembermetazone16:02 27 Jan '09  
GeneralFound a bug in vista and VS 2008 Pinmembernewbieprogrammerguy17:05 24 Sep '08  
GeneralRe: Found a bug in vista and VS 2008 Pinmemberashish diwakar20:00 22 Sep '09  
GeneralExcel 2007 - Reading more than 256 columns from a sheet using OLEDB PinmemberFad1234:51 20 May '08  
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmembernishit.amin@successcraft.com5:04 16 Jun '09  
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmemberokay2gonow10:57 22 Jun '09  
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmemberstranger_From_Tepa13:24 6 Nov '09  
GeneralRe: Excel 2007 - Reading more than 256 columns from a sheet using OLEDB Pinmemberstranger_From_Tepa14:10 10 Nov '09  
QuestionCan I read Opened Excel file continuosly? PinmemberMember 458580410:59 14 Jan '08  
GeneralRe: Can I read Opened Excel file continuosly? Pinmembersmramesh13:10 15 Jan '08  
GeneralMixed Field issue? PinmemberEdwardQ7:30 28 Nov '07  
GeneralRe: Mixed Field issue? PinmemberSarafian2:52 29 Nov '07  
GeneralRe: Mixed Field issue? PinmemberMKauffman7:34 29 Nov '07  
GeneralRe: Mixed Field issue? PinmemberMihadh6:42 30 Nov '07  
GeneralRe: Mixed Field issue? PinmemberNair Machaan18:23 9 Dec '07  
AnswerRe: Mixed Field issue? Pinmemberstevechurcher14:04 30 Apr '08  
GeneralRe: Mixed Field issue? PinmemberMihadh10:54 27 Nov '08  
QuestionRe: Mixed Field issue? Pinmemberabhi_chadha14:12 13 Jan '09  
AnswerRe: Mixed Field issue? Pinmembercurro2k0:27 7 Mar '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2007
Editor: Deeksha Shenoy
Copyright 2007 by Mihadh
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project