Hakkı
Hakkı

Reputation: 840

reading excel with c#, 5th column is empty

I checked thousand times. But can't solve. I'm reading excel with this

string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\YEVMIYE.xls;Extended Properties='Excel 8.0;HDR=Yes;'";
        using (OleDbConnection connection = new OleDbConnection(con))
        {
            connection.Open();
            //OleDbCommand command = new OleDbCommand("select * from [RAPOR01$]", connection);
            OleDbCommand command = new OleDbCommand("select [B], [C], [D], [E], [F] from [RAPOR01$]", connection);
            using(OleDbDataReader dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    var row1Col1 = dr[0];
                    var row1Col2 = dr[1];
                    var row1Col3 = dr[2];
                    var row1Col4 = dr[3];
                    var row1Col5 = dr[4];

                    listBox1.Items.Add(row1Col1);
                    listBox2.Items.Add(row1Col2);
                    listBox3.Items.Add(row1Col3);
                    listBox4.Items.Add(row1Col4);
                    listBox5.Items.Add(row1Col5);
                }
                listBox1.SelectedIndex = 0;
            }
        }

[B], [C], [D], [E] are ok. But last column getting empty.

This is my excel file https://drive.google.com/file/d/0B4aRFZv0snnFMEFMdF82R0xPbmc/edit?usp=sharing

And my results

enter image description here

Upvotes: 1

Views: 440

Answers (1)

Peter
Peter

Reputation: 12711

Try adding IMEX=1 to your connection string:

string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\peterb\Downloads\YEVMIYE.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";

More info here.

Upvotes: 2

Related Questions