Start New Discussion within our Software Development Community

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();
                dataAdapter.Fill(dataSet);
                dgvMain.DataSource = dataSet.Tables[0];
}

:sad:

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.

This article has been dead for over six months. Start a new discussion instead.