如何以编程方式从excel工作簿中删除密码?

如何以编程方式从excel工作簿中删除密码?,excel,powershell,Excel,Powershell,几周前,我不得不从应用程序创建的excel文件中删除密码保护。我没有密码。这个任务可以通过powershell使用xml转换来完成吗?这是我想与您分享的解决方案。powershell脚本使用powershell从Excel文件中删除密码和工作表保护。不需要Excel应用程序和密码。该脚本仅适用于.xlsx文件类型,不适用于.xls。如果你有改进的想法,请告诉我。多谢各位 Add-Type -AssemblyName System.IO.Compression Add-Type -

几周前,我不得不从应用程序创建的excel文件中删除密码保护。我没有密码。这个任务可以通过powershell使用xml转换来完成吗?

这是我想与您分享的解决方案。powershell脚本使用powershell从Excel文件中删除密码和工作表保护。不需要Excel应用程序和密码。该脚本仅适用于.xlsx文件类型,不适用于.xls。如果你有改进的想法,请告诉我。多谢各位

    Add-Type -AssemblyName System.IO.Compression
    Add-Type -AssemblyName System.IO.Compression.FileSystem

    #-----------------------------------------------------------------------
    function Remove-Excel-WriteProtection {
    <#
    // Removes all password and write protection from existing excel file
    // (workbook and worksheets). 
    // No password needed.
    // 
    // Input:   Path to Excel file (must newer xlsx format)
    // Output:  true if successfull
    #>
    #-----------------------------------------------------------------------
        param(
            [Parameter(Mandatory=$true)]
            [string]$filePathExcel     
        )

        if( !(Test-Path -Path $filePathExcel) -or 
            !(Split-Path -Path $filePathExcel -Leaf).EndsWith('xlsx') ) {
            return $false
        }

        $fileItem      = Get-Item $filePathExcel
        $filePathZip   = $fileItem.DirectoryName + '\' + $fileItem.BaseName + '.zip'
        $filePathTemp  = $fileItem.DirectoryName + '\' + ([System.Guid]::NewGuid()).Guid 

        Rename-Item -Path $filePathExcel -NewName $filePathZip -Force
        Expand-Archive -Path $filePathZip -DestinationPath $filePathTemp -Force

        $xml = New-Object System.Xml.XmlDocument
        $xml.PreserveWhitespace = $true

        $workbookCollection = (Get-ChildItem -Path $filePathTemp -Filter 'workbook.xml' -Recurse -Force)

        foreach( $workbook in $workbookCollection ) {

            [void]$xml.RemoveAll()
            [void]$xml.Load($workbook.FullName)

            if( $xml.workbook.fileSharing.readOnlyRecommended -or $xml.workbook.fileSharing.reservationPassword ) {

                if( $xml.workbook.fileSharing.readOnlyRecommended ) {
                    $xml.workbook.fileSharing.readOnlyRecommended = '0'
                }
                if( $xml.workbook.fileSharing.reservationPassword ) {
                    $xml.workbook.fileSharing.reservationPassword = ''
                }

                [void]$xml.Save($workbook.FullName)
            }
        }

        $worksheetCollection = (Get-ChildItem -Path $filePathTemp -Filter 'sheet*.xml' -Recurse -Force)

        foreach( $worksheet in $worksheetCollection ) {

            [void]$xml.RemoveAll()
            [void]$xml.Load($worksheet.FullName)

            if( $xml.worksheet.sheetProtection ) {
                [void]$xml.worksheet.RemoveChild($xml.worksheet.sheetProtection)
                [void]$xml.Save($worksheet.FullName)
            }
        }

        Remove-Item -Path $filePathZip -Force
        Compress-Archive -Path ($filePathTemp + '\*') -DestinationPath $filePathZip -Force -CompressionLevel Optimal
        Remove-Item -Path $filePathTemp -Recurse -Force
        Rename-Item -Path $filePathZip -NewName $filePathExcel -Force

        return $true
    }

    # Remove all passwords for test.xlsx

    $result = Remove-Excel-WriteProtection -filePathExcel 'C:\Users\YourName\Desktop\test.xlsx'
添加类型-AssemblyName System.IO.Compression
添加类型-AssemblyName System.IO.Compression.FileSystem
#-----------------------------------------------------------------------
函数删除Excel写保护{
#-----------------------------------------------------------------------
param(
[参数(必需=$true)]
[字符串]$filePathExcel
)
如果(!(测试路径-路径$filePathExcel)-或
!(拆分路径-Path$filePathExcel-Leaf).EndsWith('xlsx')){
返回$false
}
$fileItem=获取项目$filePathExcel
$filePathZip=$fileItem.DirectoryName+'\'+$fileItem.BaseName+'.zip'
$filePathTemp=$fileItem.DirectoryName+'\'+([System.Guid]::NewGuid()).Guid
重命名项-Path$filePathExcel-NewName$filePathZip-Force
展开归档-Path$filePathZip-DestinationPath$filePathTemp-Force
$xml=New Object System.xml.XmlDocument
$xml.PreserveWhitespace=$true
$workbookCollection=(Get ChildItem-Path$filePathTemp-Filter'workbook.xml'-Recurse-Force)
foreach($workbookCollection中的工作簿){
[void]$xml.RemoveAll()
[void]$xml.Load($workbook.FullName)
如果($xml.workbook.fileSharing.readOnlyRecommended-或$xml.workbook.fileSharing.reservationPassword){
if($xml.workbook.fileSharing.readOnlyRecommended){
$xml.workbook.fileSharing.readOnlyRecommended='0'
}
if($xml.工作簿.fileSharing.reservationPassword){
$xml.workbook.fileSharing.reservationPassword=''
}
[void]$xml.Save($workbook.FullName)
}
}
$worksheetCollection=(Get ChildItem-Path$filePathTemp-Filter'sheet*.xml'-Recurse-Force)
foreach($worksheetCollection中的工作表){
[void]$xml.RemoveAll()
[void]$xml.Load($worksheet.FullName)
如果($xml.worksheet.sheetProtection){
[void]$xml.worksheet.RemoveChild($xml.worksheet.sheetProtection)
[void]$xml.Save($worksheet.FullName)
}
}
删除项目-Path$filePathZip-Force
压缩归档文件-Path($filePathTemp+'\*')-DestinationPath$filePathZip-Force-CompressionLevel最佳
删除项-路径$filePathTemp-Recurse-Force
重命名项-Path$filePathZip-NewName$filePathExcel-Force
返回$true
}
#删除test.xlsx的所有密码
$result=删除Excel写保护-filePathExcel'C:\Users\YourName\Desktop\test.xlsx'

可能重复“否”,此解决方案根本不需要密码!Duplucate用于结果而不是方法…这在现代Excel版本中实际上不起作用。你错了。此脚本尤其适用于新的Excel文件格式。你在投票前测试过吗???如果是,请将各自的Excel文件发送给我,我会检查。