问题描述
使用PowerShell,创建一个简单的Excel Application类实例并开始对其进行操作就很容易了:
Using PowerShell, it is easy enough to create, say, an instance of the Excel Application class and start manipulating it:
$app = New-Object -ComObject "Excel.Application"
但是,如果我需要使用xlDoubleQuote或xlDelimited之类的常量-似乎我被迫对它们进行硬编码.我真的很想能够做类似的事情:
However, if I need to use the constants like xlDoubleQuote or xlDelimited - it seems like I am forced to hard code them. I would really like to be able to do something like:
$constants = New-Object -ComObject "Excel.Constants"
$constants.xlDoubleQuote
并看到它将返回1的值.不幸的是,我无法创建枚举的实例,并且似乎没有像普通的.NET类库一样引用它的方法:
And see that it would return the value of 1. Unfortunately I can't create an instance of an enumeration, and there doesn't seem to be a way to reference it like you would a normal .NET class library:
[Excel.Constants]::xlDoubleQuote
是否存在将枚举动态导入PowerShell的某种方法?也许通过托管库而不是通过COM?
Is there some way to dynamically import that enumeration into PowerShell? Maybe through the managed libraries rather than COM?
推荐答案
Keith已经给您答案了,这是另一个选择.您可以在$ xlConstants对象上使用制表符补全来获取常量:
Keith already gave you the answer, here's another option. You can use tab completion on the $xlConstants object to get the constants:
$xl = New-Object -ComObject Excel.Application
$constants = $xl.gettype().assembly.getexportedtypes() | where-object {$_.IsEnum -and $_.name -eq 'constants'}
$pso = new-object psobject
[enum]::getNames($constants) | foreach { $pso | Add-Member -MemberType NoteProperty $_ ($constants::$_) }
$xlConstants = $pso
这篇关于查找MS Office Interop常数的值,而不是对其进行硬编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!