24 April 2018

Convert XLS Files to CSV From Command Line

This is how you can convert an XLS file to a CSV file using the Windows command line. You'll need to create a VBScript to do this.

Create a file called ConvertXLS_To_CSV.vbs (or whatever you like) and paste the following code  (*) (this requires Excel to be installed on the machine you are on though) :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if WScript.Arguments.Count < 2 Then
    WScript.Echo "Error! Please specify the source path and the destination. Usage: ConvertXLS_To_CSV SourcePath.xls Destination.csv"
    Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"

Your VBScript is ready. Now, all you have to do is to execute from the command line the script, setting the source file (XLS) and the desired .csv file:

C:\> ConvertXLS_To_CSV.vbs [sourcexlsFile].xls [destinationcsvfile].csv

(*) Found on Stack Overflow

No comments:

Post a Comment