c# - 使用 oledb 命令从 excel 中读取

标签 c# excel

在下面的代码中而不是指定选项卡名称.. 无论如何我们可以只说“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/

相关文章:

C++ Excel 加载项加载错误 : XLL file is loaded by Excel as text file

vba - 在运行时向工作表添加命令按钮并定义事件

c# - 将 WinForm 应用程序转换为 WPF ViewModel

c# - C#中如何获取某个目录下所有文件的扩展名?

c# - WPF C#将所有I/O音频设备静音/取消静音

excel - Excel 中的分箱

c# - 未在 C# 中设置对象属性

c# - Unity脚本MonoBehaviour报错

excel - 引用一组非连续行的紧凑方法是什么?

excel - 用 yes no 绘制分类数据