【问题标题】:How to convert Excel xlsx file to xls file in batch by PowerShell如何通过PowerShell将Excel xlsx文件批量转换为xls文件
【发布时间】:2019-08-09 11:34:47
【问题描述】:

我有多个从 SAP BO4.2 生成的 .xlsx 文件 但是用户只阅读 .xls 就想编写一些将 .xlsx 转换为 .xls 的脚本

推荐- https://gallery.technet.microsoft.com/How-to-convert-Excel-xlsx-d9521619 并尝试将其用于 .xls

$ErrorActionPreference = 'Stop'

Function Convert-xlsInBatch
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][String]$Folder
    )
    $ExcelFiles = Get-ChildItem -Path $Folder -Filter *.xlsx -Recurse

    $excelApp = New-Object -ComObject Excel.Application
    $excelApp.DisplayAlerts = $false

    $ExcelFiles | ForEach-Object {
        $workbook = $excelApp.Workbooks.Open($_.FullName)
        $xlsFilePath = $_.FullName -replace "\.xlsx$", ".xls"
        $workbook.SaveAs($xlsFilePath, [Microsoft.Office.Interop.Excel.XlFileFormat]::xlExcel7)
        $workbook.Close()
    }

    # Release Excel Com Object resource
    $excelApp.Workbooks.Close()
    $excelApp.Visible = $true
    Start-Sleep 5
    $excelApp.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp)     | Out-Null
}

#
# 0. Prepare the folder path which contains all excel files
$FolderPath = "D:\XXX\AA\BB\Apr-2018"

Convert-XlsInBatch -Folder $FolderPath

我得到的错误-

PS D:\Batch Script> D:\Batch Script\ConvertExcelToXlsInBatch.ps1

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 D:\Batch Script\ConvertExcelToXlsInBatch.ps1:27 char:14
+     $excelApp = New-Object -ComObject Excel.Application
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

【问题讨论】:

  • 您遇到的问题与stackoverflow.com/questions/29772500/… 类似
  • @BenoîtMayer- 在我的系统中,Outlook 和 MS Office 都没有安装。它只是一个服务器,文件操作来自应用程序并通过 Perl 脚本转到其他位置。
  • 这是问题所在:如果没有安装 MS Office,您将无法使用 ComObjects...
  • 有什么方法可以在没有 MS Office 的情况下转换文件。 BatchScript、ShellScript、perlscript 什么都行。要求是它应该是正确的转换,而不仅仅是扩展名重命名/更改。
  • 据我所知,没有安装 Excel 是不可能将 xlsx 文件转换为 xls 的,至少使用 Powershell 是不可能的,而且我已经找了很长时间了。

标签: excel powershell


【解决方案1】:

不确定您是否看过此解决方案。它已经 4 岁了,但它似乎可以工作。

https://superuser.com/questions/875831/using-powershell-is-it-possible-to-convert-an-xlsx-file-to-xls

【讨论】:

  • [Type]::Missing,$true) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ( :) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull 您不能在空值表达式上调用方法。在 D:\Batch Script\Convert.ps1:10 char:1 + $Workbook.SaveAs($NewFilepath,56) + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 2020-03-29
  • 2011-10-05
  • 1970-01-01
  • 2021-01-27
相关资源
最近更新 更多