我们有大量(1000个)旧的Excel 2003电子表格需要加密。Excel 2003中的加密现在已经过时,因此我们想将文件更新为2010格式,然后再对其进行加密。我正在尝试找到执行此操作的脚本或程序,但我一直无法做到。我发现的唯一东西是Microsoft迁移计划工具包,其中包括OFC工具。问题在于,此格式转换为2007格式,尽管兼容,但与2010格式不同。2007格式加密使用较弱的EBC块链接,而不是2010使用的CBC方法。同样,在2010年打开2007年的文件将要求保存文件,即使未进行任何更改(大概是将其更新为较新的格式)。
我最终使用Powershell来自动进行转换。这是我给有兴趣的人的脚本:
function ConvertTo-XLSX {
<#
.SYNOPSIS
XLS files within a provided path are recursively enumerated and convert to XLSX files.
.DESCRIPTION
XLS files within a provided path are recursively enumerated and convert to XLSX files.
The original XLS files remain intact, a new XLSX file will be created.
.PARAMETER Path
This parameter takes the input of the path where the XLS files are located.
.PARAMETER Visible
Using the parameter will show you how Excel does the work. Not using the parameter will enable Excel
to accomplish its tasks in the background.
Note: By not using this parameter you will be able to convert some XLS files which have corruptions
in them, when using the parameter and therefor the Excel GUI will give you an error.
.PARAMETER ToFolder
This parameter enables you to provide a location where the file is saved. When this parameter is
not used, the file will be saved as an XLS file in the same location as where the
original XLS file is located.
.PARAMETER Password
If specified, the password will be used to encrypt the converted Excel document. Passwords are case
sensitive and must be less than 15 characters long.
.PARAMETER Force
If specified, then files that have already been converted (there is already a file with the same name
but a .xlsx extension in the output directory) will be re-converted.
.EXAMPLE
ConvertTo-XLSX -Path 'D:\Data\2012'
.EXAMPLE
ConvertTo-XLSX -Path 'D:\Data\2012' -Visible
.EXAMPLE
ConvertTo-XLSX -Path 'D:\Data\2012' -ToFolder 'D:\Data\2012XLSX'
.EXAMPLE
ConvertTo-XLSX -Path 'D:\Data\2012' -Visible -ToFolder 'D:\Data\2012XLSX'
.EXAMPLE
ConvertTo-XLSX -Path 'D:\Data\2012' -Password 'SecureP@ssword'
.EXAMPLE
ConvertTo-XLSX -Path 'D:\Data\2012' -Force
#>
[cmdletbinding()]
param (
[parameter(mandatory=$true)][string]$Path,
[parameter(mandatory=$false)][switch]$Visible,
[parameter(mandatory=$false)][string]$ToFolder,
[parameter(mandatory=$false)][string]$Password,
[parameter(mandatory=$false)][switch]$Force
)
begin {
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
Write-Verbose 'Opening Excel COM object.'
$Excel = New-Object -ComObject excel.application
if ($Visible -eq $true) {
$Excel.visible = $true
} else {
$Excel.visible = $false
$Excel.DisplayAlerts = $false
$Excel.ScreenUpdating = $false
$Excel.UserControl = $false
$Excel.Interactive = $false
}
$filetype = "*xls"
} process {
if (Test-Path -Path $Path) {
Get-ChildItem -Path $Path -Include '*.xls' -recurse | ForEach-Object {
Write-Verbose "Processing $($_.Basename)"
if ($ToFolder -ne '') {
$FilePath = Join-Path $ToFolder $_.BaseName
$FilePath += ".xlsx"
} else {
$FilePath = ($_.fullname).substring(0, ($_.FullName).lastindexOf("."))
$FilePath += ".xlsx"
}
if (!(Test-Path $FilePath) -Or $Force) {
Write-Verbose "Opening $($_.Basename)"
$WorkBook = $Excel.workbooks.open($_.fullname)
Write-Verbose "Saving $($_.Basename) to $FilePath with password $Password"
$WorkBook.saveas($FilePath, $xlFixedFormat, $Password)
Write-Verbose "Closing $($_.Basename)"
$WorkBook.close()
} else {
Write-Verbose "$($_.Basename) already converted."
}
}
} else {
return 'No path provided or access has been denied.'
}
} end {
Write-Verbose 'Closing Excel'
$Excel.Quit()
$Excel = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
}
}
这是基于Jeff Wouters的脚本,该脚本位于:http : //jeffwouters.nl/index.php/2013/10/powershell-function-to-convert-xls-files-to-xlsx/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句