Vous êtes sur la page 1sur 8

Import -XChang from Excel to DataGridView in C# Data F

PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

lic

lic

Total Online Users: 3998


o
.c

Article Submission Guidelines


w
w
w

to

to

bu

N
.d o

O W !
c u -tr a c k
.c

.d o

c u -tr a c k

Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training | Consulting Hi! Naratip Logout My Account My Messages (0) Jump to Technology Website Sponsored by Print Email to a friend Post a comment Bookmark Search : Home Learn .NET Import Data from Excel to DataGridView in C#

Import Data from Excel to DataGridView in C#


By Ankur Gupta July 20, 2010
In this article you will learn how to import Data from Excel to DataGridView in C#.

Download Files:

Import From Excel.zip

First need to add the reference "Microsoft ADO Ext. 2.8". You can easily add it from Add an open Dialog box control on form

Put the following code on Browser button click events.. private void button1_Click_1(object sender, EventArgs e) { OpenFileDialog fdlg = new OpenFileDialog(); fdlg.Title = "Select file"; fdlg.InitialDirectory = @"c:\";

3/12/2554 21:17

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

.d o

c u -tr a c k

.c

fdlg.FileName = txtFileName.Text; fdlg.Filter = "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*"; fdlg.FilterIndex = 1; fdlg.RestoreDirectory = true; if (fdlg.ShowDialog() == DialogResult.OK) { txtFileName.Text = fdlg.FileName; Import(); Application.DoEvents(); } } This will filter only Excel file from your Machine.

to

lic

lic

to

bu

N
w

O W !
.d o

c u -tr a c k

.c

This Excel file can contains more than one Sheet. You need to add another form to al can select any one excel sheet which he want to import. Write the following code on Page Load even of this form private void Select_Tables_Load(object sender, EventArgs e) { if (!DataTables) { if (Tables != null) { for (int tables = 0; tables < Tables.Length; tables++) { try { ListViewItem lv = new ListViewItem(); lv.Text = Tables[tables].ToString(); lv.Tag = tables; lstViewTables.Items.Add(lv); } catch (Exception ex) {} } } } else { if (dtTable.Rows.Count>0) { for (int tables = 0; tables < dtTable.Rows.Count; tables++) { try { ListViewItem lv = new ListViewItem(); lv.Text = dtTable.Rows[tables][0].ToString(); lv.Tag = dtTable.Rows[tables][0]; lstViewTables.Items.Add(lv); } catch (Exception ex) {} } } } }

Become a Sponsor Resources About Us

3/12/2554 21:17

w
w

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

ASP.NET Hosting Authors .c .d o c u -tr a c k Book Chapters Book Reviews C# Consulting C# Training Downloads Media Kit News & Events Prizes Product Reviews Resources Tips Tools User Groups
to k lic
m
o

By the following function we can find the total sheets in Excel file.

lic

to

bu

N
w
.d o

O W !
c u -tr a c k
.c

Our Network .NET Heaven C# Corner DbTalks Interview Corner Longhorn Corner Mindcracker VB.NET Heaven

