用c#读取excel,第5列是空的

时间:2014-03-31 14:06:43

标签: c# excel

我检查了一千次。但无法解决。我用这个

阅读excel
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]都可以。但最后一栏变空了。

这是我的excel文件 https://drive.google.com/file/d/0B4aRFZv0snnFMEFMdF82R0xPbmc/edit?usp=sharing

我的结果

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试将IMEX=1添加到您的连接字符串中:

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;'";

更多信息here