12

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?

Bond
  • 16,071
  • 6
  • 30
  • 53
crackruckles
  • 336
  • 2
  • 5
  • 21

5 Answers5

19

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.
Arminius
  • 1,029
  • 7
  • 11
  • Providing link is ok, try to explain how this will solve the problem in question. – Arun Vinoth-Precog Tech - MVP Aug 04 '17 at 16:46
  • @ArunVinoth - thanks, better now? I'm a newcomer to SO. – Arminius Aug 09 '17 at 20:53
  • +1. Seems to work fine after a few tests. Here is the 2 lines batch file I made to convert files with a simple drag'n drop : `csv2xlsx_amd64.exe -colsep ";" -sheet "data" -outfile "%1.xlsx" -infile "%1" ` `pause`. The result may look a bit ugly (I'd love to have a font parameter to set Tahoma instead of Verdana), but it seems to do a nice job. The performance is decent too. Not extremely fast, but I wouldn't call it slow. I just had one a (very stupid) issue : tried to use " -noheader yes" while noheader uses no parameter, which resulted in a wrong error (*input file doesn't exist.*) – Balmipour Jan 04 '18 at 15:32
  • 2
    @Balmipour I enhanced the tool. There are now options for FontName, FontSize as well as a filemask for wildcard/bulk processing and many more, including support for other encodings than UTF-8 – Arminius Jun 25 '18 at 16:15
  • 2
    @user121391 other encodings are now possible, have a look at the new release – Arminius Jun 25 '18 at 16:17
9

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!"
SierraOscar
  • 17,507
  • 6
  • 40
  • 68
  • I launched it from the command line with a single .csv file as the argument but it just says done and nothing happens. – crackruckles Jul 24 '15 at 09:36
  • have you passed arguments to it? and are you calling the script from CMD, or using it directly through drag-n-drop? – SierraOscar Jul 24 '15 at 09:39
  • So i got your method to work i just needed to pass the absolute path not just the file name. Thanks for the help. – crackruckles Jul 24 '15 at 09:46
  • 1
    Use `Replace(WB.FullName, ".csv", ".xlsx", 1, -1, vbTextCompare)` and you don't need to worry about case. Or `Replace(LCase(WB.FullName), ".csv", ".xlsx")`. – Bond Jul 24 '15 at 12:35
  • @Bond good shout, although I avoided `LCase()` because the output filename is then all lower case. Pedantic I know but hey ho... – SierraOscar Jul 24 '15 at 13:58
  • 1
    @Andrew not sure what you're referring to? This uses the `.SaveAs` method in Excel to save a copy of the file as a CSV - it's not just renaming the file, which obviously wouldn't work. The example you pointed to also isn't a script that would run in the command line – SierraOscar Sep 11 '17 at 15:59
1

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>
Community
  • 1
  • 1
James Moberg
  • 4,360
  • 1
  • 22
  • 21
1

Here's a open-source tool for Windows machines I created using the NPOI libraries that does simple delimited file to XLS/XLSX conversions without Excel needing to be installed on the machine. The binary is in Bin/Debug if you don't want to build it yourself. All the necessary libraries are included in the executable so it can operate standalone.

https://github.com/nmolinos/csv2excel

Nico M
  • 173
  • 1
  • 8
0

Are you on Windows or Linux/Mac?

I might have a solution for you either way.

Here is the 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 the case would be the xlsx filter) from the directory where your file is located.

Nvm this I just saw 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 the filter.

That works for single files, let me add batch in a sec.

Robert Melikyan
  • 52
  • 2
  • 14
  • Sorry i should have mentioned windows – crackruckles Jul 24 '15 at 09:18
  • Try these out, they are useful converters for Windows http://www.dbf2002.com/csv-converter/commandline.html http://www.softinterface.com/Convert-XLS/Features/Convert-CSV-To-XLSX.htm – Robert Melikyan Jul 24 '15 at 09:24
  • Ive tried both of those and they both convert them incorrectly and results in an error when i try to import them using SSIS to my SQL database, however if i do it manually through excel it works perfectly – crackruckles Jul 24 '15 at 09:27