在没有 Office 的服务器上对 Excel 文件执行 PowerShell 操作(打开和使用密码另存为)

秃头47

我正在尝试执行一个使用 Excel 文件运行的 PowerShell 脚本,该脚本打开 excel 并使用密码保护对文件进行 SaveAs。这在装有 Office 的 PC 上效果很好,但在没有安装 Office 的服务器上就不行。

我知道有一些库可以使用,但我不知道如何使用它们,这里是 PowerShell 脚本的代码:

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$LoadPath = Join-Path -Path $pwd -ChildPath "TEMPLATE4WEEKS.xlsx"    #$pwd is your current working dir
$SavePath = Join-Path -Path $pwd -ChildPath "TEMPLATE4WEEKSprotected.xlsx"
$wb = $excel.Workbooks.Open($LoadPath)


$wb.SaveAs($SavePath,[Type]::Missing,"password")
$excel.Quit()

我已经阅读了一些关于诸如 EPPlus ( https://archive.codeplex.com/?p=epplus ) 之类的库的内容,但我不知道如何使用它。

如果值得一提,我在尝试在服务器上执行 PowerShell 脚本时遇到的错误如下:

New-Object : Retrieving the COM class factory for component with CLSID {00000000-0000-0000-0000-000000000000} failed
due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
At E:\TEMP\PasswordProtectLAST4WEEKS.ps1:1 char:10
+ $excel = New-Object -ComObject Excel.Application
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

The property 'Visible' cannot be found on this object. Verify that the property exists and can be set.
At E:\TEMP\PasswordProtectLAST4WEEKS.ps1:2 char:1
+ $excel.Visible = $true
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

The property 'DisplayAlerts' cannot be found on this object. Verify that         the property exists and can be set.
At E:\TEMP\PasswordProtectLAST4WEEKS.ps1:3 char:1
+ $excel.DisplayAlerts = $false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At E:\TEMP\PasswordProtectLAST4WEEKS.ps1:6 char:1
+ $wb = $excel.Workbooks.Open($LoadPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At E:\TEMP\PasswordProtectLAST4WEEKS.ps1:9 char:1
+ $wb.SaveAs($SavePath,[Type]::Missing,"jacobs4321")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At E:\TEMP\PasswordProtectLAST4WEEKS.ps1:10 char:1
+ $excel.Quit()
+ ~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PL

在这里综合我的多条评论作为答案:

  1. 下载 EPPlus:https ://www.nuget.org/packages/EPPlus/

  2. 阅读一些文档并检查他们的示例代码:https : //github.com/JanKallman/EPPlus/wiki/Getting-Started

  3. 然后像这样打开并保存您的文件:

.

Add-Type -Path D:\PathOfExile\epplus.4.5.3.2\lib\net40\EPPlus.dll
$ExcelPackage = New-Object OfficeOpenXml.ExcelPackage("C:\Metallica\WhereverIMayRoam\hehe.xlsx")
$ExcelPackage.Workbook.Worksheets.Add("Test")
$ExcelPackage.SaveAs("C:\Metallica\WhereverYOUMayRoam\newfile.xlsx", "hohoho") # This sets the password of the xlsx to hohoho

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Office.Interop.Excel将文件另存为PDF / A

使用PowerShell更改文件服务器上的权限

服务器上是否需要 MS Office 才能使用 Microsoft.Excel.interop

Javascript使用SheetJS在服务器上读取Excel文件

没有安装Excel的情况下使用Microsoft.Office.Interop.Excel创建Excel文件

有什么方法可以使用“另存为”功能将文件直接保存到ftp服务器?

使用Access VBA的FTP在服务器上创建没有数据的文件

在服务器上读取Excel文件

Excel另存为PDF并覆盖现有文件

如何使用Access VBA获取在服务器上打开(Excel)文件的用户的名称?

将查询结果另存为csv文件在特定文件夹下的服务器上

列出ssh服务器上目录中的文件,并使用R SSH包另存为数据框

业力:文件服务器上没有缓存

Laravel没有在服务器上上传实际文件

定义打开 Excel 文件 (openpyxl) 并另存为 DataFrame 的函数

如何使用OpenOffice打开Excel文件并将其另存为CSV

使用PowerShell将一个FTP目录中的所有文件移动到同一服务器上的另一个文件

为服务器上生成的文件创建“另存为”按钮

将Blob对象另存为服务器上的文件

C#(Microsoft.Office.Interop.Excel),将数据另存为Excel 2003文件(xls)

在本地服务器上使用Powershell脚本应用Service Pack(.msu文件)更新

在PowerShell中使用WinSCP .NET程序集重命名SFTP服务器上的文件

使用 PowerShell 将数据附加到 FTP 服务器上的 CSV 文件

在PowerShell中使用WinSCP .NET程序集监视FTP服务器上的上传文件

在命令行上使用PowerShell启动具有管理员权限的服务器

在没有 laravel 和 composer 安装的情况下在 wamp 服务器上运行 laravel 项目文件

我想在 react js 中的另一个文件上使用类组件但没有服务器?

使用VBScript在远程服务器上运行BAT文件。没有psexec,并且作为其他用户

如何使用宏通过“文件>另存为”对话框保存带有密码的文档?