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 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:
- Why does perl consume my entire CPU?
I use a perl script to convert an Excel file to CSV on an Ubuntu... - FuzzyOCR for SpamAssassin on Ubuntu
FuzzyOCR is a plugin for SpamAssassin that analyzes the content and properties of images to... - Ridiculously simple NTLM Authentication for Apache (Ubuntu)
We all know Ubuntu makes things amazingly simple. This is the best I've found so... - Installing mytop on cpanel
MyTop is one of the best MySQL monitoring tools available, written by a MySQL... - Really freakin’ simple mysql virtual hosting pure-ftpd
This article will guide you through installing pure-ftpd configured for virtual hosts using mysql as...
April 21st, 2009 - 19:34
Thank you for the info, today I’m sharing with you just exactly the same situation you described two years ago
December 9th, 2009 - 07:09
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
December 9th, 2009 - 07:36
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.
July 15th, 2010 - 17:04
Open Office can do this, open the excel file, then save as csv.
July 15th, 2010 - 17:06
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.