Converting XLS to CSV. Simple, eh? Well, sorta.

I hate problems that can be solved really easily. (With a totally different set of tools). I needed to open Excel (XLS) files in PHP. Simple enough you think, IF you are using Windows.

IF I was using Windows, I would have a bunch of options open to me, I could use PHP’s COM & .NET functions. Or PHP’s ODBC functions. Or a selection of classess from PHP Classes.

BUT I am using Linux. Just to be awkward. So I don’t have any of those options.

AND to really confuse things, there are two packages called xls2csv.

  • Ken Prows provides a Perl tool. View site.
  • Vitus Wagner provides C tool part of the catdoc package. View site. (Currently down).

The best alternative to COM & ODBC I can find is Ken Prowsxls2csv package. I’d have to call this Perl utility via system() or similar from PHP. Already it sounds ugly.

Sometimes making a valid business decision being lazy pays off. I use Ubuntu. I can use apt to install catdoc

apt-get install catdoc

Job done. Usage is fairly straight forward. There is a man page here.

I slightly prefer Ken’s version of this tool for a few reasons;

  • His site works. Well, not his, CPAN’s.
  • It has a -q for quiet mode.

Here’s how I installed it,

First off, You need a whole bunch of modules you’ve never heard of before.
It’s probably worth throwing that lot into an interactive CPAN shell and letting it do the hard work.perl -MCPAN -e 'shell'

  • Locale::Recode
  • Unicode::Map
  • Spreadsheet::ParseExcel
  • Spreadsheet::ParseExcel::FmtUnicode (Should come with the above).
  • Text::CSV_XS

Now, download the source from Ken Prows‘ site.

CPAN wouldn’t build xls2csv from the shell, but when I ran it manually, it seemed fine.

tar zxpfv xls2csv-1.06.tar.gz
cd xls2csv-1.06
perl Makefile.PL
make test
make install

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

8 thoughts on “Converting XLS to CSV. Simple, eh? Well, sorta.”

  1. Thank you for the info, today I’m sharing with you just exactly the same situation you described two years ago

  2. Correct Jim, it can. Microsoft Office can do this and a whole load of GUI programs. The problem arises when you need to do it programmatically from a script running in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *