excel - 使用 OleDbConnection::GetOleDbSchemaTable() 列出 excel 表

标签 excel ado.net oledb

以下 PowerShell 片段将通过 OleDbConnection.GetOleDbSchemaTable() 列出 Excel 电子表格中的所有工作表和命名范围。 :

$file = "C:\Users\zippy\Documents\Foo.xlsx";
$cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$($file)`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";";


$cn = New-Object System.Data.OleDb.OleDbConnection $cnStr;
$cn.Open();

# to list the sheets
$worksheets = $cn.GetOleDbSchemaTable([System.Data.OleDb.OleDbSchemaGuid]::Tables,$null);
$cn.Close();
$cn.Dispose();
$worksheets | Format-List;

但是,这不会列出表(在 Excel 2003 中称为列表)或引用列表的命名范围。

如果我通过 OleDbSchemaGuid程序或 View 类型我得到 MethodInvocationException带有 Operation is not supported for this type of object. 的消息

这是否可以通过使用连接字符串或限制参数进行调整来列出表格?

最佳答案

试试这个简单的来源:

  using (var connection = (OleDbConnection)GetConnection())
        {
            connection.Open();

            var dt = connection.GetSchema("TABLES");
            var list=dt.Select().Where(w => w["TABLE_NAME"].ToString()).ToList();

            //TODO:
        }

关于excel - 使用 OleDbConnection::GetOleDbSchemaTable() 列出 excel 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10464386/

相关文章:

vba - 如何更改 ADO 记录集字段的数据类型?

c# - 适用于 Windows 和 MS-Access 数据库和 Jet.OLEDB.4.0 的 Docker

.net - 使用 VB.net 和 excel(组合框和搜索)

vba - 需要帮助使用 VBA 将 If 和 Vlookup 公式输入到单元格中

excel - 将多个项目存储在字典中以便稍后打印

excel - 使用VBA循环不同范围并比较部分字符串

c# - 如何将 DbContext.Database.SqlQuery<TElement>(sql, params) 与存储过程一起使用? EF 代码优先 CTP5

c# - 是什么让 ADO.NET Entity Framework 与市场上的其他 ORM 不同?

excel - 计数器帮助VBA

database - 作为服务运行时 OleDbConnection.Open 的问题