15

Is there a way, ideally using the command line, to convert multiple .csv files to one multi-sheet .xls spreadsheet?

If there isn't a cli solution, it'd be good to know if there's an API that works in, ideally, awk or pascal, but, failing that, in pretty-well anything.

kos
  • 35,535
  • 13
  • 101
  • 151
Peter Brooks
  • 313
  • 2
  • 6
  • ssconvert, mentioned by Maythux, is a companion command to Gnumeric, which will be installed by default on an Ubuntu Desktop system, but not on a server version. – Arronical Feb 17 '16 at 10:09
  • Given the fact Excel will load CSV in an `.xls` file you could simply do: `cat *.csv > file.xls` – Ash Feb 17 '16 at 12:05
  • try [this](http://stackoverflow.com/questions/31150342/csvs-to-multi-sheet-xls-without-excel-installed-powershell) – Lety Feb 17 '16 at 12:56
  • 1
    ssconvert does the trick, perfectly. What I needed was to have each .csv file in a separate sheet. Using cat doesn't achieve that. ssconvert does. – Peter Brooks Feb 22 '16 at 06:37
  • @ash csv and xls are different formats, catting a bunch of csvs to a file and renaming it xls wont work at all, since csvs dont support multiple sheets so it would just make one long csv, and also since csv is a plaintext format whereas xls is a binary format – chiliNUT Feb 23 '16 at 23:19
  • @chiliNUT - I misread the part where OP wanted multiple CSV files into a single spreadsheet as multiple worksheets. However, I was only suggesting a way of concatenating multiple CSV files into a single CSV - I realise it's not what's being asked but I do know the difference between a binary and a text file. – Ash Feb 24 '16 at 01:03
  • wasnt saying you didnt, just that you may or may not hav e been aware that one was binary and the other wasnt, esp. since most of the recent excel formats are no longer binary either – chiliNUT Feb 24 '16 at 01:11

1 Answers1

16

You can use the command ssconvert.

ssconvert example.csv example.xls

To do it for multiple files you have to make a bash loop over csv files and do the job. Here a hint:

for i in *.csv; do ssconvert "$i" "${i%.*}".xls; done

EDIT:

To convert and merge into one single xls file also you still can use ssconvert.

ssconvert --merge-to=output.xls file1.csv file2.csv ....

or easily

ssconvert --merge-to=output.xls *.csv 
Maythux
  • 82,867
  • 54
  • 239
  • 271
  • 1
    It is vital to quote the variables, as `"$i" "${i%.*}".xls`, otherwise any file name containing a space will break the command (and potentially overwrite an unrelated file). – Paddy Landau Feb 22 '16 at 23:08