Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to import data from excel to datagrid and then to mysql

C#
if ( openFileDialog1.ShowDialog ( ) == DialogResult.OK )
    {
    System.IO.StreamReader sr = new
        System.IO.StreamReader ( openFileDialog1.FileName );
    MessageBox.Show ( sr.ReadToEnd ( ) );
    sr.Close ( );
    }
            
System.Data.OleDb.OleDbConnection MyConnection;

System.Data.DataSet DtSet;
textBox1.Text = openFileDialog1.FileName;
System.Data.OleDb.OleDbDataAdapter MyCommand;

MyConnection = new System.Data.OleDb.OleDbConnection ( 
                   @"provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + 
                   openFileDialog1.FileName + 
                   "';Extended Properties=Excel 8.0;");

MyCommand = new System.Data.OleDb.OleDbDataAdapter ( 
                "select * from [Sheet1$]", MyConnection );

MyCommand.TableMappings.Add ( "Table",                     //error here  
                              "openFileDialog1.FileName");  
DtSet = new System.Data.DataSet();

MyCommand.Fill(DtSet);

praktikanDataGridView.DataSource = DtSet.Tables[0];

MyConnection.Close();

The error was:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: External table is not in the expected format.
Posted
Updated 8-Jun-14 2:27am
v2
Comments
[no name] 8-Jun-14 8:30am    
You probably need to use Microsoft.Jet.OLEDB.12.0

 
Share this answer
 
C#
// ************************** copy_excel_sheet_to_datagridview

void copy_excel_sheet_to_datagridview ( )
    {
    string  sheet_name = "Sheet1";

    if ( !retrieved_excel_filename ( ) )
        {
        return;
        }

    if ( !sheet_name_valid ( sheet_name ) )
        {
        return;
        }

    try
        {
        OleDbConnection connection = null;
        StringBuilder   connection_string = 
                            new StringBuilder ( );
        DataSet         data_set = new DataSet ( );
        string          select_command = String.Empty;

        connection_string.AppendFormat (  
            "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source='{0}';" + 
            "Extended Properties=Excel 8.0;",
            excel_filename );
        select_command = String.Format ( 
                                    "select * from [{0}$]",
                                    sheet_name );
        try
            {
            connection = new OleDbConnection ( 
                                   connection_string.
                                   ToString ( ) );
            }
       catch ( Exception ex )
            {
            MessageBox.Show ( String.Format (
                                  "{0}{1}{2}",
                                  ex.Message,
                                  Environment.NewLine,
                                  ex.StackTrace ),
                              "Failed to create connection" );
            return;
            }

        try
            {
            OleDbDataAdapter adapter = null;
            OleDbCommand     command = null;

            command = new OleDbCommand ( select_command, 
                                         connection );
            adapter = new OleDbDataAdapter ( command );
            connection.Open ( );
            adapter.Fill ( data_set );
            }
       catch ( Exception ex )
            {
            MessageBox.Show ( String.Format (
                                  "{0}{1}{2}",
                                  ex.Message,
                                  Environment.NewLine,
                                  ex.StackTrace ),
                              "Failed to retrieve data" );
            return;
            }
        finally
            {
            connection.Close();
            }

        dataGridView1.DataSource = null;
        dataGridView1.DataSource = data_set.Tables [ 0 ];
        }
    catch ( Exception ex )
        {
        MessageBox.Show ( String.Format (
                              "{0}{1}{2}",
                              ex.Message,
                              Environment.NewLine,
                              ex.StackTrace ),
                          "Failed setup" );
        return;
        }
    }
 
Share this answer
 

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