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
我的结果
答案 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。