Click here to Skip to main content
15,031,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello viewers...I m try to Import Excel File in a C# based Window Form in using control DataGridView...so any Idea or views please share me....thank you.
Posted

C#
//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);
                   }
                   else
                   {
                       MessageBox.Show("chose Excel sheet path..", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   }
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }


then..write this code in button Click Event to show in DataGridview....
C#
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();
                   dataAdapter.Fill(dataSet);
                   DataTable dtView = dataSet.Tables[0];
                   if (dtView.Rows.Count > 0)
                   {
                       dgrdReciver.Rows.Clear();
                       dgrdReciver.Rows.Add(dtView.Rows.Count);
                       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"];
                           i++;
                       }

                   }
               }
               else
               {
                   MessageBox.Show("No File is Selected");
               }
   
v2
Comments
Karthik Bilakanti 13-Nov-14 1:28am
   
GetSheetNames(openDialog.FileName);
where is the GetSheetNames method and what does it do
   
Comments
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", "Net-informations.com");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dataGridView1.DataSource = DtSet.Tables[0];
MyConnection.Close();

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)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900