這對我有效。您可以根據需要更改SELECT
語句(特定行,多列,...)以獲取單個結果或返回的一組行。
string connectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=test.xls;";
connectionString += "Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(connectionString);
DataSet ds = new DataSet("stuff");
OleDbDataAdapter adapter = new OleDbDataAdapter();
// adapter.SelectCommand = new OleDbCommand("Select * from [Sheet1$A1:A100];", con);
// adapter.SelectCommand = new OleDbCommand("Select * from [Sheet1$];", con);
adapter.SelectCommand = new OleDbCommand("Select * from [Sheet1$] where [A] = 'John';", con);
adapter.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
foreach (DataColumn dc in ds.Tables[0].Columns)
{
Console.WriteLine(dr[dc].ToString());
}
}
不工作請幫我excelCommand.CommandText = @「SELECT * FROM」+ SpreadSheetName +「Where RowNumber = 3」; – Chicharito 2010-08-06 12:55:14
錯誤:未給出一個或多個必需參數的值。 – Chicharito 2010-08-06 12:57:31
可以嘗試這樣的語法:SELECT * FROM [+「SpreadSheetName」+ $ A3:B3] 其中$ A3:B3 - 單元格範圍 – Anton 2010-08-06 14:15:58