Convert a CSV file to a XLS file on the linux command line? [closed]
Want to improve this question? Update the question so it’s on-topic for Server Fault.
Closed 5 years ago .
I’m using Debian Linux and I want to convert a CSV file to an Excel XLS Spreadsheet file. The catdoc package includes the xls2csv command that converts from XLS to CSV. However it doesn’t do the reverse.
Since I just have a CSV file, I don’t care about formatting or anything like that. I’m not worried if it only generates a very simple XLS file, and doesn’t support the fancy new versions. Just so long as it’s an XLS spreadsheet.
9 Answers 9
Why would you want to do that? Since you are not interested in adding or modifying data/metadata, you could just leave it as csv. CSV is associated with MS Excel by default, so whoever is going to open it, is going to get it opened in MS Excel.
If that does not work for you for some reason, keep in mind, that, as far as I know, you can generate a (sort of) valid xls file by using the following skeleton:
Try using the ssconvert tool from «gnumeric» package. On Debian, install the package with sudo apt-get install gnumeric and then from the command-line, run:
This should do the job.
There is a python based solution on Sourceforge called csv2xls which may fit the bill.
It does not appear to be maintained at the moment (last activity was over a year ago) but if it does what you need then that shouldn’t matter too much.
Try using an Openoffice macro.
See for a start : How to convert Word -> PDF from the command line http://www.oooforum.org/forum/viewtopic.phtml?t=3772
There are indeed some problems with the filters from CSV and any form XLS or even openOffice. Like the: Record separator: , Line terminator: \n or ; String definition: » or the like.
If I get it correctly you want to do it on the command line within some scripts. This is exactly the same problem I have right now: from DATA in csv format, present it to the customer in XLS format.
But I am not totally satisfied. They might be an option.
Indeed if you have some sort of openOffice installed you can try to play with some info from: http://ask.libreoffice.org/en/question/2641/convert-to-command-line-parameter/
On my shell I typed:
And part of the output is:
I would write a little code to directly output a simple version of the Excel 2002 XML format: http://en.wikipedia.org/wiki/Microsoft_Excel#File_formats
XML Spreadsheet
Though the intended file extension for this format is .xml, the program also correctly handles XML files with .xls extension. This feature is widely used by third-party applications (e.g. MySQL Query Browser) to offer «export to Excel» capabilities without implementing binary file format. The following example will be correctly opened by Excel if saved either as Book1.xml or Book1.xls:
Alternately, if that didn’t provide enough features, I’d directly implement minimal .xlsx output. It is a zip file with XML files which should be tractable compared to the old binary formats.
Convert xlsx to csv in Linux with command line
I’m looking for a way to convert xlsx files to csv files on Linux.
I do not want to use PHP/Perl or anything like that since I’m looking at processing several millions of lines, so I need something quick. I found a program on the Ubuntu repos called xls2csv but it will only convert xls (Office 2003) files (which I’m currently using) but I need support for the newer Excel files.
10 Answers 10
The Gnumeric spreadsheet application comes with a command line utility called ssconvert that can convert between a variety of spreadsheet formats:
To install on Ubuntu:
If you already have a Desktop environment then I’m sure Gnumeric / LibreOffice would work well, but on a headless server (such as Amazon Web Services), they require dozens of dependencies that you also need to install.
I found this Python alternative:
Took 2 seconds to install and works like a charm.
If you have multiple sheets you can export all at once, or one at a time:
He also links to several alternatives built in Bash, Python, Ruby, and Java.
You can do this with LibreOffice:
For reasons not clear to me, you might need to run this with sudo. You can make LibreOffice work with sudo without requiring a password by adding this line to you sudoers file:
In bash, I used this libreoffice command to convert all my xlsx files in the current directory:
It takes care of spaces in the filename.
Tried again some years later, and it didn’t work. This thread gives some tips, but the quickiest solution was to run as root (or running a sudo libreoffice ). Not elegant, but quick.
Use the command scalc.exe in Windows
For details check their excellent docs
Another option would be to use R via a small bash wrapper for convenience:
If .xlsx file has many sheets, -s flag can be used to get the sheet you want. For example:
second_sheet.csv would contain data of 2nd sheet in my_file.xlsx .
Using the Gnumeric spreadsheet application which comes which a commandline utility called ssconvert is indeed super simple:
If you are OK to run Java command line then you can do it with Apache POI HSSF’s Excel Extractor. It has a main method that says to be the command line extractor. This one seems to just dump everything out. They point out to this example that converts to CSV. You would have to compile it before you can run it but it too has a main method so you should not have to do much coding per se to make it work.
Another option that might fly but will require some work on the other end is to make your Excel files come to you as Excel XML Data or XML Spreadsheet of whatever MS calls that format these days. It will open a whole new world of opportunities for you to slice and dice it the way you want.
MX Linux Forum
Support for MX and antiX Linux distros
New users
- Please read this first, and don’t forget to add system and hardware information to posts!
- Here are the Forum Rules
Current releases
— MX-19.2 release info here
— MX-19.2 KDE release info here
— Migration information to MX-19.2 here
— antiX-19 release info here
Batch conversion of .csv to .xls
Batch conversion of .csv to .xls
Post by Gaer Boy » Sun Aug 05, 2018 5:24 am
I’m looking for a quick method for converting about 12 .csv files to .xls. I do this monthly, currently opening each file in LibreOffice and saving as .xls. It’s a boring, repetitive process. I need a local solution — the online converters I’ve found so far don’t make the task any quicker.
Re: Batch conversion of .csv to .xls
Post by Jerry3904 » Sun Aug 05, 2018 5:34 am
This looks promising:
If it’s a simple data file, I usually just open a terminal in the folder the files are in and do this:
Re: Batch conversion of .csv to .xls
Post by Gaer Boy » Sun Aug 05, 2018 10:32 am
Thanks, Jerry. csv2odf certainly looks a good and simple possibility. The description doesn’t include .xls as an output format, only .xlsx, which Filemaker rejects. However, the examples in the documentation shows an output template for .xls, so it may be OK. It does mean that I have to create templates for the 3 different tables involved. I don’t have time to do that this month but I’ll have a go before next month.
The simple mv command doesn’t work. Filemaker doesn’t recognise the separate fields in the .xls file so created.
Re: Batch conversion of .csv to .xls
Post by topcat » Sun Aug 05, 2018 3:05 pm
This sounds like a fairly easy Excel macro using VBA (Visual Basic for Applications that comes with Excel).
Libreoffice also has a basic macro language called Libreoffice Basic.
Do you have Excel available to use for this or do you need a linux-only solution?
I did a lot of work using Excel VBA as a front-end to our budgeting system using IBM Cognos TM1 and it probably wouldn’t take me long to create one for Excel.
Zotac ZBOX-ID91-U (desktop system)
SanDisk Extreme Pro 240GB SATA III SSD
G.SKILL 8GB DDR3 1600 (PC3 12800)
ZOTAC ZBOX nano ID62-U (media streamer)
Silicon Power Slim S60 120GB SATA III SSD
G.SKILL Ripjaws 4GB DDR3L 1600 (PC3L 12800)
Re: Batch conversion of .csv to .xls
Post by Jerry3904 » Sun Aug 05, 2018 3:20 pm
Re: Batch conversion of .csv to .xls
Post by topcat » Sun Aug 05, 2018 3:21 pm
Sample code here from a google search:
Haven’t tried it but it looks like it should work. It is a windows/excel solution. You would just need to replace the file folder to work with.
It would be interesting to learn how to use Libreoffice Basic to do this.
Zotac ZBOX-ID91-U (desktop system)
SanDisk Extreme Pro 240GB SATA III SSD
G.SKILL 8GB DDR3 1600 (PC3 12800)
ZOTAC ZBOX nano ID62-U (media streamer)
Silicon Power Slim S60 120GB SATA III SSD
G.SKILL Ripjaws 4GB DDR3L 1600 (PC3L 12800)
Re: Batch conversion of .csv to .xls
Post by Gaer Boy » Sun Aug 05, 2018 5:37 pm
Thanks. I don’t have Excel (and have never used any Microsoft apps), but I would think the same thing is possible with a LO macro. That’s probably a big learning curve for me — my experience with scripting is Lotus 123 (DOS), Lotus Approach and Filemaker. All slightly different and full of traps when switching from one to the other.
As far as I’ve found already, I need to go through LO and produce .xls files. Filemaker rejects .xlsx.
I’m away for the next week with no internet (although rumour has it that a new mobile mast is about to be commissioned, so that may change).
Re: Batch conversion of .csv to .xls
Post by fehlix » Sun Aug 05, 2018 5:50 pm
I’m looking for a quick method for converting about 12 .csv files to .xls. I do this monthly, currently opening each file in LibreOffice and saving as .xls. It’s a boring, repetitive process. I need a local solution — the online converters I’ve found so far don’t make the task any quicker.