在下面的代码中而不是指定选项卡名称.. 无论如何我们可以只说“select * from [tab1]”吗?标签名称可能是什么..
OleDbCommand excelOledbCommand =
new OleDbCommand("Select * From [Sheet1$]", excelOledbCon);
最佳答案
这可能有帮助
Tips for reading Excel spreadsheets using ADO.NET
OleDbConnection.GetOleDbSchemaTable Method
有点像
OleDbConnection dbConnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\BAR.XLS;Extended Properties=""Excel 8.0;HDR=Yes;""");
dbConnection.Open ();
try
{
// Get the name of the first worksheet:
DataTable dbSchema = dbConnection.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception ("Error: Could not determine the name of the first worksheet.");
}
string firstSheetName = dbSchema.Rows [0] ["TABLE_NAME"].ToString ();
// Now we have the table name; proceed as before:
OleDbCommand dbCommand = new OleDbCommand ("SELECT * FROM [" + firstSheetName + "]", dbConnection);
OleDbDataReader dbReader = dbCommand.ExecuteReader ();
// And so on...
}
finally
{
dbConnection.Close ();
}
关于c# - 使用 oledb 命令从 excel 中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2429713/