Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C#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
0 Sergey Alexandrovich Kryukov 861
1 OriginalGriff 410
2 CPallini 255
3 Volynsky Alex 205
4 Richard Deeming 175
0 OriginalGriff 5,540
1 Sergey Alexandrovich Kryukov 4,668
2 CPallini 4,540
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2014
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