2

Looking for a PHP solution to create Excel files on the fly (Think dynamic reporting).

Now I have seen PHP Excel and I know I can create a CSV file but are these by best options?

I running this script on a Linux system using PHP Excel but it doesn't set all the options

$objPHPExcel->getProperties()->setCreator("Phill");
$objPHPExcel->getProperties()->setLastModifiedBy("Phill");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX");

Also if I give the extension .xls instead of the .xlsx it throws a invalid file and doesn't open. (NOTE: I'm using Open Office to view the generated excel sheet)

Wanted to know if there are any other/better solutions out there?

EDIT:

How I save the file

$file = '/path/to/777/dir/file.xlsx'; // not viewable by public
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($file);
pnuts
  • 58,317
  • 11
  • 87
  • 139
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • 2
    The problem is that the MS Excel format is a moving target. I always wind up going back to CSV. – bogeymin Oct 28 '10 at 19:49
  • “Most effective way to create Excel files” – use Excel ;) Other than that, are you sure you want to create an excel file? There might be better options (for example as you mentioned yourself: csv), but that depends on what you are trying to acomplish. – poke Oct 28 '10 at 19:50
  • 2
    [See Mark Baker's list. I think it's complete.](http://stackoverflow.com/questions/3930975/alternative-for-php-excel) – Gordon Oct 28 '10 at 19:52

2 Answers2

4

There are alternatives to PHPExcel, as listed here

But before you dismiss PHPExcel out of hand, I'd like to know why it isn't working. This is something that normally works without any problems; but your code snippet doesn't show anything about saving the file. How are you saving the file: which writer are you using?

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('/path/to/777/dir/file.xls');

EDIT

or

$file = '/path/to/777/dir/file.xls'; // not viewable by public 
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
$objWriter->save($file); 

The latest version of Open Office will (I believe) read .xlsx files as well, but it does prefer if they have the correct extension.

Note that the Excel5 Writer doesn't support document properties (though Excel2007 does). There is an active work item for this on the PHPExcel issues list.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • @Phil - you're trying to save using the Excel2007 writer, which creates Exzcel2007 (and Excel2010) .xlsx files. You need to use the Excel5 writer to create BIFF8 .xls files. – Mark Baker Oct 28 '10 at 19:54
  • I see, you have two different functions for different formats. Makes sense. BTW not dismissing PHP Excel it's just not setting all the properties as in the example files, but is this due to me reading it in Open Office? – Phill Pafford Oct 28 '10 at 19:56
0

Yes there is. You can use Pear's Spreadsheet_Excel_Writer. It only creates BIFF8 or older files (Word 2003), but it works quite well.

Edit: I just noticed they are saying it needs a complete rewrite and don't recommend using it for new development. I've been using it for about 3 years in production systems, and other than a few tiny little bugs it's worked great for me...

ircmaxell
  • 163,128
  • 34
  • 264
  • 314