Batch convert XLS to XLSX
Solution 1
Check out Office Migration Planning Manager.
The toolkit also contains the Office File Converter (OFC), which enables bulk document conversions from binary to OpenXML formats. (Technet)
Note that you'll also need the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint File Formats
for the OFC to work.
Both tools don't really seem to be supported anymore.
Solution 2
I'd recommend using a macro to process the files within a folder to convert them from xls to xlsx. This code assumes that the files are all located within one folder and that all xls files need to be converted, but if you wanted to select individual files this code could be updated.
This code would need to be run from an Excel 2007 or above workbook.
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
Note: If the files you are converting contain macros then you would need to update the
FileFormat:=xlOpenXMLWorkbook
to readFileFormat:=xlOpenXMLWorkbookMacroEnabled
. Or if you don't need the macro code in the converted files you could leave it alone and it will remove the macros when it converts it to the xlsx format.
Solution 3
So I wrote a simple VBScript to convert .xls files to .xlsx in a silent fashion.
./convert-xls-xlsx.vbs {path to folder containing .xls files}
convert-xls-xlsx.vbs:
Set args = WScript.Arguments
strPath = args(0)
strPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(strPath)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strPath)
For Each objFile In objFolder.Files
fileName = objFile.Path
If (objFso.GetExtensionName(objFile.Path) = "xls") Then
Set objWorkbook = objExcel.Workbooks.Open(fileName)
saveFileName = Replace(fileName,".xls",".xlsx")
objWorkbook.SaveAs saveFileName,51
objWorkbook.Close()
objExcel.Application.DisplayAlerts = True
End If
Next
MsgBox "Finished conversion"
NOTE: Look out for spaces in the folder path, if your path has a space in between, put the path in quotes.
Solution 4
If you have LibreOffice or OpenOffice then you can use their CLI convert feature
for %f in (*.xls) do "path\to\libreoffice\soffice.com" --convert-to xlsx "%f"
%f
needs to be changed to %%f
in a batch file
You can also do that in PowerShell
ls -R *.xls |% { & "path\to\libreoffice\soffice.com" --convert-to xlsx $_.FullName }
# Or the full unaliased version:
Get-ChildItem -Recurse *.xls | ForEach-Object { `
& "path\to\libreoffice\soffice.com" --convert-to xlsx $_.FullName }
In some versions you need to use libreoffice
instead of soffice
. And sometimes you need to add --headless
before or after --convert-to
to make it work
You can also use unoconv
since it can convert "between any document format supported by LibreOffice/OpenOffice"
for %f in (*.xls) do unoconv -f xlsx "%f"
Related videos on Youtube
Origin
Updated on September 18, 2022Comments
-
Origin almost 2 years
I have to work with an old application that can only export XLS files and I write programs in .NET using the EPPlus library that is only capable of reading XLSX files.
What is the easiest way to batch convert them from XLS to XLSX?
-
slhck over 10 yearsInstead of just pasting a bunch of code, could you maybe also explain a little what it does, how to use it, and what the limitations are (if any)? That'd be more helpful for future visitors.
-
moritzebeling almost 10 yearsWhat if I want to convert .CSV to .XLSX?
-
John about 8 yearsIt appears quite a few people, including me, have issue with this tool failing to convert their xls files for no discernible reason.
-
BeowulfNode42 about 4 yearsI changed the DisplayAlerts to = False as I don't want to know about any macros, just delete them, that's why I'm converting these things in the first place. Then removed the final MsgBox, as I didn't want it bugging me.
-
brosahay about 4 years@BeowulfNode42 cool, but I think I kept the DisplayAlerts just cause my workbooks were corrupt. But then again I don't remember exactly now.