-1

试图让我的脚本工作并需要一些帮助是我的代码。

#excel
#open ap
$XL = new-object -com "Excel.Application"
$XLbooks = $XL.workbooks
$netci = [system.Globalization.CompareInfo]"en-us"
$wkbk = $XLbooks.PSBase.GetType().Invokemember("Add",[Reflection.BindingFlags]::InvokeMethod,$null,$XLbooks,$null,$newci)
$sheet = $XLbooks.worksheets.item(1)
$sheet.name = "name"
$sheet.cells.item($row,1).formulalocal = "Fred Nurk"
$file = "c\windows\scripts\test.xlsx"
[void]$wkbk.PSBase.GetType().InvokeMember("SaveAs",[Reflection.BindingFlags]::InvokeMethod,$null,$wkbk,$file,$newci)
("Close",[Reflection.BindingFlags]::Invokemedthod,$null,$wkbk,0,$newci)
$XL.quit()

错误

Cannot convert the "en-us" value of type "System.String" to type "System.Globalization.CompareInfo".
At C:\scripts\test.ps1:5 char:44
+ $netci = [system.Globalization.CompareInfo] <<<< "en-us"
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

You cannot call a method on a null-valued expression.
At C:\scripts\test.ps1:7 char:34
+ $sheet = $XLbooks.worksheets.item <<<< (1)
    + CategoryInfo          : InvalidOperation: (item:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Property 'name' cannot be found on this object; make sure it exists and is settable.
At C:\scripts\test.ps1:8 char:8
+ $sheet. <<<< name = "name"
    + CategoryInfo          : InvalidOperation: (name:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\scripts\test.ps1:9 char:18
+ $sheet.cells.item <<<< ($row,1).formulalocal = "Fred Nurk"
    + CategoryInfo          : InvalidOperation: (item:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "InvokeMember" with "6" argument(s): "Microsoft Excel cannot access the file 'C:\Users\Jared\Document
s\c\windows\scripts\5ADD7000'. There are several possible reasons:
 The file name or path does not exist.
 The file is being used by another program.
 The workbook you are trying to save has the same name as a currently open workbook."
At C:\scripts\test.ps1:11 char:42
+ [void]$wkbk.PSBase.GetType().InvokeMember <<<< ("SaveAs",[Reflection.BindingFlags]::InvokeMethod,$null,$wkbk,$file,$n
ewci)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation
4

2 回答 2

4

您需要解决的主要问题是创建 CompareInfo。第一个错误告诉你这条线不起作用:

$netci = [system.Globalization.CompareInfo]"en-us"

因此,您需要以这种方式创建 CompareInfo 对象:

$netci = ([system.Globalization.CultureInfo]"en-us").CompareInfo

虽然不是使用这种疯狂的方式来创建工作簿:

$wkbk = $XLbooks.PSBase.GetType().Invokemember("Add",[Reflection.BindingFlags]::InvokeMethod,$null,$XLbooks,$null,$newci)

...尝试这种更理智的方式:D

$wkbk = $XL.workbooks.Add()

如果您这样做,您将不必担心创建 CompareInfo 对象。

于 2013-04-30T05:53:37.163 回答
0

跳出来的问题$netci = [system.Globalization.CompareInfo]"en-us"是语法无效。您没有在 System.Globalization.CompareInfo 类上调用任何方法,只是在其后放置一个字符串。PowerShell 解释[System.Globalization.CompareInfo]为类型转换运算符,并抱怨字符串“en-us”无法转换为数据类型 System.Globalization.CompareInfo - 因为该数据类型不存在。

您需要调用在“en-us”上运行的方法。您可以从 MSDN 获取方法列表:

http://msdn.microsoft.com/en-us/library/system.globalization.compareinfo.aspx

假设您想要的方法是 GetCompareInfo (在我看来最有可能 - 它返回一个 CompareInfo 对象),您可以这样编写该行:

$netci = [System.Globalization.CompareInfo]::GetCompareInfo('en-us')

请注意,顺便说一句,您$netci在创建它时拥有此变量,但$newci与脚本的其余部分一样。

我没有对其他错误进行过深入研究,但它们可能是未能正确创建 $newci 的级联效应,所以我怀疑如果你解决了这个问题,其他错误就会消失。

于 2013-04-30T16:38:17.927 回答