尝试让我的脚本工作并需要一些帮助,这是我的代码。
#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
答案 0 :(得分: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对象。
答案 1 :(得分: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而产生的级联效应,所以我怀疑如果你解决这个问题,其他错误就会消失。