Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何删除Powershell上的Excel文件列?_Excel_Powershell - Fatal编程技术网

如何删除Powershell上的Excel文件列?

如何删除Powershell上的Excel文件列?,excel,powershell,Excel,Powershell,我想一次删除文件夹中的许多Excel文件。 所以我写了下面的代码,但当它运行时,终端注销Range类的Delete方法失败错误弹出。 更令人困惑的是,在某些工作表中,删除过程成功运行。 我认为,这是因为无法很好地完成powershell的发布表对象 有人能帮我吗?问候 # Launch Excel $excel = New-Object -ComObject Excel.Application -Property @{Visible = $false} $baseDir = Convert-

我想一次删除文件夹中的许多Excel文件。 所以我写了下面的代码,但当它运行时,终端注销
Range类的Delete方法失败
错误弹出。
更令人困惑的是,在某些工作表中,删除过程成功运行。
我认为,这是因为无法很好地完成powershell的发布表对象

有人能帮我吗?问候


# Launch Excel
$excel = New-Object -ComObject Excel.Application -Property @{Visible = $false} 
$baseDir = Convert-Path $(Split-Path $MyInvocation.InvocationName -Parent)
$files = Get-ChildItem -Recurse | ? { $_.Extension -eq ".xlsx" }  

# "${baseDir}\{$_.name}"
# Open Book
$files| 
%{
    Write-Host $_.Name
    $excel.Workbooks.Open("${baseDir}\" + $_.name) | %{
        $_.Worksheets | %{
            # Delete Column
            # $_.Activate
            Write-Host $_.Name

            #$_.Columns.Item("J").Delete()
            #$_.Columns("J:J").EntireColumn.Delete()
            #$_.Columns.item(3).Insert()
            #$_.Range("J:J").Delete()
            $_.Columns("J").Delete()
        }
        $_.Save()
    }
}

# Excel 
$excel.Quit()
\[System.Runtime.InteropServices.Marshal]::FinalReleaseComObject($excel) | Out-Null



如果您希望删除每个工作簿的每个工作表中的J列,这可能会有所帮助

$files| % {
    # Prints name of File
    Write-Host $_.Name

    # There is always one workbook. 
    $workbook = $excel.Workbooks.Open($_.FullName) # FullName has the complete path.
    $workbook.Worksheets | % {
        # prints name of each worksheet
        Write-Host $_.Name

        # Deletes the column
        $_.Range("J:J").EntireColumn.Delete() # prints True if successful.

        # Or you can use the above statement in an IF statement.
        if ($_.Range("J:J").EntireColumn.Delete()) {
           Write-Host "Column J Deleted successfully"
        } 
        # else print it didnt for $_.Name worksheet.
    }
    $workbook.Save()
}

如果您希望删除每个工作簿的每个工作表中的J列,这可能会有所帮助

$files| % {
    # Prints name of File
    Write-Host $_.Name

    # There is always one workbook. 
    $workbook = $excel.Workbooks.Open($_.FullName) # FullName has the complete path.
    $workbook.Worksheets | % {
        # prints name of each worksheet
        Write-Host $_.Name

        # Deletes the column
        $_.Range("J:J").EntireColumn.Delete() # prints True if successful.

        # Or you can use the above statement in an IF statement.
        if ($_.Range("J:J").EntireColumn.Delete()) {
           Write-Host "Column J Deleted successfully"
        } 
        # else print it didnt for $_.Name worksheet.
    }
    $workbook.Save()
}
也许可以看看这个模块。它被认为比使用com对象更方便用户。也许可以看看这个模块。它被认为比使用com对象更方便用户。