Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0C#4.0
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 12-Jan-13 18:39pm
Rate this: bad
good
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);
                   }
                   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....
 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");
                }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
sonu Ranjan at 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 at 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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 339
1 Sergey Alexandrovich Kryukov 285
2 OriginalGriff 280
3 Peter Leow 171
4 Abhinav S 168
0 Sergey Alexandrovich Kryukov 8,979
1 OriginalGriff 5,280
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid