如何使用PowerShell打开带有’密码修改’的Excel文件

huangapple go评论75阅读模式
英文:

How to open Excel file with 'password to modify', using PowerShell

问题

我正在尝试使用PowerShell打开一个带有“修改密码”的Excel文件。

我已经尝试了标准代码来打开受密码保护的文件(如下所示),但对于“修改密码”却不起作用。

$wb = $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password)

我知道在使用Excel VBA时,可以使用以下代码打开这样的文件,但在PowerShell中是否有等效的代码?

Set openExcelFile = Workbooks.Open(filepath, WriteResPassword:=password)

非常感谢。

英文:

I am trying to open an Excel file with 'password to modify' using PowerShell.

I have tried the standard cord to open a password-protected file (see below), but it does not work for 'password to modify'.

$wb = $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password)

I know that I can open such a file using the code below when I use Excel VBA, but is there any equivalent code in PowerShell?

Set openExcelFile = Workbooks.Open(filepath, WriteResPassword:=password)

Many thanks.

答案1

得分: 1

我不是PowerShell专家,但是'打开密码'是第5个参数,'修改密码'(即WriteResPassword)是第6个参数,所以我会尝试再添加一个[Type]::Missing,如下所示:

$wb = $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password, [Type]::Missing)
英文:

I'm not a PowerShell expert, however, the 'password to open' is the 5th param and the 'password to modify' (ie WriteResPassword) is the 6th so I would try with one more [Type]::Missing ie

$wb = $excel.Workbooks.Open($filepath, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, $password)

答案2

得分: 1

有人已经为这个创建了一个模块:

https://github.com/dfinke/ImportExcel

你试过了吗?或者如果你的工作场所不允许使用其他模块,你可以从这里复制相关的脚本:

https://github.com/dfinke/ImportExcel/blob/master/Public/Import-Excel.ps1

英文:

Someone has already made a module for this:

https://github.com/dfinke/ImportExcel

Did you give that a try? or if your not allowed to use others modules in your workplace, you could always copy the relevant script from the function here:

https://github.com/dfinke/ImportExcel/blob/master/Public/Import-Excel.ps1

huangapple
  • 本文由 发表于 2023年7月31日 18:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定