opening excel files in php

Tuesday 3rd July 2007 05:35 PM

TAGS: excel, PHP
Sometimes it's really convenient to be able to open an excel spreadsheet up and grab the data - a common example is clients who can't use anything other than excel files (or refuse to use anything else like CSV).

If you're using PHP on Windows, you can use the inbuilt COM library

$excel = new COM("excel.application");
$excel->Workbooks->Open("filename.xls");

A solution I've found that works well on Linux machines, is PHPExcelReader, and it seems to work very efficiently and easily with only a few lines of code.

$data = new Spreadsheet_Excel_Reader();
$data->read("filename.xls");

And you can access the data with a simple loop:

for($i=1; $i <= $data->sheets[0]['numRows']; $i++)
{

  echo 'Row '. $i .', Column a:' $data->sheets[0]['cells'][$i][1] . '<br />';
  echo 'Row '. $i .', Column b:' $data->sheets[0]['cells'][$i][2] . '<br />';

}

Comments on this article:


#1 Jeroen Mulder says:

Tuesday 3rd July 2007 07:37 PM

What a coincidence! I actually need to write Excel spreadsheets with PHP somewhere this week. Thanks :-)


Add Comment:


Make a Comment

*Nb, all comments are moderated to prevent spam or inappropriate content.








netforge logo
netforge provides high quality and friendly website design services to business. We're Australian based and reliable... (find out more).