Convert .CSV to .XLSX using the command line

I am looking for a way to batch convert a sequence of .csv files to .xlsx using the command line.

I tried a bunch of different VBScripts that I found, but they all seem to convert .xlsx to .csv , and not vice versa.

Here is the closest I could find, but again its .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?

+5
source share
5 answers

the only premise is that ".csv" must be lowercase in the file name:

 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!" 
+6
source

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

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

Benefits:

  • can be used even where Windows Script Host is locked.
  • regardless of the operating system, so you can use a familiar tool with the same set of options on each OS (save brain memory :-)).
  • No installation of Excel or LibreOffice, etc. is required, so you do not need to worry about installing additional modules or languages.
  • specifying several CSV parameters as command line parameters makes the SysAdmin-friendly tool, as they do not need to dig through the source code.
  • you can even specify ranges of rows or columns for csv data.
  • The xlsx file format is very complex; writing it without resorting to installing an office suite is not for the faint of heart.
  • better suited for batch processing because the tool can be integrated into OS-specific loop logic.
  • I tried to provide a useful set of command line flags with DevOps / SysAdmin, many existing scripts do not have good control using command line parameters.
+4
source

On Windows, I recently answered a similar question on SuperUser.com.

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

I think Total CSV Converter is the cheapest option with most features. It does not even require Excel installation and can output CSV data in JSON, Access, DBF, XML or SQL.

http://www.coolutils.com/TotalCSVConverter

 CSVConverter.exe <source> <destination> <options> 
+1
source

Are you on Windows or Linux / Mac?

I may have a solution for you anyway.

Here is a solution to your problems without the programs commented below:

https://social.msdn.microsoft.com/Forums/en-US/74df1378-7c0c-4c0f-b174-fa97a5c2969b/convert-csv-to-xlsx?forum=Vsexpressvb

EDIT So, here is the basic solution:

Basically you apply a filter (which in this case will be an xlsx filter) from the directory where your file is located.

Nvm is what I just saw that you are on Windows . / directory --headless --convert-to xlsx: "Calc MS Excel 2007 XML" file.csv

in this case, "Calc MS Excel 2007 XML" is a filter.

This works for single files, allows me to add a package per second.

0
source

Here's an open source tool for Windows machines created using the NPOI libraries that makes a simple delimited file for XLS / XLSX conversions without having to install Excel on your computer. The binary file is in Bin / Debug, if you do not want to create it yourself. All necessary libraries are included in the executable file, so it can work autonomously.

https://github.com/nmolinos/csv2excel

0
source

All Articles