Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .CSV to .XLSX using command line

I'm looking for a way to batch-convert a series of .csv files to .xlsx using the command line.

I have tried a bunch of different VBScripts that I found but they all seem to be converting .xlsx to .csv and not the other way around.

Here is the closest one I could find but again it's .xlsx to .csv:

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv 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"

Any ideas?

like image 516
crackruckles Avatar asked Jul 24 '15 09:07

crackruckles


People also ask

How do I automatically convert CSV file to Excel?

Power Automate allows you to open comma-delimited and tab-delimited CSV files directly through the Launch Excel action. To convert a CSV file of this type to XLSX, open the file and then save it as a new Excel workbook using the Save document as option in the Close Excel action.


3 Answers

Disclaimer: I have written CSV2XLSX available as open-source at https://gitlab.com/DerLinkshaender/csv2xlsx

You may want to try an external tool like the one above. Why?

Advantages:

  • may be used even where Windows Script Host is blocked.
  • independent of operating system, so you may use a familiar tool with then same set of options on every OS (save brain memory :-) ).
  • no installation of Excel or LibreOffice etc. needed, so you don't have to care about installing additional modules or languages.
  • specifying several CSV parameters as command-line parameters makes the tool "SysAdmin-friendly", as they do not have to dig thru source code.
  • you may even specify line or column ranges for the csv data.
  • the xlsx file format is very complex, writing it without resorting to an installation of an office package is not for the faint of heart.
  • better suited for batch processing as the tool can be integrated in the OS-specific loop logic.
  • I tried to provide a useful set of command line flags with DevOps/SysAdmin in mind, many existing scripts lack good control via command-line options.
like image 140
Arminius Avatar answered Oct 21 '22 04:10

Arminius


only pre-requisite is that the ".csv" must be lower case in the filename:

Dim file, WB

With CreateObject("Excel.Application")
    On Error Resume Next
    For Each file In WScript.Arguments
        Set WB = .Workbooks.Open(file)
        WB.SaveAs Replace(WB.FullName, ".csv", ".xlsx"), 51
        WB.Close False
    Next    
    .Quit
End With

WScript.Echo "Done!"
like image 23
SierraOscar Avatar answered Oct 21 '22 04:10

SierraOscar


For Windows, I recently responded to a similar question on SuperUser.com.

https://superuser.com/a/1011154/326177

I think Total CSV Converter is the least expensive option with the most features. It doesn't even require Excel to be installed and can can output CSV data to JSON, Access, DBF, XML or SQL.

http://www.coolutils.com/TotalCSVConverter

CSVConverter.exe <source> <destination> <options>
like image 1
James Moberg Avatar answered Oct 21 '22 03:10

James Moberg