以下 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/