批量转换 XLS 到 XLSX

Ori*_*gin 15 batch conversion microsoft-excel

我必须使用只能导出 XLS 文件的旧应用程序,并且我使用只能读取 XLSX 文件的 EPPlus 库在 .NET 中编写程序。

将它们从 XLS 批量转换为 XLSX 的最简单方法是什么?

Chr*_*isN 6

查看 Office 迁移规划管理器。

该工具包还包含 Office 文件转换器 (OFC),它支持从二进制格式到 OpenXML 格式的批量文档转换。(的Technet

Technet 概述

下载链接

请注意,您还需要Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint File FormatsOFC 才能工作。
这两种工具似乎都不再受支持。


小智 4

我建议使用宏来处理文件夹中的文件,将它们从 xls 转换为 xlsx。此代码假定所有文件都位于一个文件夹内,并且所有 xls 文件都需要转换,但如果您想选择单个文件,则可以更新此代码。

此代码需要从 Excel 2007 或更高版本的工作簿运行。

Option Explicit

' Convert all xls files in selected folder to xlsx

Public Sub convertXLStoXLSX()

    Dim FSO As Scripting.FileSystemObject
    Dim strConversionPath As String
    Dim fFile As File
    Dim fFolder As Folder
    Dim wkbConvert As Workbook

    ' Open dialog and select folder
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Show
        On Error Resume Next ' Prevent debug mode if user cancels selection
        strConversionPath = .SelectedItems(1)
        On Error GoTo 0      ' Re-enable default error handling
    End With

    Set FSO = New Scripting.FileSystemObject

    ' Check if the folder exists
    If FSO.FolderExists(strConversionPath) Then
        Set fFolder = FSO.GetFolder(strConversionPath)

        ' Disable confirmation dialogs (to prevent "unsaved changes" dialog popping up)
        ' and screen updates (to speed up conversion)
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False

        ' Loop through files, find the .xls files
        For Each fFile In fFolder.Files
            If LCase$(Right(fFile.Name, 4)) = ".xls" Then
                ' Open temporary workbook
                Set wkbConvert = Workbooks.Open(fFile.Path)
                ' Save as OpenXML workbook - if your .xls files contain macros
                ' then change to FileFormat:=xlOpenXMLWorkbookMacroEnabled
                wkbConvert.SaveAs FSO.BuildPath(fFile.ParentFolder, _
                                    Left(fFile.Name, Len(fFile.Name) - 4)) & ".xlsx", _
                                  FileFormat:=xlOpenXMLWorkbook
                wkbConvert.Close SaveChanges:=False
                ' Delete original file
                fFile.Delete Force:=True
            End If
        Next fFile

        ' Re-enable confirmation dialogs and screen updates
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True

    End If

End Sub
Run Code Online (Sandbox Code Playgroud)

注意:如果您要转换的文件包含宏,那么您需要更新FileFormat:=xlOpenXMLWorkbook以读取FileFormat:=xlOpenXMLWorkbookMacroEnabled. 或者,如果您不需要转换后的文件中的宏代码,您可以不理会它,它将在将其转换为 xlsx 格式时删除宏。