views:

104

answers:

3

I want to generate an MS Excel file from PHP. I know one can do something like this:

header ( "Content-type: application/vnd.ms-excel" );
header ( "Content-Disposition: attachment; filename=foo_bar.xls" );

But it will generate a file with just one Sheet. What I want is generating a file with multiple sheets. How can I do that? Maybe there's a third party library, but I haven't found too much.

+5  A: 

Try looking at PHPExcel. This is a simple example that creates an Excel file with two sheets:

<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');

// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();

// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');

// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Mark Baker
Thanks for editing in an example Cristian. Hope you find the library easy to use
Mark Baker
This library is simply awesome... in fact, I'm want to donate an amount of money. Thanks for sharing!
Cristian
@Christian - I do have a vested interest in sharing: I am one of the main authors :-)
Mark Baker
OMG! Thanks for your work... it's really amazing.
Cristian
+2  A: 

If you mean like have your PHP script create an Excel file, write some stuff to it on any sheet, etc, then offer that up for the client to download, you can just use PHP's built-in COM extension. See: http://us2.php.net/manual/en/class.com.php for all sorts of examples. However, you will need Excel (or a clone like OpenOffice) installed on the server. If you don't, perhaps Mark Baker's answer above will work instead without it.

bob-the-destroyer
I don't believe that Open Office supports COM, I think that's limited to a range of MS applications, which does limit this option to a Windows server. COM is pretty powerful, and very efficient, but sadly not well documented
Mark Baker
@Mark Baker: OpenOffice supports COM. See: http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Automation_Bridge
bob-the-destroyer
Thanks for the pointer, that's very useful to know... I'll have to do some playing with it
Mark Baker
Np. Mimicking COM on other OS's is possible (XPCOM, CORBA), but it looks painfully complex, and unknown if PHP would even work with those. I'm sure you can also fake it using WINE. OSX (http://macdevcenter.com/pub/a/mac/2004/04/16/com_osx.html?page=1) should work with it out of the box.
bob-the-destroyer
Forgot to mention: I can't find the citation on PHP's manual any longer, but it did suggest not using COM on a server. This is good advice. If clients are accessing the same Excel file, corruptions can occur. If something causes a popup prompt like "Are you sure you want to do this?", the request will hang expecting *the server itself* to click OK.
bob-the-destroyer
A: 

I ended up having a devil of a time trying to do the same thing until I found this library, which is awesome and produces multiple-worksheet Excel files very easily. Highly recommended.

http://web.burza.hr/blog/php-class-for-exporting-data-in-multiple-worksheets-excel-xml/

NChase