1

I have the following code which will import my excel file. But for some reason it will not show all of the data from the orginal spread sheet.

Here is the original spreadsheet:

Original Spreadsheet

Here is the output:

Output

For some reason some of the columns under the benefit period months get lost or do not show up.

 private void btnLoadExcel_Click(object sender, EventArgs e){ string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtPath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";"; OleDbConnection conn = new OleDbConnection(pathConn); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + txtSheet.Text+ "$]",conn); conn.Open(); DataTable dt = new DataTable(); myDataAdapter.Fill(dt); dataGridView1.AutoResizeColumns(); dataGridView1.DataSource = dt; for (int i = 0; i < dataGridView1.Columns.Count - 1; i++) { dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; } dataGridView1.Columns[dataGridView1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; for (int i = 0; i < dataGridView1.Columns.Count; i++) { int colw = dataGridView1.Columns[i].Width; dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; dataGridView1.Columns[i].Width = colw; } } 

1 Answer 1

1

Copying your code produced the same results you explained. The only change I made was to the pathConn string by adding the parameter IMEX=1 which will set the column types as strings. I am guessing that the columns may have different “types” for different rows in the excel file.

string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtPath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\";"; 

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.