gpt4 book ai didi

excel - 在powershell中打开受密码保护的Excel

转载 作者:行者123 更新时间:2023-12-03 02:31:08 25 4
gpt4 key购买 nike

我正在尝试在 powershell 中打开受密码保护的 Excel 工作表,并输出有关工作表中有多少行的报告。

如果工作表没有密码保护,脚本工作得绝对正常,但如果设置了密码,我似乎无法让 powershell 打开它。

我当前的脚本是

$Report = "S:\Business Support\excel tests\MI Tool - Live.csv"
$path = "S:\Business Support\excel tests"
[Array]$Results = $null
$excelSheets = Get-Childitem -Path $path -Include "MI Tool - Live.xlsm" -Recurse
$excel = New-Object -comobject Excel.Application
$excel.visible = $false
$password = "blablabla"
$updatelinks = 0


foreach($excelSheet in $excelSheets)
{
$workbook = $excel.Workbooks.Open($excelSheet,$updatelinks,$password)
$rowCount = $null

$worksheet = $workbook.sheets.item("Data")
$rowMax = ($worksheet.usedRange.rows).count
$rowCount += $rowMax

$Results += New-Object Psobject -Property @{
"File Name"=$excelSheet.Name
"Row Count"=$rowCount}
$excelSheet.Name
$workbook.Sheets.count
$rowCount
}
$excel.quit()
Stop-Process -Name EXCEL -Force
$Results | select "File Name","Row Count" | Export-Csv $Report -NoTypeInformation

这是我得到的错误:

Exception calling "Open" with "3" argument(s): "Open method of Workbooks class failed"
At line:3 char:35
+ $workbook = $excel.Workbooks.Open <<<< ($excelSheet,$updatelinks,$password)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

You cannot call a method on a null-valued expression.
At line:5 char:37
+ $worksheet = $workbook.sheets.item <<<< ("Data")
+ CategoryInfo : InvalidOperation: (item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

如果我取出 $password 变量,它可以工作,但我必须手动输入密码。

最佳答案

您的打开过载不正确。密码是第五个变量。 Have a look at MSDN to see

expression .Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad)

所以我认为您需要首先填充 ReadOnly 和 Format。您必须填充这些值。

$excel.Workbooks.open($path,0,0,5,$password)

查看 MSDN 以了解 2,3 和 4 位置上的值代表什么。

关于excel - 在powershell中打开受密码保护的Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42998625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com