Click here to Skip to main content
12,353,607 members (69,084 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0
Hello viewers...I m try to Import Excel File in a C# based Window Form in using control any Idea or views please share me....thank you.
Posted 12-Jan-13 18:39pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

//using C# code..Import Excel File in using open file dialog and save Excel sheet name in combo box...

OpenFileDialog openDialog = new OpenFileDialog();
               openDialog.Title = "Select file";
               openDialog.InitialDirectory = @"c:\";
               openDialog.Filter = "Excel Sheet(*.xlsx)|*.xlsx|All Files(*.*)|*.*";
               openDialog.FilterIndex = 1;
               openDialog.RestoreDirectory = true;
               if (openDialog.ShowDialog() == DialogResult.OK)
                   if (openDialog.FileName != "")
                       strExcelPathName = openDialog.FileName;
                       cmbExcelSheet.DataSource = GetSheetNames(openDialog.FileName);
                       MessageBox.Show("chose Excel sheet path..", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
           catch (Exception ex)

then..write this code in button Click Event to show in DataGridview....
if (System.IO.File.Exists(strExcelPathName))
                   string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""",strExcelPathName);
                   //string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);
                   string query = String.Format("select * from [{0}$]",cmbExcelSheet.SelectedItem);
                   OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                   DataSet dataSet = new DataSet();
                   DataTable dtView = dataSet.Tables[0];
                   if (dtView.Rows.Count > 0)
                       int i = 0;
                       foreach (DataRow drow in dtView.Rows)
                           //dgrdReciver.Rows[i].Cells["SerialNo"].Value = j;
                           dgrdReciver.Rows[i].Cells["Addressid"].Value = drow["ADRSID"];
                           dgrdReciver.Rows[i].Cells["ReciverName"].Value = drow["NAME"];
                           dgrdReciver.Rows[i].Cells["companyName"].Value = drow["COMPANY"];
                           dgrdReciver.Rows[i].Cells["RecvAddress"].Value = drow["ADDRESS"];
                           dgrdReciver.Rows[i].Cells["ReciverState"].Value = drow["STATE"];
                           dgrdReciver.Rows[i].Cells["ReciverCity"].Value = drow["CITY"];
                           dgrdReciver.Rows[i].Cells["RecvCountryCode"].Value = drow["CONTRY CODE"];
                           dgrdReciver.Rows[i].Cells["ReciverPostal"].Value = drow["POSTAL CODE"];
                           dgrdReciver.Rows[i].Cells["RecvContactNo"].Value = drow["CONTACT NO"];
                   MessageBox.Show("No File is Selected");
Karthik Bilakanti 13-Nov-14 1:28am
where is the GetSheetNames method and what does it do
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

sonu Ranjan 13-Jan-13 2:00am
I am using this code...
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+excelFileName+"';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "");
DtSet = new System.Data.DataSet();
dataGridView1.DataSource = DtSet.Tables[0];

try to import xls file..but code is treble and Given exception....

External table is not in the expected format.

so do you have any idea please share me...How it is possible...
ridoy 13-Jan-13 7:32am
I hope you find your answer from previous solution.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100