public static string[] GetTableExcel(string strFileName) { string[] strTables = new string[100]; Catalog oCatlog = new Catalog(); ADOX.Table oTable = new ADOX.Table(); ADODB.Connection oConn = new ADODB.Connection(); oConn.Open("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + strFileName + oCatlog.ActiveConnection = oConn; if (oCatlog.Tables.Count > 0) { int item = 0; foreach (ADOX.Table tab in oCatlog.Tables) { if (tab.Type == "TABLE") { strTables[item] = tab.Name; item++; } } } return strTables; }

Following function return a dataset so that you can bind it from Data Grid View easily.

public static DataTable GetDataTableExcel(string strFileName, string Table) { System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConn Extended Properties = \"Excel 8.0;HDR=Yes;IMEX=1\";"); conn.Open(); string strQuery = "SELECT * FROM [" + Table + "]";

3/12/2554 21:17

w
w

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

.d o

c u -tr a c k

.c

System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb. System.Data.DataSet ds = new System.Data.DataSet(); w .c .d o c u -tr a c k adapter.Fill(ds); return ds.Tables[0];
C lic k to

lic

to

bu

N
o

O W !

To see the complete source code you can download the zip file.

Comment Request!

Thank you for reading this post. Please post your feedback, question, or comments about this post
Add your contents and source code to this article [Top] About the author Ankur Gupta I am Ankur Gupta Working in MNC as a sr. software engg. My major area of experiance in Accounting and CRM with C#,VB.Net,WCF,ASP.net languages.

Nevron Chart for .NET 2010.1 Now Available The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datas apps. Download evaluation now.

Post a Feedback, Comment, or Question about this article

3/12/2554 21:17

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

to

Subject:
w
w

lic

lic

to

bu

N
.c

O W !
w

.d o

c u -tr a c k

Comment:

.d o

c u -tr a c k

.c

Submit

Comments

Another way to import Excel from .NET application by Filip On September 27, 2 Another way to import Excel from .NET application is with this Excel .NET library. It is m Here is a sample Excel C# code how to import DataSet from Excel:
var ef = new ExcelFile(); ef.LoadXls("Excel file.xls");

// DataSet schema has to be defined before this. for (int i = 0; i < ef.Worksheets.Count; ++i) { var ws = ef.Worksheets[i]; ws.ExtractToDataTable(dataSet.Tables[i], ws.Rows.Count, ExtractDataOptions.StopAtFirstEm }

Re: Another way to import Excel from .NET application by Onair On February 8 ?????

question regarding in exporting excel by rolando On October 11, 2010 sir if i run the example program it prompt that the "Provider cannot be found.it may

Re: question regarding in exporting excel by Ankur On October 12, 2010 What version of office you are using ?

3/12/2554 21:17

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

to

lic

.d o

c u -tr a c k

.c

w Re: Re: question regarding in exporting excel by rolando On Octobero30, 2010 .c .d c u -tr a c k sir MS 2007.what can I do to run this example..

lic

to

bu

N
o

O W !

Re: Re: question regarding in exporting excel by Chaume On January 11, 2011 It wont open with *.xlsx extension

Re: question regarding in exporting excel by Ankur On October 12, 2010 What version of office you are using ?

Re: Re: question regarding in exporting excel by Mohammad On October 12, 20 Thanks for this useful code

Re: question regarding in exporting excel by Christian On October 27, 2010 Hello all the idea for the code is very good! But I have the same problem that the provider cannot be found. I work primary with office 2007 and secondary with office 2003.
I read that the missing of the MDAC could be the reason of the problem. But my system is windows 7 so i would think that i have the latest version of the MDAC. Can somebody help me please? chris

Re: Re: question regarding in exporting excel by Ankur On September 14, 2011 if you are working with 64 bit machine then you need to change target plateform x86

thanxx.... by usman On February 3, 2011 have the same error....as discussed aboveee......what to do????

3/12/2554 21:17

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

to

lic

lic

to

bu

N
.c

O W !
w
.d o

.d o

c u -tr a c k

c u -tr a c k

.c

delete row by elnaz On February 8, 2011 hi how to delete a row of gridview and then it deleted from excel?

combo box use to read header line in excel by nishant On September 13, 2011 i having combo box,listbox and (Add) button & i want to show excel data within combo me

Error Showing in import application following by your code by nishant if i m using this library System.Windows.Documents & System.Windows.Controls this tw the namespace 'System.Windows' (are you missing an assembly reference?) D:\Nis 123Application & Error 2 The type or namespace name 'Controls' does not exist in the \C#\Windows Forms Application\123Application\123Application\Form1.cs 10 22 123App

Error Showing in import application following by your code by nishant if i m using this library System.Windows.Documents & System.Windows.Controls this tw the namespace 'System.Windows' (are you missing an assembly reference?) D:\Nis 123Application & Error 2 The type or namespace name 'Controls' does not exist in the \C#\Windows Forms Application\123Application\123Application\Form1.cs 10 22 123App

Error Showing in import application following by your code by nishant if i m using this library System.Windows.Documents & System.Windows.Controls this tw the namespace 'System.Windows' (are you missing an assembly reference?) D:\Nis 123Application & Error 2 The type or namespace name 'Controls' does not exist in the \C#\Windows Forms Application\123Application\123Application\Form1.cs 10 22 123App

Hosted by MaximumASP | Found a broken link? | Contact Us | Terms & conditions | Privacy Policy | Site Map | Suggest an Idea | Advertise With Us 2011 contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved. Current Version: 5.2011.8.6

Channels: Jobs | Interviews | Consulting | Training | Photos | Authors | Tips | Forums | Blogs Programming: C# | Visual Basic | ASP.NET & Web Development | C++ | Other .NET Languages | Windows Vista | XAML | Tutorials Sponsors:

3/12/2554 21:17

w
w

w
w

Import -XChang from Excel to DataGridView in C# Data F


PD
e

http://www.c-sharpcorner.com/uploadfile/ankurmee/import-data-... F -X C h a n g
PD
e

O W !

bu

to

lic

lic

to

bu

N
.c

O W !
w
.d o

.d o

c u -tr a c k

c u -tr a c k

.c

3/12/2554 21:17

w
w

w
w

Vous aimerez peut-être aussi