HI Friends..i want to import an excel file to DataGridView .In My code its working Fine For all excel sheet that have less number of columns...but its showing error(External table is not in the expected format.) for excel sheet having more number of columns...please help me...my code is below

private void btnbrowse_Click(object sender, EventArgs e)
          DialogResult dr = this.ofdopen.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
                string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", ofdopen.FileName);
                string query = String.Format("select * from [{0}$]", "Sheet1");
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                DataSet dataSet = new DataSet();
                //DataTable dtbl = new DataTable();
                dgvMain.DataSource = dataSet.Tables[0];


I had a similiar problem before, I don't think you can really do anything about that, it just doesn't read it if it's too big.

You can, however, get around this issue if you open multiple tables in your Excel file, and just read them in a loop, however, all I needed to do was load the values into a List of Lists, and not a dataset, so I could easily push each column from each table and its' values into the lists, but I'm not sure how well that would work if you split your tables.
Still a room for thought, maybe you have a way you can twick this with too.

Unfortunately, I can't give you the code I used, as this was done in my work PC that has no Internet connection.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.