powershell XLS到CSV转换问题

时间:2015-11-19 15:22:16

标签: powershell-v3.0

我在代码下运行,将XLS转换为CSV但却出现了奇怪的错误。有人能指出我可以解决这个问题的正确方向吗? 我使用的是PowerShell v3.0。不知道我在这里缺少什么。

PARAM
    (
        [parameter(Mandatory = $true)]
        [string]
        $excelFilePath = $(throw "Must give a valid Excel (*.xls) file path.")
        ,

        [parameter(Mandatory = $true)]
        [int]
        $leadingRowsToDelete = $(throw "Must specify how many leading rows to delete.")
        ,

        [parameter(Mandatory = $false)]
        [string]
        $csvFilePath = $null
        ,

        [parameter(Mandatory = $false)]
        [int]
        $xlCSV = 6
    )


    if(! $csvFilePath)
    {
        $csvFilePath = $excelFilePath -replace ".xls$", ".csv"
    }

    $Excel = New-Object -Com Excel.Application -Property @{Visible = $false} 
    $Excel.displayalerts=$False

    $WorkBook = $Excel.Workbooks.Open($excelFilePath) # Open the file
    $Sheet = $WorkBook.Sheets.Item(1) # Activate the first worksheet

    # Delete the first $leadingRowsToDelete rows
    for($i = 1; $i -le $leadingRowsToDelete; $i ++)
    {
        [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
    }

    $WorkBook.SaveAs($csvFilePath, $xlCSV)

    $Excel.quit()

错误消息我得到的是:

Exception calling "Open" with "1" argument(s): "The server threw an exception. (Exception from HRESULT: 0x80010105
(RPC_E_SERVERFAULT))"
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:74 char:1
+ $WorkBook = $Excel.Workbooks.Open($excelFilePath) # Open the file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:75 char:1
+ $Sheet = $WorkBook.Sheets.Item(1) # Activate the first worksheet
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:80 char:2
+     [void]$Sheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\multinasdub301\Software\SysAdmin\WebDownloads\XLS-To-CSV.ps1:83 char:1
+ $WorkBook.SaveAs($csvFilePath, $xlCSV)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

0 个答案:

没有答案