powershell XLS to CSV conversion issue

天大地大妈咪最大 提交于 2019-12-12 03:23:22

问题


I am running below code with to convert XLS to CSV but getting strange error. Can someone point me to right direction as to what can be causing this issue? I am using powershell v3.0. not sure what I am missing here.

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()

Error msg I get is:

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

回答1:


According to OP's comments, issue was caused by faulty permissions on the target Excel file.

Script is working fine on a local file with full permissions.



来源:https://stackoverflow.com/questions/33807857/powershell-xls-to-csv-conversion-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!