Home Features Requirements Examples FAQ Contribute Documents Credits Contact

Examples

Examples can be found in the test package in the latest release.

File examples

The following examples are included, each representing another feature of PHPExcel:

Hello World example

A simple "Hello World" example, which results in a file like 01simple.xlsx:
<?php
/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$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, generated using PHP classes.");


// Add some data
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');

		
// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
Last edited Aug 7 2007 at 8:40 AM by maartenba, version 12
Comments
bdp Jun 13 2007 at 3:26 AM 
I don't understand the file excel where is saved ???
where is the object "ZipArchive" ($objZip = new ZipArchive()) ?
i don't understand the object "ZipArchive" in the function "save" in the file "Excel2007.php" ?
can u explain to me ?
thanks

MarkBaker Jun 20 2007 at 12:36 PM 
bdp -> You need to have a build of PHP with Zip enabled.
Visit http://www.php.net/manual/en/ref.zip.php for instructions on how to do this

freddy1984 Jun 30 2007 at 10:40 AM 
Hello! I have the problem when execute the first example. I use wamp (php5 and php_zip.dll enabled). the problem is this:
Fatal error: Class 'ZipArchive' not found in C:\wamp\www\1.3.5\Classes\PHPExcel\Writer\Excel2007.php on line 243.
What is the problem? Can you explain to me?
Thanks

perry76 Jul 2 2007 at 3:07 PM 
Hi there. I have the same problem as freddy1984. When I run any example: "15:59:54 Create new PHPExcel object 15:59:54 Set properties 15:59:54 Add some data 15:59:54 Rename sheet 15:59:54 Write to Excel2007 format
Fatal error: Class 'ZipArchive' not found in /home/****/****/*******/Classes/PHPExcel/Writer/Excel2007.php on line 243"

Please explain to us what the problem is.

Regards,
PB, Sweden

handrea Jul 4 2007 at 10:34 PM 
Hi there. I need to open the file . How I do that???? But I only find that I can save it.

If someone knows please help me.

funjay Jul 13 2007 at 12:22 AM 
Is there somewhere I can get sample code for the other examples? Specifically the conditional format example?

l0fty Jul 26 2007 at 9:52 AM 
This is a really cool project, I am having one difficulty though. I am more interested in the reading of the excel files and parsing information into a database.
When I run the script from the cli version it works just fine, when trying it through a browser I get the following error..

Warning: simplexml_load_file(zip://05featuredemo.xlsx#_rels/.rels) [function.simplexml-load-file]: failed to open stream: No such file or directory in C:\apache\coolscripts\excel\Classes\PHPExcel\Reader\Excel2007.php on line 85

any ideas?

xinhaozheng Dec 10 2007 at 8:24 AM 
any support for utf-8?i i can not insert chinese text in the file when use excel5.

ck862 May 20 2008 at 9:06 AM 
Well Done!
Hey Guys! Read the documentation first plz!
The following software is required to develop using PHPExcel:
» PHP version 5.2 or newer
» PHP extension php_zip enabled

nathank Jun 28 2008 at 8:42 PM 
Iam trying to read a cell value in a .xls file. Which function shoud I use. The example code is not having any function which does this functionality. or internet I couldnt get the information.
Any help?

nathank Jun 29 2008 at 7:31 AM 
I got to know how to do it, use
$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load("c:\profile.xls");
$val = ($objPHPExcel->getActiveSheet()->getCell('A1'));
$temp = $val->getvalue();//returns the value in the cell

kv3184 Jul 21 2008 at 4:24 PM 
Y have the same problem as nathank, I need to read the cells in a .xlsx file. I'm using the same lines but I can't get the information. Help me please.

skeef Nov 19 2008 at 5:08 PM 
Hi!
Please, write any examle parsing Excel2007 file.
Thanks

xxxbetoxxx Nov 22 2008 at 11:58 PM 
hi!!
this is a example parsing Excel 2007 file

$objReader = PHPExcel_IOFactory::createReader('Excel2007');//use excel2007
$objPHPExcel = $objReader->load("c:\profile.xls"); //DIR ARCHIVE
$val = ($objPHPExcel->getActiveSheet()->getCell('A1')); //CELL
$temp = $val->getvalue();//returns the value in the cell

BYE!!

xxxbetoxxx Nov 23 2008 at 12:03 AM 
A COMPLETE EXAMPLE

/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/'); //Classes dir

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('Excel2007');//excel2007
$objPHPExcel = $objReader->load("c:\profile.xlsx"); //ARCHIVE excel2007 dir
$val = ($objPHPExcel->getActiveSheet()->getCell('A1')); //CELL
$temp = $val->getvalue();//returns the value in the cell

mira360 Feb 21 at 1:24 AM 
please help!!!
i try to use a xls template, open and write on this template.
Can you write a example? please.

mira360 Feb 21 at 1:33 AM 
ok thanks i find the solution on a examples, thank you and excuse me.

echo date('H:i:s') . " Load from Excel2007 file\n";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("05featuredemo.xlsx");

echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

mira360 Feb 23 at 6:59 PM 
URGENT HELP PLEASE

many times i have this error mensage on my aplication:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 393216 bytes) in.........
¿why? i need to change any parameter ¿Where?
thanks all

