Monday, August 18, 2014

Converting xls, xlsx files into csv file thru command line

Thanks to "ScottF", "plang" and "Jon of All Trades" from "StackOverflow" for this solution.

I had a situation where there are hundreds of xls files that needs to be loaded into Oracle and feeling lazy (as usual) to convert all of them into csv file to use in sqlldr.
So, doing little google found this solution:

Copy below code and write into a file Convert_xls2csv.vbs file:
-------------------------------------------------------------------------------------------------------
if WScript.Arguments.Count < 2 Then
    WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv "
    Wscript.Quit
End If

csv_format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)

oBook.SaveAs dest_file, csv_format

oBook.Close False
oExcel.Quit
-------------------------------------------------------------------------------------------------------

Now, create another Convert2csv.bat batch file and copy the below to run thru the loop:

-------------------------------------------------------------------------------------------------------
FOR /f "delims=" %%i IN ('DIR "*.xls" /b') DO Convert_xls2csv.vbs "%%i" "%%i.csv"
-------------------------------------------------------------------------------------------------------

Please these two files in the directory where you have all your xls(x) files and run the batch file and relax :)

Original Source:

No comments:

Post a Comment