使用oledbcommand从Excel读取 [英] Reading from excel using oledbcommand

查看:54
本文介绍了使用oledbcommand从Excel读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,而不是指定选项卡名称..无论如何,我们是否只能说从[tab1]中选择*"?选项卡名称可能是什么.

In the below code instead of specifying the tab name.. Is there anyway we can just say "select * from [tab1]"? what ever the tab name might be..

 OleDbCommand excelOledbCommand =
                            new OleDbCommand("Select * From [Sheet1$]", excelOledbCon);

推荐答案

这可能有帮助

使用ADO.NET读取Excel电子表格的提示

OleDbConnection.GetOleDbSchemaTable方法

类似

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 ();
}

这篇关于使用oledbcommand从Excel读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