koyama Feb 24 at 6:31 AM 
@mira360: You need to increase memory limit. Check this thread:
http://www.codeplex.com/PHPExcel/Thread/View.aspx?ThreadId=36847
If you need more help, you can also try posting in the discussions.

skeef Apr 7 at 6:29 AM 
Hi!
How do I find the last cell/row in xlsx document?
Thanks all

Aslan2000 May 7 at 7:26 PM 
Hi! Please hekp me. How to download PHPExcel 1.6.7?

samaj Jul 14 at 1:58 PM 
Hy , i used this code for the applying the reachtext but it cant working so hepl me
i would like to highlite some text in difert font,style bold so i thinki have to use rach text but it's can't working so please help me

$objRichText = new PHPExcel_RichText();
$objRichText->createText('saome text');
$objPayable = $objRichText->createTextRun('saometextfor highlight');
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
$objRichText->createText(', unless specified otherwise on the invoice.');

$objPHPExcel->getActiveSheet()->getCell('A'.$ROW)->setValue($objRichText);

samaj Jul 14 at 2:01 PM 
and also i would like to save one copy of xlsx file in my another folder for the backup so please say to me how can i set this

Intercolombia Sep 15 at 9:56 PM 
Hello I have a problem. Please can u help me?
I tryed to put in a cell the formula:

$E->getActiveSheet()->setCellValue('L6','=IF(K6;K6;IF(J6=0;I6;0))');

And the excecution always print this error:

Fatal error: Uncaught exception 'Exception' with message 'Formula Error: An unexpected error occured' in /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Calculation.php:2860 Stack trace: #0 /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Calculation.php(2442): PHPExcel_Calculation->_raiseFormulaError('Formula Error: ...') #1 /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Calculation.php(1883): PHPExcel_Calculation->_parseFormula('IF(K6;K6;IF(J6=...') #2 /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Calculation.php(1780): PHPExcel_Calculation->_calculateFormulaValue('=IF(K6;K6;IF(J6...', 'L6', Object(PHPExcel_Cell)) #3 /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Cell.php(281): PHPExcel_Calculation->calculateCellValue(Object(PHPExcel_Cell), true) #4 /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Writer/Excel2007/Worksheet.php(985): PHPExcel_Cell->getCalculatedValue() #5 /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Writer/Excel2007/Worksheet.php(929): PHPExcel_Writer_Excel in /home/dsfarmed/public_html/Control/inc/Excel/PHPExcel/Calculation.php on line 2860

What is wrong? thanks.

MarkBaker Oct 4 at 11:44 AM 
@Intercolombia
It looks as though you're using a semi-colon (;) as a parameter separator. The PHPExcel calculation engine requires a comma (,) as the parameter separator

$E->getActiveSheet()->setCellValue('L6','=IF(K6,K6,IF(J6=0,I6,0))');

yusuf Nov 11 at 10:31 AM 
ı have xls template, title cells bg color : #d8d8d8, description cell bgcolor color : #f2f2f2.
when ı try to write in my template ı get all cellbg colors only #d8d8d8.
all cells with bg color #f2f2f2 replaced with #d8d8d8..
pls help, it s a big template with 120 rows :(

koyama Nov 12 at 2:54 AM 
@yusuf: Your xls template was probably created in MS Office Excel 2007 as #f2f2f2 is not a standards palette color in MS Office Excel 2003 (BIFF8). You should probably avoid using non-standard palette colors if your audience only has MS Office Excel 2003 in which case they will not see such extended color information (but only the closest standard color).

With that said, Excel5 reader in latest source code (PHPExcel 1.7.1+) supports RGB colors that are stored as extended color information (XFEXT) so you may have better luck with that. If you need further information, explain the problem in a discussion post in the forum, and we will try to answer there.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987