kieranbarnes Independent PHP, WordPress & CubeCart Programmer

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

Posted on July 3, 2007

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 Prows' xls2csv 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
make test
make install


Related posts

  1. Why does perl consume my entire CPU?
    I use a perl script to convert an Excel file to CSV on an Ubuntu...
  2. Minify CSS on the fly with nginx
    Code minification rules, but it sucks. The process of removing all unnecessary characters from source...
  3. FuzzyOCR for SpamAssassin on Ubuntu
    FuzzyOCR is a plugin for SpamAssassin that analyzes the content and properties of images to...
  4. Ridiculously simple NTLM Authentication for Apache (Ubuntu)
    We all know Ubuntu makes things amazingly simple. This is the best I've found so...
  5. Installing mytop on cpanel
    MyTop is one of the best MySQL monitoring tools available, written by a MySQL guru,...

Posted by Kieran


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

  2. wrote a blog based on your article : http://software.krimnet.com/xls/guide-converting-xls-files-csv-with-xls2csv-ubuntu.htm

    mainly for ubuntu newbies ….

    but i have come across a problem, where the xls file do not have worksheet (the xls files are generated by another program).

    so now trying out with jodconverter – which uses openoffice.org as a service

  3. jodconverter works ! tested on ubuntu netbook 9.10 ! and it is a whole lot easier than fiddling with perl scripts and supports a whole lot more file formats.

  4. Open Office can do this, open the excel file, then save as csv.

  5. 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.

  6. i wrote a blog for using gnumeric to convert a multiple worksheet xls file into a csv text file
    http://software.krimnet.com/ubuntu/guide-convert-a-worksheet-a-xls-file-a-csv-file.htm

    if you are already using ubuntu, then installing gnumeric is very easy. and the resource utilisation is much lower than using a headless openoffice.org


Leave a comment

(required)