使用PowerShell操作Excel

时间:2017-05-24 11:48:22

标签: excel powershell cmd automation

每个人都很好,所以我们尝试自动化一些Excel并遇到一些错误,所以我们有下面列出的脚本,但我们遇到了3个不同的错误。

有人可以帮我们确定脚本出错的地方吗?

我们得到的错误是:

Exception calling  "Open" with  "1" arguments : "Server error. (excpetion  HRESULT: 0x80010105 (RPC_E_SERVERFAU
LT))"
C:\Users\Montage\Desktop\blockadp.ps1:9 char:34
+ $workbook = $excel.workbooks.open <<<< ($excelMacrosFile);
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

Cant call a method with  NULL.
C:\Users\Montage\Desktop\blockadp.ps1:10 char:39
+ $worksheet = $workbook.worksheets.item <<<< (1);
    + CategoryInfo          : InvalidOperation: (item:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "Run" with "31" arguments: "server error. (excpetion from HRESULT: 0x80010105 (RPC_E_SERVERFAU
LT))"
C:\Users\Montage\Desktop\blockadp.ps1:11 char:11
+ $excel.Run <<<< ("Convert", $inputExcelFile, $outputADSFile);
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

cannot call a method with NULL.
C:\Users\Montage\Desktop\blockadp.ps1:13 char:16
+ $workbook.close <<<< ();
    + CategoryInfo          : InvalidOperation: (close:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

脚本本身:

$excelMacrosFile = "C:\Users\Montage\Desktop\friday.xls";
$inputExcelFile = "C:\Users\Montage\Desktop\25.05.17.xls";
$outputADSFile = "C:\Users\Montage\Desktop\25.05.17.adp";

$excel = new-object -comobject excel.application;

$workbook = $excel.workbooks.open($excelMacrosFile);
$worksheet = $workbook.worksheets.item(1);
$excel.Run("Convert", $inputExcelFile, $outputADSFile);
#$workbook.save();
$workbook.close();
$excel.quit();

即使我只尝试“https://support.jamsscheduler.com/hc/en-us/articles/206191918-How-to-Automate-the-opening-of-an-Excel-Spreadsheet-in-Powershell

$excelMacrosFile = "C:\Users\Montage\Desktop\friday.xls";
$excel = new-object -comobject excel.application;
$workbook = $excel.workbooks.open($excelMacrosFile);

然后我仍然得到同样的错误

Exception calling "Open" with "1" argument(s): "The server threw an exception. (Exception from HRESULT: 0x80010105
(RPC_E_SERVERFAULT))"

这也不是如下所述的权限,因为这有效:

$test = Get-Content 'C:\Users\Montage\Desktop\25.05.17.xls

1 个答案:

答案 0 :(得分:0)

问题是安装的Microsoft Office版本是x86。 当系统本身是x64时。

在x64办公室进行测试显示没有错误,并且脚本按预期工作。