You are on page 1of 34

PHPExcel Developer Documentation

Author: Maarten Balliauw


Version: 1.6.7
Date: 2 July 2014
1. Contents
PHPExcel Developer Documentation............................................................................1
1. ontent!..........................................................................................................2
2. Prere"ui!ite!.....................................................................................................#
#. $rc%itecture..................................................................................................... &
4. reatin' a !prea(!%eet.........................................................................................7
&. Per)ormin' )ormula calculation!............................................................................22
6. *ea(in' an( writin' to )ile...................................................................................24
7. re(it!........................................................................................................... #2
$ppen(ix $+ ,ali( array -ey! )or !tyle apply.rom$rray/0...................................................##
PHPExcel Developer Documentation 2
2. Prerequisites
2.1. Software requirements
1%e )ollowin' !o)tware i! re"uire( to (evelop u!in' PHPExcel+
PHP ver!ion &.2 or newer
PHP exten!ion p%p23ip ena4le(
2.2. Installation instructions
5n!tallation i! "uite ea!y+ copy t%e content! o) t%e la!!e! )ol(er to any location
in your application re"uire(.
$)terwar(!6 ma-e !ure you can inclu(e all PHPExcel )ile!. 1%i! can 4e ac%ieve( 4y
re!pectin' a 4a!e )ol(er !tructure6 or 4y !ettin' t%e PHP inclu(e pat%6 )or example+
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/PHPExcel/');
2.3. Useful links and tools
1%ere are !ome lin-! an( tool! w%ic% are very u!e)ul w%en (evelopin' u!in' PHPExcel. Plea!e re)er
to t%e PHPExcel o(ePlex pa'e! )or an up(ate ver!ion o) t%e li!t 4elow.
2.3.1. OpenXML / prea!s"eetML
File format documentation
%ttp+77www.ecma8international.or'7new!714&2current2wor-714&2availa4le2(oc!.%tm
OpenXML Explained e-boo
%ttp+77openxml(eveloper.or'7article!71970.a!px
Microsoft Office !ompatibilit" #ac for $ord% Excel% and #o&er#oint '(() File Formats
%ttp+77www.micro!o)t.com7(ownloa(!7(etail!.a!px:)amilyi(;9414#4708#ae984aee8<)4#8
c64474c(1466=(i!playlan';en
OpenXML #aca*e Explorer
%ttp+77www.co(eplex.com7Pac-a'eExplorer7
2.3.2. #requentl$ as%e! questions
1%e up8to8(ate ..$.>. pa'e )or PHPExcel can 4e )oun( on
%ttp+77www.co(eplex.com7PHPExcel7?i-i7,iew.a!px:title;.$>=re)errin'1itle;*e"uirement!.
&"ere seems to 'e a pro'lem (it" c"aracter enco!in)...
5t i! nece!!ary to u!e @1.8< enco(in' )or all text! in PHPExcel. 5) t%e !cript u!e! (i))erent enco(in'
t%en it i! po!!i4le to convert t%e text! wit% PHPA! iconv/0 )unction.
PHP complains a'out *ip+rc"ive not 'ein) ,oun!
Ma-e !ure you meet all *e"uirement!6 e!pecially p%p23ip exten!ion !%oul( 4e ena4le(.
Excel 2--. can not open t"e ,ile )enerate! '$ PHPExcel/0riter/2--. on 0in!o(s
Excel found unreadable content in '*.xlsx'. Do you want to recover the contents of this workbook?
If you trust the source of this workbook, click Yes.
Bome ver!ion! o) t%e p%p23ip exten!ion on ?in(ow! contain an error w%en creatin' C5P )ile!. 1%e
ver!ion t%at can 4e )oun( on %ttp+77!nap!.p%p.net7win#27p%p&.28win#28late!t.3ip !%oul( wor- at
all time!.
PHPExcel Developer Documentation #
Protection on m$ (or%s"eet is not (or%in)1
?%en you ma-e u!e o) any o) t%e wor-!%eet protection )eature! /e.'. cell ran'e protection6
pro%i4itin' (eletin' row!6 ...06 ma-e !ure you ena4le wor-!%eet !ecurity. 1%i! can )or example 4e
(one li-e t%i!+
o!"PHPExcel#$getActi%eSheet()#$getP&otection()#$setSheet(t&ue);
#eature X is not (or%in) (it" PHPExcel/0riter/2
Dot all )eature! o) PHPExcel are implemente( in t%e *ea(er 7 ?riter cla!!e!. 1%i! i! mo!tly (ue to
un(erlyin' li4rarie! not !upportin' a !peci)ic )eature or not %avin' implemente( a !peci)ic )eature.
.or example auto)ilter i! not implemente( in PE$* Bprea(!%eet2Excel2writer6 w%ic% i! t%e 4a!e o)
our Excel& writer.
#ormulas !on3t seem to 'e calculate! in Excel2--3 usin) compati'ilit$ pac%1
1%i! i! normal 4e%aviour o) t%e compati4ility pac-6 Excel2007 (i!play! t%i! correctly. @!e
PHPExcel2?riter2Excel& i) you really nee( calculate( value!6 or )orce recalculation in Excel200#.
ettin) column (i!t" is not 1--4 accurate
1ryin' to !et column wi(t%6 5 experience one pro4lem. ?%en 5 open t%e )ile in Excel6 t%e actual
wi(t% i! 0.71 point! le!! t%en it !%oul( 4e.
Eoo-! li-e u!in' ali4ri a! t%e (e)ault )ont lea(! to t%i! pro4lem+
%ttp+77!upport.micro!o)t.com7-47214#94
1%i! can 4e )ixe( /w%en nee(e(0 4y u!in' anot%er (e)ault )ont+
o!"PHPExcel#$getActi%eSheet()#$get'e(aultSt)le()#$get*ont()#$set+a,e('A&ial -)&');
2.3.3. &utorials
French #+#Excel tutorial
%ttp+77'8ernael!ten.(eveloppe3.com7tutoriel!7excel20077
PHPExcel Developer Documentation 4
3. +rc"itecture
3.1. Schematical
3.2. Spreadsheet in memory
PHPExcelF! arc%itecture i! 4uilt in a way t%at it can !erve a! an in8memory !prea(!%eet. 1%i! mean!
t%at6 i) one woul( want to create a we4 4a!e( view o) a !prea(!%eet w%ic% communicate! wit%
PHPExcelF! o4Gect mo(el6 %e woul( only %ave to write t%e )ront8en( co(e.
Ju!t li-e (e!-top !prea(!%eet !o)tware6 PHPExcel repre!ent! a !prea(!%eet containin' one or more
wor-!%eet!6 w%ic% contain cell! wit% (ata6 )ormula!6 ima'e!6 H
3.3. Readers and writers
In it! own6 PHPExcel (oe! not provi(e t%e )unctionality to rea( )rom or write to a per!i!te(
!prea(!%eet /on (i!- or in a (ata4a!e0. 1o provi(e t%at )unctionality6 rea(er! an( writer! can 4e
u!e(.
By (e)ault6 t%e PHPExcel pac-a'e provi(e! !ome rea(er! an( writer!6 inclu(in' one )or t%e Ipen
JME !prea(!%eet )ormat /a.-.a. Excel 2007 )ile )ormat0. Kou are not limite( to t%e (e)ault rea(er!
an( writer!6 a! you are )ree to implement t%e PHPExcel2?riter25*ea(er an(
PHPExcel2?riter25?riter inter)ace in a cu!tom cla!!.
PHPExcel Developer Documentation &
PHPExcel Developer Documentation 6
5. Creatin) a sprea!s"eet
4.1. The !"#cel class
1%e PHPExcel cla!! i! t%e core o) PHPExcel. 5t contain! re)erence! to t%e containe( wor-!%eet!6
(ocument !ecurity !ettin'! an( (ocument meta (ata.
1o !impli)y t%e PHPExcel concept+ t%e PHPExcel cla!! repre!ent! your wor-4oo-.
4.2. $orksheets
$ wor-!%eet i! a collection o) cell!6 )ormulaF!6 ima'e!6 'rap%!6 H 5t %ol(! all (ata you want to
repre!ent a! a !prea(!%eet wor-!%eet.
4.3. %ccessin& cells
$cce!!in' cell! in a PHPExcel wor-!%eet !%oul( 4e pretty !trai'%t)orwar(. 1%i! topic li!t! !ome o)
t%e option! to acce!! a cell.
5.3.1. ettin) a cell value '$ coor!inate
Bettin' a cell value 4y coor(inate can 4e (one u!in' t%e wor-!%eetF! set-ell.alue met%o(.
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/0'1 'So,e %alue');
5.3.2. 6etrievin) a cell '$ coor!inate
1o retrieve t%e value o) a cell6 t%e cell !%oul( )ir!t 4e retrieve( )rom t%e wor-!%eet u!in' t%e
get-ell met%o(. $ cellF! value can 4e rea( a'ain u!in' t%e )ollowin' line o) co(e+
o!"PHPExcel#$getActi%eSheet()#$get-ell('/0')#$get.alue();
5) you nee( t%e calculate( value o) a cell6 u!e t%e )ollowin' co(e. 1%i! i! )urt%er explaine( in 4.4.##
Bettin' t%e (e)ault column wi(t%.
o!"PHPExcel#$getActi%eSheet()#$get-ell('/0')#$get-alculated.alue();
5.3.3. ettin) a cell value '$ column an! ro(
Bettin' a cell value 4y coor(inate can 4e (one u!in' t%e wor-!%eetF!
set-ell.alue/)-olu,nAndRo2 met%o(.
// Set cell /0
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue/)-olu,nAndRo2(31 01 'So,e %alue');
5.3.5. 6etrievin) a cell '$ column an! ro(
1o retrieve t%e value o) a cell6 t%e cell !%oul( )ir!t 4e retrieve( )rom t%e wor-!%eet u!in' t%e
get-ell/)-olu,nAndRo2 met%o(. $ cellF! value can 4e rea( a'ain u!in' t%e )ollowin' line o)
co(e+
// 4et cell /0
o!"PHPExcel#$getActi%eSheet()#$get-ell/)-olu,nAndRo2(31 0)#$get.alue();
5) you nee( t%e calculate( value o) a cell6 u!e t%e )ollowin' co(e. 1%i! i! )urt%er explaine( in 4.4.##
Bettin' t%e (e)ault column wi(t%.
// 4et cell /0
o!"PHPExcel#$getActi%eSheet()#$get-ell/)-olu,nAndRo2(31 0)#$get-alculated.alue();
5.3.7. Loopin) cells
Loopin) cells usin) iterators
1%e ea!ie!t way to loop cell! i! 4y u!in' iterator!. @!in' iterator!6 one can u!e )oreac% to loop
wor-!%eet!6 row! an( cell!.
PHPExcel Developer Documentation 7
Below i! an example w%ere we rea( all t%e value! in a wor-!%eet an( (i!play t%em in a ta4le.
56php
o!"Reade& 7 PHPExcel_8O*acto&)99c&eateReade&('Excel:;;<');
o!"Reade&#$setRead'ataOnl)(t&ue);
o!"PHPExcel 7 o!"Reade&#$load(=test.xlsx=);
o!">o&?sheet 7 o!"PHPExcel#$getActi%eSheet();
echo '5ta!le$' . =@n=;
(o&each (o!">o&?sheet#$getRo28te&ato&() as &o2) A
echo '5t&$' . =@n=;
cell8te&ato& 7 &o2#$get-ell8te&ato&();
cell8te&ato&#$set8te&ateOnl)Existing-ells((alse); // This loops all cells1
// e%en i( it is not set.
// /) de(ault1 onl) cells
// that a&e set 2ill !e
// ite&ated.
(o&each (cell8te&ato& as cell) A
echo '5td$' . cell#$get.alue() . '5/td$' . =@n=;
B

echo '5/t&$' . =@n=;
B
echo '5/ta!le$' . =@n=;
6$
Dote t%at we %ave !et t%e cell iteratorF! set8te&ateOnl)Existing-ells() to )al!e. 1%i! ma-e!
t%e iterator loop all cell!6 even i) t%ey were not !et 4e)ore.
,he cell iterator &ill return null as the cell if it is not set in the &orsheet-
Bettin' t%e cell iteratorF! set8te&ateOnl)Existing-ells()to (alse will loop all cell! in t%e wor-!%eet
t%at can 4e availa4le at t%at moment. 1%i! will create new cell! i) re"uire( an( increa!e memory u!a'eL Inly
u!e it i) it i! inten(e( to loop all cell! t%at are po!!i4ly availa4le.
Loopin) cells usin) in!exes
Ine can u!e t%e po!!i4ility to acce!! cell value! 4y column an( row in(ex li-e /0610 in!tea( o) A$1A
)or rea(in' an( writin' cell value! in loop!.
Dote+ 5n PHPExcel column in(ex i! 084a!e( w%ile row in(ex i! 184a!e(. 1%at mean! A$1A M /0610
Below i! an example w%ere we rea( all t%e value! in a wor-!%eet an( (i!play t%em in a ta4le.
56php
o!"Reade& 7 PHPExcel_8O*acto&)99c&eateReade&('Excel:;;<');
o!"Reade&#$setRead'ataOnl)(t&ue);
o!"PHPExcel 7 o!"Reade&#$load(=test.xlsx=);
o!">o&?sheet 7 o!"PHPExcel#$getActi%eSheet();
highestRo2 7 o!">o&?sheet#$getHighestRo2(); // e.g. 3;
highest-olu,n 7 o!">o&?sheet#$getHighest-olu,n(); // e.g '*'
highest-olu,n8ndex 7 PHPExcel_-ell99colu,n8ndex*&o,St&ing(highest-olu,n); // e.g.
C
echo '5ta!le$' . =@n=;
(o& (&o2 7 3; &o2 57 highestRo2; DD&o2) A
echo '5t&$' . =@n=;
(o& (col 7 ;; col 57 highest-olu,n8ndex; DDcol) A
PHPExcel Developer Documentation <
echo '5td$' . o!">o&?sheet#$get-ell/)-olu,nAndRo2(col1 &o2)#$get.alue() .
'5/td$' . =@n=;
B
echo '5/t&$' . =@n=;
B
echo '5/ta!le$' . =@n=;
6$
5.3.8. 9sin) value 'in!ers to ,acilitate !ata entr$
5nternally6 PHPExcel u!e! a (e)ault PHPExcel_-ell_8.alue/inde& implementation
/PHPExcel_-ell_'e(ault.alue/inde&0 to (etermine (ata type! o) entere( (ata u!in' a cellF!
set.alue() met%o(.
Iptionally6 t%e (e)ault 4e%aviour o) PHPExcel can 4e mo(i)ie(6 allowin' ea!ier (ata entry. .or
example6 a PHPExcel_-ell_Ad%anced.alue/inde& cla!! i! pre!ent. 5t automatically convert!
percenta'e! an( (ate! entere( a! !trin'! to t%e correct )ormat6 al!o !ettin' t%e cellF! !tyle
in)ormation. 1%e )ollowin' example (emon!trate! %ow to !et t%e value 4in(er in PHPExcel+
/EE PHPExcel E/
&eFui&e_once 'PHPExcel.php';
/EE PHPExcel_-ell_Ad%anced.alue/inde& E/
&eFui&e_once 'PHPExcel/-ell/Ad%anced.alue/inde&.php';
/EE PHPExcel_8O*acto&) E/
&eFui&e_once 'PHPExcel/8O*acto&).php';
// Set %alue !inde&
PHPExcel_-ell99set.alue/inde&( ne2 PHPExcel_-ell_Ad%anced.alue/inde&() );
// -&eate ne2 PHPExcel o!"ect
o!"PHPExcel 7 ne2 PHPExcel();
// ...
// Add so,e data1 &ese,!ling so,e di((e&ent data t)pes
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('AG'1 'Pe&centage %alue9');
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/G'1 '3;H');
// -on%e&ts to ;.3 and sets pe&centage cell st)le
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('AC'1 ''ate/ti,e %alue9');
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/C'1 ':3 'ece,!e& 3I0J');
// -on%e&ts to date and sets date (o&,at cell st)le
!reatin* "our o&n .alue binder is eas"
?%en a(vance( value 4in(in' i! re"ure(6 you can implement t%e PHPExcel_-ell_8.alue/inde& inter)ace
or exten( t%e PHPExcel_-ell_'e(ault.alue/inde& or PHPExcel_-ell_Ad%anced.alue/inde&
cla!!e!.
4.4. !"#cel recipes
1%e )ollowin' pa'e! o))er you !ome wi(ely8u!e( PHPExcel recipe!. Plea!e note t%at t%e!e (o DI1
o))er complete (ocumentation on !peci)ic PHPExcel $P5 )unction!6 4ut Gu!t a 4ump to 'et you
!tarte(. 5) you nee( !peci)ic $P5 )unction!6 plea!e re)er to t%e $P5 (ocumentation.
.or example6 4.4.7 Bettin' a wor-!%eetF! pa'e orientation an( !i3e cover! !ettin' a pa'e orientation
to $4. It%er paper )ormat!6 li-e @B Eetter6 are not covere( in t%i! (ocument6 4ut in t%e PHPExcel
$P5 (ocumentation.
5.5.1. ettin) a sprea!s"eet3s meta!ata
PHPExcel allow! an ea!y way to !et a !prea(!%eetF! meta(ata6 u!in' (ocument property acce!!or!.
Bprea(!%eet meta(ata can 4e u!e)ul )or )in(in' a !peci)ic (ocument in a )ile repo!itory or a
PHPExcel Developer Documentation 9
(ocument mana'ement !y!tem. .or example Micro!o)t B%arepoint u!e! (ocument meta(ata to
!earc% )or a !peci)ic (ocument in it! (ocument li!t!.
Bettin' !prea(!%eet meta(ata i! (one a! )ollow!+
o!"PHPExcel#$getP&ope&ties()#$set-&eato&(=Kaa&ten /alliau2=);
o!"PHPExcel#$getP&ope&ties()#$setLastKodi(ied/)(=Kaa&ten /alliau2=);
o!"PHPExcel#$getP&ope&ties()#$setTitle(=O((ice :;;< MLSM Test 'ocu,ent=);
o!"PHPExcel#$getP&ope&ties()#$setSu!"ect(=O((ice :;;< MLSM Test 'ocu,ent=);
o!"PHPExcel#$getP&ope&ties()#$set'esc&iption(=Test docu,ent (o& O((ice :;;< MLSM1
gene&ated using PHP classes.=);
o!"PHPExcel#$getP&ope&ties()#$setNe)2o&ds(=o((ice :;;< openx,l php=);
o!"PHPExcel#$getP&ope&ties()#$set-atego&)(=Test &esult (ile=);
5.5.2. ettin) a sprea!s"eet3s active s"eet
1%e )ollowin' line o) co(e !et! t%e active !%eet in(ex to t%e )ir!t !%eet+
o!"PHPExcel#$setActi%eSheet8ndex(;);
5.5.3. 0rite a !ate into a cell
5n Excel6 (ate! are !tore( a! numeric value! countin' t%e num4er o) (ay! elap!e( !ince 1900801801.
.or example6 t%e (ate A200<8128#1A i! repre!ente( a! #9<1#. Kou can veri)y t%i! in Micro!o)t I))ice
Excel 4y enterin' t%at (ate in a cell an( a)terwar(! c%an'in' t%e num4er )ormat to ANeneralA !o t%e
true numeric value i! reveale(.
?ritin' a (ate value in a cell con!i!t! o) 2 line! o) co(e. Belect t%e met%o( t%at !uit! you t%e 4e!t.
Here are !ome example!+
// K)SOL#li?e ti,esta,p ':;;0#3:#J3'
PHPExcel_-ell99set.alue/inde&( ne2 PHPExcel_-ell_Ad%anced.alue/inde&() );
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue(''3'1 ':;;0#3:#J3')
o!"PHPExcel#$getActi%eSheet()#$getSt)le(''3')#$get+u,!e&*o&,at()#
$set*o&,at-ode(PHPExcel_St)le_+u,!e&*o&,at99*ORKAT_'ATE_PPPPKK''SLASH)
// PHP#ti,e (Qnix ti,e)
ti,e 7 g,,?ti,e(;1;1;13:1J31:;;0); // int(3:J;R03R;;)
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue(''3'1
PHPExcel_Sha&ed_'ate99PHPToExcel(ti,e))
o!"PHPExcel#$getActi%eSheet()#$getSt)le(''3')#$get+u,!e&*o&,at()#
$set*o&,at-ode(PHPExcel_St)le_+u,!e&*o&,at99*ORKAT_'ATE_PPPPKK''SLASH)
// Excel#ti,e
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue(''3'1 JI03J)
o!"PHPExcel#$getActi%eSheet()#$getSt)le(''3')#$get+u,!e&*o&,at()#
$set*o&,at-ode(PHPExcel_St)le_+u,!e&*o&,at99*ORKAT_'ATE_PPPPKK''SLASH)
1%e a4ove met%o(! )or enterin' a (ate all yiel( t%e !ame re!ult. PHPExcel2Btyle2Dum4er.ormat
provi(e! a lot o) pre8(e)ine( (ate )ormat!.
/otes:
1. Bee !ection O@!in' value 4in(er! to )acilitate (ata entryO to learn more a4out t%e
$(vance(,alueBin(er u!e( in t%e )ir!t example.
2. 5n previou! ver!ion! o) PHPExcel up to an( inclu(in' 1.6.66 w%en a cell %a( a (ate8li-e
num4er )ormat co(e6 it wa! po!!i4le to enter a (ate (irectly u!in' an inte'er PHP8time
wit%out convertin' to Excel (ate )ormat. Btartin' wit% PHPExcel 1.6.7 t%i! i! no lon'er
!upporte(.
#. Excel can al!o operate in a 190484a!e( calen(ar /(e)ault )or wor-4oo-! !ave( on Mac0.
Dormally6 you (o not %ave to worry a4out t%i! w%en u!in' PHPExcel.
PHPExcel Developer Documentation 10
5.5.5. 0rite a ,ormula into a cell
5n!i(e t%e Excel )ile6 )ormula! are alway! !tore( a! t%ey woul( appear in an En'li!% ver!ion o)
Micro!o)t I))ice Excel. 1%i! i! irre'ar(le!! o) w%ic% lan'ua'e ver!ion o) Micro!o)t I))ice Excel you
may %ave u!e( to create t%e Excel )ile.
1%ere)ore6 w%en you write )ormula! wit% PHPExcel6 you mu!t alway! u!e En'li!% )ormula!. 1%e
)ollowin' rule! %ol(+
Decimal !eparator i! A.A /perio(0
.unction ar'ument !eparator i! A6A /comma0
Matrix row !eparator i! APA /!emicolon0
$lway! u!e En'li!% )unction name!
?%en t%e )inal wor-4oo- i! opene( 4y t%e u!er6 Micro!o)t I))ice Excel will ta-e care o) (i!playin'
t%e )ormula accor(in' t%e application! lan'ua'e. 1ran!lation i! ta-en care o) 4y t%e applicationL
1%e )ollowin' line o) co(e write! t%e )ormula Q;M5D/B2+&0R into t%e cell B<. Dote t%at t%e )ormula
mu!t !tart wit% Q;Q to ma-e PHPExcel reco'ni!e t%i! a! a )ormula.
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/0'1 '7K8+(/:9-C)');
$ cellF! )ormula can 4e rea( a'ain u!in' t%e )ollowin' line o) co(e+
o!"PHPExcel#$getActi%eSheet()#$get-ell('/0')#$get.alue();
5) you nee( t%e calculate( value o) a cell6 u!e t%e )ollowin' co(e. 1%i! i! )urt%er explaine( in 4.4.##
Bettin' t%e (e)ault column wi(t%.
o!"PHPExcel#$getActi%eSheet()#$get-ell('/0')#$get-alculated.alue();
5.5.7. Explicitl$ set a cell3s !atat$pe
Kou can !et a cellF! (atatype explicitly 4y u!in' t%e cellF! set.alueExplicit met%o(6 or t%e
set-ell.alueExplicit met%o( o) a wor-!%eet. HereF! an example+
o!"PHPExcel#$getActi%eSheet()#$get-ell('A3')#$set.alueExplicit(':C'1
PHPExcel_-ell_'ataT)pe99TPPE_+QKER8-);
5.5.8. C"an)e a cell into a clic%a'le 96L
Kou can ma-e a cell a clic-a4le @*E 4y !ettin' it! %yperlin- property+
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('E:R'1 '222.phpexcel.net');
o!"PHPExcel#$getActi%eSheet()#$get-ell('E:R')#$getH)pe&lin?()#
$setQ&l('http9//222.phpexcel.net');
5) you want to ma-e a %yperlin- to anot%er wor-!%eet7cell6 u!e t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('E:R'1 '222.phpexcel.net');
o!"PHPExcel#$getActi%eSheet()#$get-ell('E:R')#$getH)pe&lin?()#
$setQ&l(Ssheet9//'Sheetna,e'TA3U);
5.5... ettin) a (or%s"eet3s pa)e orientation an! si:e
Bettin' a wor-!%eetF! pa'e orientation an( !i3e can 4e (one u!in' t%e )ollowin' line! o) co(e+
o!"PHPExcel#$getActi%eSheet()#$getPageSetup()#
$setO&ientation(PHPExcel_>o&?sheet_PageSetup99OR8E+TAT8O+_LA+'S-APE);
o!"PHPExcel#$getActi%eSheet()#$getPageSetup()#
$setPape&SiVe(PHPExcel_>o&?sheet_PageSetup99PAPERS8WE_AG);
Dote t%at t%ere are a((itional pa'e !ettin'! availa4le. Plea!e re)er to t%e $P5 (ocumentation )or all
po!!i4le option!.
5.5.;. Center a pa)e "ori:ontall$/verticall$
1o center a pa'e %ori3ontally7vertically6 you can u!e t%e )ollowin' co(e+
PHPExcel Developer Documentation 11
o!"PHPExcel#$getActi%eSheet()#$getPageSetup()#$setHo&iVontal-ente&ed(t&ue);
o!"PHPExcel#$getActi%eSheet()#$getPageSetup()#$set.e&tical-ente&ed((alse);
5.5.<. ettin) t"e print "ea!er an! ,ooter o, a (or%s"eet
Bettin' a wor-!%eetF! print %ea(er an( )ooter can 4e (one u!in' t%e )ollowin' line! o) co(e+
o!"PHPExcel#$getActi%eSheet()#$getHeade&*oote&()#$setOddHeade&('X-XHPlease t&eat
this docu,ent as con(identialT');
o!"PHPExcel#$getActi%eSheet()#$getHeade&*oote&()#$setOdd*oote&('XLX/' .
o!"PHPExcel#$getP&ope&ties()#$getTitle() . 'XRPage XP o( X+');
Bu4!titution an( )ormattin' co(e! /!tartin' wit% =0 can 4e u!e( in!i(e %ea(er! an( )ooter!. 1%ere i!
no re"uire( or(er in w%ic% t%e!e co(e! mu!t appear.
1%e )ir!t occurrence o) t%e )ollowin' co(e! turn! t%e )ormattin' ID6 t%e !econ( occurrence turn! it
I.. a'ain+
Btri-et%rou'%
Buper!cript
Bu4!cript
Buper!cript an( !u4!cript cannot 4ot% 4e ID at !ame time. ?%ic%ever come! )ir!t win! an( t%e
ot%er i! i'nore(6 w%ile t%e )ir!t i! ID.
1%e )ollowin' co(e! are !upporte( 4y Excel2007+
&L
o(e )or Ole)t !ectionO /t%ere are t%ree %ea(er 7 )ooter location!6
Ole)tO6 OcenterO6 an( Ori'%tO0. ?%en
two or more occurrence! o) t%i! !ection mar-er exi!t6 t%e content!
)rom all mar-er! are concatenate(6 in t%e or(er o) appearance6 an(
place( into t%e le)t !ection.
&P
o(e )or Ocurrent pa'e SO
&N
o(e )or Ototal pa'e!O
&font size
o(e )or Otext )ont !i3eO6 w%ere )ont !i3e i! a )ont !i3e in point!.
&K
o(e )or Otext )ont colorO
*NB olor i! !peci)ie( a! **NNBB
1%eme olor i! !peci)e( a! 11BDD w%ere 11 i! t%e t%eme
color 5(6 B i! eit%er OTO or O8O o) t%e tint7!%a(e value6 DD i! t%e
tint7!%a(e value.
&S
o(e )or Otext !tri-et%rou'%O on 7 o))
&X
o(e )or Otext !uper !criptO on 7 o))
&Y
o(e )or Otext !u4!criptO on 7 o))
&C
o(e )or Ocenter !ectionO. ?%en two or more occurrence! o) t%i!
!ection mar-er exi!t6 t%e content! )rom all mar-er! are
concatenate(6 in t%e or(er o) appearance6 an( place( into t%e center
!ection.
&D
o(e )or O(ateO
&T
o(e )or OtimeO
&G
o(e )or Opicture a! 4ac-'roun(O
Plea!e ma-e !ure to a(( t%e ima'e to t%e %ea(er7)ooter+
o!"'&a2ing 7 ne2
PHPExcel_>o&?sheet_Heade&*oote&'&a2ing();
o!"'&a2ing#$set+a,e('PHPExcel logo');
o!"'&a2ing#$setPath('./i,ages/phpexcel_logo.gi(');
o!"'&a2ing#$setHeight(JR);
o!"PHPExcel#$getActi%eSheet()#$getHeade&*oote&()#
$add8,age(o!"'&a2ing1
PHPExcel_>o&?sheet_Heade&*oote&998KA4E_HEA'ER_LE*T);
&U
o(e )or Otext !in'le un(erlineO
&E
o(e )or O(ou4le un(erlineO
&R
o(e )or Ori'%t !ectionO. ?%en two or more occurrence! o) t%i!
PHPExcel Developer Documentation 12
!ection mar-er exi!t6 t%e content! )rom all mar-er! are
concatenate(6 in t%e or(er o) appearance6 an( place( into t%e ri'%t
!ection.
&Z
o(e )or Ot%i! wor-4oo-A! )ile pat%O
&F
o(e )or Ot%i! wor-4oo-A! )ile nameO
&A
o(e )or O!%eet ta4 nameO
&+
o(e )or a(( to pa'e S
&-
o(e )or !u4tract )rom pa'e S
&font n!"e#font t$%e
o(e )or Otext )ont nameO an( Otext )ont typeO6 w%ere )ont name an(
)ont type are !trin'! !peci)yin' t%e name an( type o) t%e )ont6
!eparate( 4y a comma. ?%en a %yp%en appear! in )ont name6 it
mean! Onone !peci)ie(O. Bot% o) )ont name an( )ont type can 4e
locali3e( value!.
&-#&ol'
o(e )or O4ol( )ont !tyleO
&&
o(e )or O4ol( )ont !tyleO
&-#Re(ul!)
o(e )or Ore'ular )ont !tyleO
&-#*t!li+
o(e )or Oitalic )ont !tyleO
&*
o(e )or Oitalic )ont !tyleO
&-#&ol' *t!li+
o(e )or O4ol( italic )ont !tyleO
&,
o(e )or Ooutline !tyleO
&-
o(e )or O!%a(ow !tyleO
5.5.1-. ettin) printin) 'rea%s on a ro( or column
1o !et a print 4rea-6 u!e t%e )ollowin' co(e6 w%ic% !et! a row 4rea- on row 10.
o!"PHPExcel#$getActi%eSheet()#$set/&ea?( 'A3;' 1 PHPExcel_>o&?sheet99/REAN_RO> );
1%e )ollowin' line o) co(e !et! a print 4rea- on column D+
o!"PHPExcel#$getActi%eSheet()#$set/&ea?( ''3;' 1
PHPExcel_>o&?sheet99/REAN_-OLQK+ );
5.5.11. "o(/"i!e )ri!lines ("en printin)
1o !%ow7%i(e 'ri(line! w%en printin'6 u!e t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$setSho24&idlines(t&ue);
5.5.12. ettin) ro(s/columns to repeat at top/le,t
PHPExcel can repeat !peci)ic row!7cell! at top7le)t o) a pa'e. 1%e )ollowin' co(e i! an example o)
%ow to repeat row 1 to & on eac% printe( pa'e o) a !peci)ic wor-!%eet+
o!"PHPExcel#$getActi%eSheet()#$getPageSetup()#
$setRo2sToRepeatAtTop/)Sta&tAndEnd(31 C);
5.5.13. peci,$ printin) area
1o !peci)y a wor-!%eetF! printi'n area6 u!e t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$getPageSetup()#$setP&intA&ea('A39EC');
5.5.15. #ormattin) a cell
$ cell can 4e )ormatte( wit% )ont6 4or(er6 )ill6 H !tyle in)ormation. .or example6 one can !et t%e
)ore'roun( colour o) a cell to re(6 ali'ne( to t%e ri'%t6 an( t%e 4or(er to 4lac- an( t%ic- 4or(er
!tyle. EetF! (o t%at on cell B2+
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get*ont()#$get-olo&()#
$setAR4/(PHPExcel_St)le_-olo&99-OLOR_RE');
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$getAlign,ent()#
$setHo&iVontal(PHPExcel_St)le_Align,ent99HOR8WO+TAL_R84HT);
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get/o&de&s()#$getTop()#
$set/o&de&St)le(PHPExcel_St)le_/o&de&99/OR'ER_TH8-N);
PHPExcel Developer Documentation 1#
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get/o&de&s()#$get/otto,()#
$set/o&de&St)le(PHPExcel_St)le_/o&de&99/OR'ER_TH8-N);
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get/o&de&s()#$getLe(t()#
$set/o&de&St)le(PHPExcel_St)le_/o&de&99/OR'ER_TH8-N);
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get/o&de&s()#$getRight()#
$set/o&de&St)le(PHPExcel_St)le_/o&de&99/OR'ER_TH8-N);
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get*ill()#
$set*illT)pe(PHPExcel_St)le_*ill99*8LL_SOL8');
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#$get*ill()#$getSta&t-olo&()#
$setAR4/('**;;;;;;');
1%ere i! al!o an alternative manner to !et !tyle! on a cell. 1%e )ollowin' co(e !et! a cellF! !tyle to
)ont 4ol(6 ali'nment ri'%t6 top 4or(er t%in an( a 'ra(ient )ill+
o!"PHPExcel#$getActi%eSheet()#$getSt)le('AJ')#$appl)*&o,A&&a)(
a&&a)(
'(ont' 7$ a&&a)(
'!old' 7$ t&ue
)1
'align,ent' 7$ a&&a)(
'ho&iVontal' 7$
PHPExcel_St)le_Align,ent99HOR8WO+TAL_R84HT1
)1
'!o&de&s' 7$ a&&a)(
'top' 7$ a&&a)(
'st)le' 7$ PHPExcel_St)le_/o&de&99/OR'ER_TH8+
)
)1
'(ill' 7$ a&&a)(
't)pe' 7$
PHPExcel_St)le_*ill99*8LL_4RA'8E+T_L8+EAR1
'&otation' 7$ I;1
'sta&tcolo&' 7$ a&&a)(
'a&g!' 7$ '**A;A;A;'
)1
'endcolo&' 7$ a&&a)(
'a&g!' 7$ '********'
)
)
)
);
5) you want to copy t%e !tyle to ot%er cell!6 you can (uplicate t%e entire !tyle o4Gect+
o!"PHPExcel#$getActi%eSheet()#$duplicateSt)le( o!"PHPExcel#$getActi%eSheet()#
$getSt)le('/:')1 '/J9/<' );
Kou can al!o (uplicate a !tyle array into a ran'e o) cell!. Plea!e note t%at t%i! met%o( (oe! not
!upport con(itional )ormattin'.
1%e previou!ly create( example can 4e applie( to a cell ran'e /$#+E#0 u!in' t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$duplicateSt)leA&&a)(
a&&a)(
'(ont' 7$ a&&a)(
'!old' 7$ t&ue
)1
'align,ent' 7$ a&&a)(
'ho&iVontal' 7$
PHPExcel_St)le_Align,ent99HOR8WO+TAL_R84HT1
)1
'!o&de&s' 7$ a&&a)(
'top' 7$ a&&a)(
'st)le' 7$ PHPExcel_St)le_/o&de&99/OR'ER_TH8+
)
)1
'(ill' 7$ a&&a)(
PHPExcel Developer Documentation 14
't)pe' 7$
PHPExcel_St)le_*ill99*8LL_4RA'8E+T_L8+EAR1
'&otation' 7$ I;1
'sta&tcolo&' 7$ a&&a)(
'a&g!' 7$ '**A;A;A;'
)1
'endcolo&' 7$ a&&a)(
'a&g!' 7$ '********'
)
)
)1
'AJ9EJ'
);
5.5.17. ettin) t"e !e,ault st$le o, a (or%s"eet
5t i! po!!4ile to !et t%e (e)ault !tyle o) a wor-!%eet wit% )ont6 4or(er6 )ill6 H !tyle in)ormation.
EetF! !et t%e (e)ault )ont to $rial !i3e <+
o!"PHPExcel#$getActi%eSheet()#$get'e(aultSt)le()#$get*ont()#$set+a,e('A&ial');
o!"PHPExcel#$getActi%eSheet()#$get'e(aultSt)le()#$get*ont()#$setSiVe(0);
5.5.18. Con!itional ,ormattin) a cell
$ cell can 4e )ormatte( con(itionally6 4a!e( on a !peci)ic rule. .or example6 one can !et t%e
)ore'roun( colour o) a cell to re( i) it! value i! 4elow 3ero6 an( to 'reen i) it! value i! 3ero or more.
Ine can !et a con(itional !tyle rule!et to a cell u!in' t%e )ollowin' co(e+
o!"-onditional3 7 ne2 PHPExcel_St)le_-onditional();
o!"-onditional3#$set-onditionT)pe(PHPExcel_St)le_-onditional99-O+'8T8O+_-ELL8S);
o!"-onditional3#$setOpe&ato&T)pe(PHPExcel_St)le_-onditional99OPERATOR_LESSTHA+);
o!"-onditional3#$add-ondition(';');
o!"-onditional3#$getSt)le()#$get*ont()#$get-olo&()#
$setAR4/(PHPExcel_St)le_-olo&99-OLOR_RE');
o!"-onditional3#$getSt)le()#$get*ont()#$set/old(t&ue);
o!"-onditional: 7 ne2 PHPExcel_St)le_-onditional();
o!"-onditional:#$set-onditionT)pe(PHPExcel_St)le_-onditional99-O+'8T8O+_-ELL8S);
o!"-onditional:#
$setOpe&ato&T)pe(PHPExcel_St)le_-onditional99OPERATOR_4REATERTHA+OREOQAL);
o!"-onditional:#$add-ondition(';');
o!"-onditional:#$getSt)le()#$get*ont()#$get-olo&()#
$setAR4/(PHPExcel_St)le_-olo&99-OLOR_4REE+);
o!"-onditional:#$getSt)le()#$get*ont()#$set/old(t&ue);
conditionalSt)les 7 o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#
$get-onditionalSt)les();
a&&a)_push(conditionalSt)les1 o!"-onditional3);
a&&a)_push(conditionalSt)les1 o!"-onditional:);
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/:')#
$set-onditionalSt)les(conditionalSt)les);
5) you want to copy t%e rule!et to ot%er cell!6 you can (uplicate t%e !tyle o4Gect+
o!"PHPExcel#$getActi%eSheet()#$duplicateSt)le( o!"PHPExcel#$getActi%eSheet()#
$getSt)le('/:')1 '/J9/<' );
5.5.1.. +!! a comment to a cell
1o a(( a comment to a cell6 u!e t%e )ollowin' co(e. 1%e example 4elow a((! a comment to cell
E11+
o!"PHPExcel#$getActi%eSheet()#$get-o,,ent('E33')#$setAutho&('PHPExcel');
o!"-o,,entRichText 7 o!"PHPExcel#$getActi%eSheet()#$get-o,,ent('E33')#$getText()#
$c&eateTextRun('PHPExcel9');
o!"-o,,entRichText#$get*ont()#$set/old(t&ue);
PHPExcel Developer Documentation 1&
o!"PHPExcel#$getActi%eSheet()#$get-o,,ent('E33')#$getText()#$c&eateTextRun(=@&@n=);
o!"PHPExcel#$getActi%eSheet()#$get-o,,ent('E33')#$getText()#$c&eateTextRun('Total
a,ount on the cu&&ent in%oice1 excluding .AT.');
5.5.1;. +ppl$ auto,ilter to a ran)e o, cells
1o apply an auto)ilter to a ran'e o) cell!6 u!e t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$setAuto*ilte&('A39-I');
Mae sure that "ou al&a"s include the complete filter ran*e0
Excel (oe! !upport !ettin' only t%e caption row6 4ut t%atA! not a 4e!t practi!e...
5.5.1<. ettin) securit$ on a sprea!s"eet
Excel o))er! # level! o) Q protectionR+ (ocument !ecurity6 !%eet !ecurity an( cell !ecurity.
8 Document !ecurity allow! you to !et a pa!!wor( on a complete !prea(!%eet6 allowin'
c%an'e! to 4e ma(e only w%en t%at pa!!wor( i! entere(.
8 ?or-!%eet !ecurity o))er! ot%er !ecurity option!+ you can (i!allow in!ertin' row! on a
!peci)ic !%eet6 (i!allow !ortin'6 H
8 ell !ecurity o))er! t%e option to loc-7unloc- a cell a! well a! !%ow7%i(e t%e internal
)ormula
$n example on !ettin' (ocument !ecurity+
o!"PHPExcel#$getSecu&it)()#$setLoc?>indo2s(t&ue);
o!"PHPExcel#$getSecu&it)()#$setLoc?St&uctu&e(t&ue);
o!"PHPExcel#$getSecu&it)()#$set>o&?!oo?Pass2o&d(=PHPExcel=);
$n example on !ettin' wor-!%eet !ecurity+
o!"PHPExcel#$getActi%eSheet()#$getP&otection()#$setPass2o&d('PHPExcel');
o!"PHPExcel#$getActi%eSheet()#$getP&otection()#$setSheet(t&ue);
o!"PHPExcel#$getActi%eSheet()#$getP&otection()#$setSo&t(t&ue);
o!"PHPExcel#$getActi%eSheet()#$getP&otection()#$set8nse&tRo2s(t&ue);
o!"PHPExcel#$getActi%eSheet()#$getP&otection()#$set*o&,at-ells(t&ue);
$n example on !ettin' cell !ecurity+
o!"PHPExcel#$getActi%eSheet()#$getSt)le('/3')#$getP&otection()#$setLoc?ed(
PHPExcel_St)le_P&otection99PROTE-T8O+_Q+PROTE-TE'
);
Mae sure "ou enable &orsheet protection if "ou need an" of the &orsheet protection features0
1%i! can 4e (one u!in' t%e )ollowin' co(e+ Uo4GPHPExcel8V'et$ctiveB%eet/08V'etProtection/08V!etB%eet/true0P
PHPExcel Developer Documentation 16
5.5.2-. ettin) !ata vali!ation on a cell
Data vali(ation i! a power)ul )eature o) Excel2007. 5t allow! to !peci)y an input )ilter on t%e (ata
t%at can 4e in!erte( in a !peci)ic cell. 1%i! )ilter can 4e a ran'e /i.e. value mu!t 4e 4etween 0 an(
1006 a li!t /i.e. value mu!t 4e pic-e( )rom a li!t06 H
1%e )ollowin' piece o) co(e only allow! num4er! 4etween 10 an( 20 to 4e entere( in cell B#+
o!".alidation 7 o!"PHPExcel#$getActi%eSheet()#$get-ell('/J')#$get'ata.alidation();
o!".alidation#$setT)pe( PHPExcel_-ell_'ata.alidation99TPPE_>HOLE );
o!".alidation#$setE&&o&St)le( PHPExcel_-ell_'ata.alidation99STPLE_STOP );
o!".alidation#$setAllo2/lan?(t&ue);
o!".alidation#$setSho28nputKessage(t&ue);
o!".alidation#$setSho2E&&o&Kessage(t&ue);
o!".alidation#$setE&&o&Title('8nput e&&o&');
o!".alidation#$setE&&o&('+u,!e& is not allo2edT');
o!".alidation#$setP&o,ptTitle('Allo2ed input');
o!".alidation#$setP&o,pt('Onl) nu,!e&s !et2een 3; and :; a&e allo2ed.');
o!".alidation#$set*o&,ula3(3;);
o!".alidation#$set*o&,ula:(:;);
o!"PHPExcel#$getActi%eSheet()#$get-ell('/J')#$set'ata.alidation(o!".alidation);
1%e )ollowin' piece o) co(e only allow! an item pic-e( )rom a li!t o) (ata to 4e entere( in cell B#+
o!".alidation 7 o!"PHPExcel#$getActi%eSheet()#$get-ell('/C')#$get'ata.alidation();
o!".alidation#$setT)pe( PHPExcel_-ell_'ata.alidation99TPPE_L8ST );
o!".alidation#$setE&&o&St)le( PHPExcel_-ell_'ata.alidation99STPLE_8+*ORKAT8O+ );
o!".alidation#$setAllo2/lan?((alse);
o!".alidation#$setSho28nputKessage(t&ue);
o!".alidation#$setSho2E&&o&Kessage(t&ue);
o!".alidation#$setSho2'&op'o2n(t&ue);
o!".alidation#$setE&&o&Title('8nput e&&o&');
o!".alidation#$setE&&o&('.alue is not in list.');
o!".alidation#$setP&o,ptTitle('Pic? (&o, list');
o!".alidation#$setP&o,pt('Please pic? a %alue (&o, the d&op#do2n list.');
o!".alidation#$set*o&,ula3('=8te, A18te, /18te, -=');
o!"PHPExcel#$getActi%eSheet()#$get-ell('/C')#$set'ata.alidation(o!".alidation);
?%en u!in' a (ata vali(ation li!t6 ma-e !ure you put t%e li!t 4etween Q an( Q an( t%at you !plit t%e
item! wit% a comma /60.
5) you nee( (ata vali(ation on multiple cell!6 one can clone t%e rule!et+
o!"PHPExcel#$getActi%eSheet()#$get-ell('/0')#$set'ata.alidation(clone
o!".alidation);
5.5.21. ettin) a column3s (i!t"
$ columnF! wi(t% can 4e !et u!in' t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension(''')#$set>idth(3:);
5) you want PHPExcel to per)orm an automatic wi(t% calculation6 u!e t%e )ollowin' co(e. PHPExcel
will approximate t%e column wit% to t%e wi(t% o) t%e wi(e!t column value.
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension('/')#$setAutoSiVe(t&ue);
5.5.22. "o(/"i!e a column
1o !et a wor-!%eetF! column vi!i4ility6 you can u!e t%e )ollowin' co(e. 1%e )ir!t line explicitly !%ow!
t%e column 6 t%e !econ( line %i(e! column D.
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension('-')#$set.isi!le(t&ue);
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension(''')#$set.isi!le((alse);
5.5.23. =roup/outline a column
1o 'roup7outline a column6 you can u!e t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension('E')#$setOutlineLe%el(3);
PHPExcel Developer Documentation 17
Kou can al!o collap!e t%e column. Dote t%at you !%oul( al!o !et t%e column invi!i4le6 ot%erwi!e t%e
collap!e will not 4e vi!i4le in Excel 2007.
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension('E')#$set-ollapsed(t&ue);
o!"PHPExcel#$getActi%eSheet()#$get-olu,n'i,ension('E')#$set.isi!le((alse);
Plea!e re)er to t%e part Q'roup7outline a rowR )or a complete example on collap!in'.
Kou can in!truct PHPExcel to a(( a !ummary to t%e ri'%t /(e)ault06 or to t%e le)t. 1%e )ollowin'
co(e a((! t%e !ummary to t%e le)t+
o!"PHPExcel#$getActi%eSheet()#$setSho2Su,,a&)Right((alse);
5.5.25. ettin) a ro(3s "ei)"t
$ rowF! %ei'%t can 4e !et u!in' t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension('3;')#$setRo2Height(3;;);
5.5.27. "o(/"i!e a ro(
1o !et a wor-!%eetF! row vi!i4ility6 you can u!e t%e )ollowin' co(e. 1%e )ollowin' example %i(e! row
num4er 10.
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension('3;')#$set.isi!le((alse);
5.5.28. =roup/outline a ro(
1o 'roup7outline a row6 you can u!e t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension('C')#$setOutlineLe%el(3);
Kou can al!o collap!e t%e row. Dote t%at you !%oul( al!o !et t%e row invi!i4le6 ot%erwi!e t%e
collap!e will not 4e vi!i4le in Excel 2007.
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension('C')#$set-ollapsed(t&ue);
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension('C')#$set.isi!le((alse);
HereF! an example w%ic% collap!e! row! &0 to <0+
(o& (i 7 C3; i 57 0;; iDD) A
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('A' . i1 =*+a,e i=);
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/' . i1 =L+a,e i=);
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('-' . i1 =Phone+o i=);
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue(''' . i1 =*ax+o i=);
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('E' . i1 t&ue);
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension(i)#$setOutlineLe%el(3);
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension(i)#$set.isi!le((alse);
B
o!"PHPExcel#$getActi%eSheet()#$getRo2'i,ension(03)#$set-ollapsed(t&ue);
Kou can in!truct PHPExcel to a(( a !ummary 4elow t%e collap!i4le row! /(e)ault06 or a4ove. 1%e
)ollowin' co(e a((! t%e !ummary a4ove+
o!"PHPExcel#$getActi%eSheet()#$setSho2Su,,a&)/elo2((alse);
5.5.2.. Mer)e/unmer)e cells
5) you %ave a 4i' piece o) (ata you want to (i!play in a wor-!%eet6 you can mer'e two or more cell!
to'et%er6 to 4ecome one cell. 1%i! can 4e (one u!in' t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$,e&ge-ells('A309E::');
*emovin' a mer'e can 4e (one u!in' t%e un,e&ge-ells met%o(+
o!"PHPExcel#$getActi%eSheet()#$un,e&ge-ells('A309E::Y);
PHPExcel Developer Documentation 1<
5.5.2;. >nsertin) ro(s/columns
Kou can in!ert7remove row!7column! at a !peci)ic po!ition. 1%e )ollowin' co(e in!ert! 2 new row!6
ri'%t 4e)ore row 7+
o!"PHPExcel#$getActi%eSheet()#$inse&t+e2Ro2/e(o&e(<1 :);
5.5.2<. +!! a !ra(in) to a (or%s"eet
$ (rawin' i! alway! repre!ente( a! a !eparate o4Gect6 w%ic% can 4e a((e( to a wor-!%eet.
1%ere)ore6 you mu!t )ir!t in!tantiate a new PHPExcel_>o&?sheet_'&a2ing6 an( a!!i'n it! propertie!
a meanin')ul value+
o!"'&a2ing 7 ne2 PHPExcel_>o&?sheet_'&a2ing();
o!"'&a2ing#$set+a,e('Logo');
o!"'&a2ing#$set'esc&iption('Logo');
o!"'&a2ing#$setPath('./i,ages/o((icelogo."pg');
o!"'&a2ing#$setHeight(JR);
1o a(( t%e a4ove (rawin' to t%e wor-!%eet6 u!e t%e )ollowin' !nippet o) co(e. PHPExcel create!
t%e lin- 4etween t%e (rawin' an( t%e wor-!%eet+
o!"'&a2ing#$set>o&?sheet(o!"PHPExcel#$getActi%eSheet());
Kou can !et numerou! propertie! on a (rawin'6 %ere are !ome example!+
o!"'&a2ing#$set+a,e('Paid');
o!"'&a2ing#$set'esc&iption('Paid');
o!"'&a2ing#$setPath('./i,ages/paid.png');
o!"'&a2ing#$set-oo&dinates('/3C');
o!"'&a2ing#$setO((setM(33;);
o!"'&a2ing#$setRotation(:C);
o!"'&a2ing#$getShado2()#$set.isi!le(t&ue);
o!"'&a2ing#$getShado2()#$set'i&ection(GC);
5.5.3-. +!! ric" text to a cell
$((in' ric% text to a cell can 4e (one u!in' PHPExcel_RichText in!tance!. HereF! an example6
w%ic% create! t%e )ollowin' ric% text !trin'+
1%i! invoice i! payable within thirty days after the end of the month unle!! !peci)ie( ot%erwi!e
on t%e invoice.
o!"RichText 7 ne2 PHPExcel_RichText( o!"PHPExcel#$getActi%eSheet()#
$get-ell('A30') );
o!"RichText#$c&eateText('This in%oice is ');
o!"Pa)a!le 7 o!"RichText#$c&eateTextRun('pa)a!le 2ithin thi&t) da)s a(te& the end
o( the ,onth');
o!"Pa)a!le#$get*ont()#$set/old(t&ue);
o!"Pa)a!le#$get*ont()#$set8talic(t&ue);
o!"Pa)a!le#$get*ont()#$set-olo&( ne2
PHPExcel_St)le_-olo&( PHPExcel_St)le_-olo&99-OLOR_'ARN4REE+ ) );
o!"RichText#$c&eateText('1 unless speci(ied othe&2ise on the in%oice.');
5.5.31. De,ine a name! ran)e
PHPExcel !upport! t%e (e)inition o) name( ran'e!. 1%e!e can 4e (e)ine( u!in' t%e )ollowin' co(e+
// Add so,e data
o!"PHPExcel#$setActi%eSheet8ndex(;);
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('A3'1 '*i&stna,e9');
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('A:'1 'Lastna,e9');
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/3'1 'Kaa&ten');
o!"PHPExcel#$getActi%eSheet()#$set-ell.alue('/:'1 '/alliau2');
// 'e(ine na,ed &anges
PHPExcel Developer Documentation 19
o!"PHPExcel#$add+a,edRange( ne2 PHPExcel_+a,edRange('Pe&son*+'1 o!"PHPExcel#
$getActi%eSheet()1 '/3') );
o!"PHPExcel#$add+a,edRange( ne2 PHPExcel_+a,edRange('Pe&sonL+'1 o!"PHPExcel#
$getActi%eSheet()1 '/:') );
Iptionally6 a )ourt% parameter can 4e pa!!e( (e)inin' t%e name( ran'e local /i.e. only u!a4le on
t%e current wor-!%eet0. Dame( ran'e! are 'lo4al 4y (e)ault.
5.5.32. 6e!irect output to a client3s (e' 'ro(ser
Bometime!6 one really want! to output a )ile to a clientF! 4row!er6 e!pecially w%en creatin'
!prea(!%eet! on8t%e8)ly. 1%ere are !ome ea!y !tep! t%at can 4e )ollowe( to (o t%i!+
1. reate your PHPExcel !prea(!%eet
2. Iutput H11P %ea(er! )or t%e type o) (ocument you wi!% to output
#. @!e t%e PHPExcel2?riter2W o) your c%oice6 an( !ave to Qp%p+77outputR
PHPExcel2?riter2Excel2007 u!e! temporary !tora'e w%en writin' to p%p+77output. By (e)ault6
temporary )ile! are !tore( in t%e !criptF! wor-in' (irectory. ?%en t%ere i! no acce!!6 it )all! 4ac- to
t%e operatin' !y!temF! temporary )ile! location.
,his ma" not be safe for unauthori1ed .ie&in*0
Depen(in' on t%e con)i'uration o) your operatin' !y!tem6 temporary !tora'e can 4e rea( 4y anyone u!in' t%e
!ame temporary !tora'e )ol(er. ?%en con)i(entiality o) your (ocument i! nee(e(6 it i! recommen(e( not to
u!e p%p+77output.
H&&P "ea!ers
Example o) a !cript re(irectin' an Excel 2007 )ile to t%e clientA! 4row!er+
56php
/E He&e the&e 2ill !e so,e code 2he&e )ou c&eate o!"PHPExcel E/
// &edi&ect output to client !&o2se&
heade&('-ontent#T)pe9 application/%nd.openx,l(o&,ats#
o((icedocu,ent.sp&eadsheet,l.sheet');
heade&('-ontent#'isposition9 attach,ent;(ilena,e7=,)(ile.xlsx=');
heade&('-ache#-ont&ol9 ,ax#age7;');
o!">&ite& 7 PHPExcel_8O*acto&)99c&eate>&ite&(o!"PHPExcel1 'Excel:;;<');
o!">&ite&#$sa%e('php9//output');
6$
Example o) a !cript re(irectin' an Excel& )ile to t%e clientA! 4row!er+
56php
/E He&e the&e 2ill !e so,e code 2he&e )ou c&eate o!"PHPExcel E/
// &edi&ect output to client !&o2se&
heade&('-ontent#T)pe9 application/%nd.,s#excel');
heade&('-ontent#'isposition9 attach,ent;(ilena,e7=,)(ile.xls=');
heade&('-ache#-ont&ol9 ,ax#age7;');
o!">&ite& 7 PHPExcel_8O*acto&)99c&eate>&ite&(o!"PHPExcel1 'ExcelC');
o!">&ite&#$sa%e('php9//output');
6$
aution+
Ma-e !ure not to inclu(e any ec%o !tatement! or output any ot%er content! t%an t%e Excel
)ile. 1%ere !%oul( 4e no w%ite!pace 4e)ore t%e openin' X:p%p ta' an( at mo!t one line
4rea- a)ter t%e clo!in' :V ta' /w%ic% can al!o 4e omitte( to avoi( pro4lem!0.
Ma-e !ure t%at your !cript i! !ave( wit%out a BIM /Byte8or(er mar-0. /Becau!e t%i! count!
a! ec%oin' output0
Bame t%in'! apply to all inclu(e( )ile!
PHPExcel Developer Documentation 20
.ailin' to )ollow t%e a4ove 'ui(eline! may re!ult in corrupt Excel )ile! arrivin' at t%e client 4row!er6
an(7or t%at %ea(er! cannot 4e !et 4y PHP /re!ultin' in warnin' me!!a'e!0.
5.5.33. ettin) t"e !e,ault column (i!t"
De)ault column wi(t% can 4e !et u!in' t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$get'e(ault-olu,n'i,ension()#$set>idth(3:);
5.5.35. ettin) t"e !e,ault ro( "ei)"t
De)ault row %ei'%t can 4e !et u!in' t%e )ollowin' co(e+
o!"PHPExcel#$getActi%eSheet()#$get'e(aultRo2'i,ension()#$setRo2Height(3C);
5.5.37. +!! a =D !ra(in) to a (or%s"eet
1%ere mi'%t 4e a !ituation w%ere you want to 'enerate an in8memory ima'e u!in' ND an( a(( it to
a PHPExcel wor-!%eet wit%out )ir!t %avin' to !ave t%i! )ile to a temporary location.
HereF! an example w%ic% 'enerate! an ima'e in memory an( a((! it to t%e active wor-!%eet+
// 4ene&ate an i,age
gd8,age 7 Zi,agec&eatet&uecolo&(3:;1 :;) o& die('-annot 8nitialiVe ne2 4' i,age
st&ea,');
text-olo& 7 i,agecolo&allocate(gd8,age1 :CC1 :CC1 :CC);
i,agest&ing(gd8,age1 31 C1 C1 '-&eated 2ith PHPExcel'1 text-olo&);
// Add a d&a2ing to the 2o&?sheet
o!"'&a2ing 7 ne2 PHPExcel_>o&?sheet_Ke,o&)'&a2ing();
o!"'&a2ing#$set+a,e('Sa,ple i,age');
o!"'&a2ing#$set'esc&iption('Sa,ple i,age');
o!"'&a2ing#$set8,ageResou&ce(gd8,age);
o!"'&a2ing#$setRende&ing*unction(PHPExcel_>o&?sheet_Ke,o&)'&a2ing99RE+'ER8+4_[PE4);
o!"'&a2ing#$setKi,eT)pe(PHPExcel_>o&?sheet_Ke,o&)'&a2ing99K8KETPPE_'E*AQLT);
o!"'&a2ing#$setHeight(JR);
o!"'&a2ing#$set>o&?sheet(o!"PHPExcel#$getActi%eSheet());
5.5.38. ettin) (or%s"eet :oom level
1o !et a wor-!%eetF! 3oom level6 t%e )ollowin' co(e can 4e u!e(+
o!"PHPExcel#$getActi%eSheet()#$getSheet.ie2()#$setWoo,Scale(<C);
Dote t%at 3oom level !%oul( 4e in ran'e 10 Y 400.
PHPExcel Developer Documentation 21
7. Per,ormin) ,ormula calculations
'.1. Usin& the !"#cel calculation en&ine
$! PHPExcel repre!ent! an in8memory !prea(!%eet6 it al!o o))er! )ormula calculation capa4ilitie!. $
cell can 4e o) a value type /containin' a num4er or text06 or a )ormula type /containin' a )ormula
w%ic% can 4e evaluate(0. .or example6 t%e )ormula O;B@M/$1+$100O evaluate! to t%e !um o) value!
in $16 $26 ...6 $10.
1o calculate a )ormula6 you can call t%e cell containin' t%e )ormulaF! met%o(
get-alculated.alue()6 )or example+
o!"PHPExcel#$getActi%eSheet()#$get-ell('E33')#$get-alculated.alue();
5) you write t%e )ollowin' line o) co(e in t%e invoice (emo inclu(e( wit% PHPExcel6 it evaluate! to
t%e value O64O+
$not%er nice )eature o) PHPExcelA! )ormula par!er6 i! t%at it can automatically a(Gu!t a )ormula
w%en in!ertin'7removin' row!7column!. HereA! an example+
Kou !ee t%at t%e )ormula containe( in cell E11 i! OB@M/E4+E90O. Dow6 w%en 5 write t%e )ollowin' line
o) co(e6 two new pro(uct line! are a((e(+
o!"PHPExcel#$getActi%eSheet()#$inse&t+e2Ro2/e(o&e(<1 :);
PHPExcel Developer Documentation 22
Di( you notice: 1%e )ormula in t%e )ormer cell E11 /now E1#6 a! 5 in!erte( 2 new row!06 c%an'e( to
OB@M/E4+E110O. $l!o6 t%e in!erte( cell! (uplicate !tyle in)ormation o) t%e previou! cell6 Gu!t li-e
ExcelA! 4e%aviour. Dote t%at you can 4ot% in!ert row! an( column!.
'.2. (nown limitations
1%ere are !ome -nown limitation! to t%e PHPExcel calculation en'ine. Mo!t o) t%em are (ue to t%e
)act t%at an Excel )ormula i! converte( into PHP co(e 4e)ore 4ein' execute(. 1%i! mean! t%at Excel
)ormula calculation i! !u4Gect to PHPF! lan'ua'e c%aracteri!tic!.
7.2.1. Operator prece!ence
5n Excel ATA win! over A=A6 Gu!t li-e AWA win! over ATA in or(inary al'e4ra. 1%e )ormer rule i! not w%at one
)in(! u!in' t%e calculation en'ine !%ippe( wit% PHPExcel.
*e)erence )or operator prece(ence in Excel+
%ttp+77!upport.micro!o)t.com7-472&1<9
*e)erence )or operator prece(ence in PHP+
%ttp+77www.p%p.net7operator!
7.2.2. X ? @ APo(erB
Power! in a )ormula entere( li-e A2 Z #A (onAt wor-. @!in' t%e PI?E*/0 )unction (oe! wor-+
A;PI?E*/2P #0A. 1%i! i! (ue to t%e )act t%at PHP treat! Z a! 4itwi!e JI* in!tea( o) power.
*e)erence )or )ormula! in Excel+
%ttp+77o))ice.micro!o)t.com7en8u!7excel7HP0#0&61#910##.a!px
*e)erence )or operator! in PHP+
%ttp+77www.p%p.net7operator!
7.2.3. #ormulas involvin) num'ers an! text
.ormula! involvin' num4er! an( text may pro(uce unexpecte( re!ult! or even unrea(a4le )ile
content!. .or example6 t%e )ormula A;#TOHello OA i! expecte( to pro(uce an error in Excel /S,$E@EL0.
Due to t%e )act t%at PHP convert! QHelloR to a numeric value /3ero06 t%e re!ult o) t%i! )ormula i!
evaluate( a! # in!tea( o) evaluatin' a! an error. 1%i! al!o cau!e! t%e Excel (ocument 4ein'
'enerate( a! containin' unrea(a4le content.
*e)erence )or t%i! 4e%aviour in PHP+
%ttp+774e.p%p.net7manual7en7lan'ua'e.type!.!trin'.p%pSlan'ua'e.type!.!trin'.conver!ion
PHPExcel Developer Documentation 2#
8. 6ea!in) an! (ritin) to ,ile
$! you alrea(y -now )rom part #.# *ea(er! an( writer!6 rea(in' an( writin' to a per!i!te( !tora'e i!
not po!!i4le u!in' t%e 4a!e PHPExcel cla!!e!. .or t%i! purpo!e6 PHPExcel provi(e! rea(er! an(
writer!6 w%ic% are implementation! o) PHPExcel2?riter25*ea(er an( PHPExcel2?riter25?riter.
).1. !"#cel*I+,actory
1%e PHPExcel $P5 o))er! multiple met%o(! to create a PHPExcel2?riter25*ea(er or
PHPExcel2?riter25?riter in!tance+
Direct creation
,ia PHPExcel25I.actory
$ll example! un(erneat% (emon!trate t%e (irect creation met%o(. Dote t%at you can al!o u!e t%e
PHPExcel25I.actory cla!! to (o t%i!.
8.1.1. Creatin) PHPExcel/6ea!er/>6ea!er usin) PHPExcel/>O#actor$
1%ere are 2 met%o(! )or rea(in' in a )ile into PHPExcel+ u!in' automatic )ile type re!olvin' or
explicitly.
$utomatic )ile type re!olvin' c%ec-! t%e (i))erent PHPExcel2*ea(er25*ea(er (i!tri4ute( wit%
PHPExcel. 5) one o) t%em can loa( t%e !peci)ie( )ile name6 t%e )ile i! loa(e( u!in' t%at
PHPExcel2*ea(er25*ea(er. Explicit mo(e re"uire! you to !peci)y w%ic% PHPExcel2*ea(er25*ea(er
!%oul( 4e u!e(.
Kou can create a PHPExcel2*ea(er25*ea(er in!tance u!in' PHPExcel25I.actory in automatic )ile
type re!olvin' mo(e u!in' t%e )ollowin' co(e !ample+
o!"PHPExcel 7 PHPExcel_8O*acto&)#$load(=;C(eatu&ede,o.xlsx=);
Kou can create a PHPExcel2*ea(er25*ea(er in!tance u!in' PHPExcel25I.actory in explicit mo(e
u!in' t%e )ollowin' co(e !ample+
o!"Reade& 7 PHPExcel_8O*acto&)99c&eateReade&(=Excel:;;<=);
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xlsx=);
Dote t%at automatic type re!olvin' mo(e i! !li'%tly !lower t%an explicit mo(e.
8.1.2. Creatin) PHPExcel/0riter/>0riter usin) PHPExcel/>O#actor$
Kou can create a PHPExcel2?riter25writer in!tance u!in' PHPExcel25I.actory+
o!">&ite& 7 PHPExcel_8O*acto&)99c&eate>&ite&(o!"PHPExcel1 =Excel:;;<=);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xlsx=);
).2. "#cel 2--. /Spreadsheet012 file format
Excel2007 )ile )ormat i! t%e main )ile )ormat o) PHPExcel. 5t allow! outputtin' t%e in8memory
!prea(!%eet to a .xl!x )ile.
8.2.1. PHPExcel/6ea!er/Excel2--.
6ea!in) a sprea!s"eet
Kou can rea( a .xl!x )ile u!in' t%e )ollowin' co(e+
o!"Reade& 7 ne2 PHPExcel_Reade&_Excel:;;<();
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xlsx=);
PHPExcel Developer Documentation 24
6ea! !ata onl$
Kou can !et an t%e option !et*ea(DataInly on t%e rea(er6 to in!truct t%e rea(er to i'nore !tylin'6
(ata vali(ation6 H an( Gu!t rea( cell (ata+
o!"Reade& 7 ne2 PHPExcel_Reade&_Excel:;;<();
o!"Reade&#$setRead'ataOnl)(t&ue);
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xlsx=);
6ea! speci,ic s"eets onl$
Kou can !et an t%e option !etEoa(B%eet!Inly on t%e rea(er6 to in!truct t%e rea(er to only loa( t%e
!%eet! wit% a 'iven name+
o!"Reade& 7 ne2 PHPExcel_Reade&_Excel:;;<();
o!"Reade&#$setLoadSheetsOnl)( a&&a)(=Sheet 3=1 =K) special sheet=) );
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xlsx=);
6ea! speci,ic cells onl$
Kou can !et an t%e option !et*ea(.ilter on t%e rea(er6 to in!truct t%e rea(er to only loa( t%e cell!
w%ic% matc% a 'iven rule. $ rea( )ilter can 4e any cla!! w%ic% implement!
PHPExcel2*ea(er25*ea(.ilter. By (e)ault6 all cell! are rea( u!in' t%e
PHPExcel2*ea(er2De)ault*ea(.ilter.
1%e )ollowin' co(e will only rea( row 1 an( row! 20 Y #0 o) any !%eet in t%e Excel )ile+
class K)Read*ilte& i,ple,ents PHPExcel_Reade&_8Read*ilte&
A
pu!lic (unction &ead-ell(colu,n1 &o21 2o&?sheet+a,e 7 '') A
// Read title &o2 and &o2s :; # J;
i( (&o2 77 3 \\ (&o2 $7 :; XX &o2 57 J;)) A
&etu&n t&ue;
B
&etu&n (alse;
B
B
o!"Reade& 7 ne2 PHPExcel_Reade&_Excel:;;<();
o!"Reade&#$setRead*ilte&( ne2 K)Read*ilte&() );
o!"PHPExcel 7 o!"Reade&#$load(=;Rla&gescale.xlsx=);
8.2.2. PHPExcel/0riter/Excel2--.
0ritin) a sprea!s"eet
Kou can write a .xl!x )ile u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_Excel:;;<(o!"PHPExcel);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xlsx=);
#ormula preCcalculation
By (e)ault6 t%i! writer pre8calculate! all )ormula! in t%e !prea(!%eet. 1%i! can 4e !low on lar'e
!prea(!%eet!6 an( may4e even unwante(. Kou can %owever (i!a4le )ormula pre8calculation+
o!">&ite& 7 ne2 PHPExcel_>&ite&_Excel:;;<(o!"PHPExcel);
o!">&ite&#$setP&e-alculate*o&,ulas((alse);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xlsx=);
O,,ice 2--3 compati'ilit$ pac%
Becau!e o) a 4u' in t%e I))ice200# compati4ility pac-6 t%ere can 4e !ome !mall i!!ue! w%en openin'
Excel2007 !prea(!%eet! /mo!tly relate( to )ormula calculation0. Kou can ena4le I))ice200#
compati4ility wit% t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_Excel:;;<(o!"PHPExcel);
o!">&ite&#$setO((ice:;;J-o,pati!ilit)(t&ue);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xlsx=);
PHPExcel Developer Documentation 2&
Office'((2 compatibilit" should onl" be used &hen needed
I))ice200# compati4ility option !%oul( only 4e u!e( w%en nee(e(. 1%i! option (i!a4le! !everal I))ice2007 )ile
)ormat option!6 re!ultin' in a lower8)eature( I))ice2007 !prea(!%eet w%en t%i! option i! u!e(.
&emporar$ stora)e
?%en wor-in' wit% lar'e !prea(!%eet!6 a u!e)ul )eature i! (i!- cac%in'. ?%en (i!- cac%in' i!
ena4le(6 t%i! writer u!e! )ile 4a!e( temporary !tora'e in!tea( o) memory 4a!e( temporary !tora'e
w%erever po!!i4le. By (e)ault6 temporary )ile! are !tore( in t%e !criptF! wor-in' (irectory. ?%en
t%ere i! no acce!!6 it )all! 4ac- to t%e operatin' !y!temF! temporary )ile! location. Dote t%at (i!-
cac%in' (oe! not !pee( up writin'6 it only lower! memory u!a'e w%en nee(e(.
,emporar" stora*e ma" not be safe for unauthori1ed .ie&in*0
Depen(in' on t%e con)i'uration o) your operatin' !y!tem6 temporary !tora'e can 4e rea( 4y anyone u!in' t%e
!ame temporary !tora'e )ol(er. ?%en con)i(entiality o) your (ocument i! nee(e(6 it i! recommen(e( not to
u!e (i!- cac%in'.
Kou can ena4le (i!- cac%in' u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_Excel:;;<(o!"PHPExcel);
o!">&ite&#$setQse'is?-aching(t&ue);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xlsx=);
Iptionally6 t%e location o) t%e (i!- cac%e )ol(er can 4e !peci)ie( u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_Excel:;;<(o!"PHPExcel);
o!">&ite&#$setQse'is?-aching(t&ue1 =/ho,e/phpexcel/dis?cache=);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xlsx=);
).3. Seriali3ed file format
Beriali3e( )ile )ormat i! a manner o) !torin' a PHPExcel !prea(!%eet to (i!-6 creatin' a )ile
containin' a !eriali3e( PHPExcel in!tance. 5t o))er! a )a!t an( ea!y way to !tore an( rea( a
!prea(!%eet.
3eriali1ed file format should not be used as a persistent stora*e method0
Bince !eriali3e( )ile! may not 4e compati4le trou'% (i))erent PHPExcel ver!ion!6 it i! not a 'oo( i(ea to u!e t%i!
a! a per!i!tent !tora'e met%o(. 1%in- o) t%e !eriali3e( )ile )ormat a! a temporary !tora'e6 )or example
4etween 2 4atc% !cript! t%at (epen( on eac% ot%erF! output.
8.3.1. PHPExcel/6ea!er/eriali:e!
6ea!in) a sprea!s"eet
Kou can rea( a .p%pxl )ile u!in' t%e )ollowin' co(e+
o!"Reade& 7 ne2 PHPExcel_Reade&_Se&ialiVed();
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.phpxl=);
8.3.2. PHPExcel/0riter/eriali:e!
0ritin) a sprea!s"eet
Kou can write a .p%pxl )ile u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_Se&ialiVed(o!"PHPExcel);
o!">&ite&#$sa%e(=;C(eatu&ede,o.phpxl=);
).4. "#cel ' /4I,,2 file format
Excel& )ile )ormat i! t%e ol( Excel )ile )ormat6 implemente( in PHPExcel to provi(e a uni)orm
manner to create 4ot% .xl!x an( .xl! )ile!. 5t i! 4a!ically a mo(i)ie( ver!ion o) PE$*
Bprea(!%eet2Excel2?riter6 an( %a! t%e !ame limitation! an( )eature! a! t%e PE$* li4rary.
PHPExcel Developer Documentation 26
Excel& )ile )ormat will not 4e (evelope( any )urt%er6 it Gu!t provi(e! an a((itional )ile )ormat )or
PHPExcel.
Excel4 567FF8 limitations
Plea!e note t%at B5.. )ile )ormat %a! !ome limit! re'ar(in' to !tylin' cell! an( %an(lin' lar'e !prea(!%eet! via
PHP.
8.5.1. PHPExcel/6ea!er/Excel7
6ea!in) a sprea!s"eet
Kou can rea( a .xl! )ile u!in' t%e )ollowin' co(e+
o!"Reade& 7 ne2 PHPExcel_Reade&_ExcelC();
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xls=);
6ea! !ata onl$
Kou can !et an t%e option !et*ea(DataInly on t%e rea(er6 to in!truct t%e rea(er to i'nore !tylin'6
(ata vali(ation6 H an( Gu!t rea( cell (ata+
o!"Reade& 7 ne2 PHPExcel_Reade&_ExcelC();
o!"Reade&#$setRead'ataOnl)(t&ue);
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xls=);
6ea! speci,ic s"eets onl$
Kou can !et an t%e option !etEoa(B%eet!Inly on t%e rea(er6 to in!truct t%e rea(er to only loa( t%e
!%eet! wit% a 'iven name+
o!"Reade& 7 ne2 PHPExcel_Reade&_ExcelC();
o!"Reade&#$setLoadSheetsOnl)( a&&a)(=Sheet 3=1 =K) special sheet=) );
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.xls=);
6ea! speci,ic cells onl$
Kou can !et an t%e option !et*ea(.ilter on t%e rea(er6 to in!truct t%e rea(er to only loa( t%e cell!
w%ic% matc% a 'iven rule. $ rea( )ilter can 4e any cla!! w%ic% implement!
PHPExcel2*ea(er25*ea(.ilter. By (e)ault6 all cell! are rea( u!in' t%e
PHPExcel2*ea(er2De)ault*ea(.ilter.
1%e )ollowin' co(e will only rea( row 1 an( row! 20 Y #0 o) any !%eet in t%e Excel )ile+
class K)Read*ilte& i,ple,ents PHPExcel_Reade&_8Read*ilte&
A
pu!lic (unction &ead-ell(colu,n1 &o21 2o&?sheet+a,e 7 '') A
// Read title &o2 and &o2s :; # J;
i( (&o2 77 3 \\ (&o2 $7 :; XX &o2 57 J;)) A
&etu&n t&ue;
B
&etu&n (alse;
B
B
o!"Reade& 7 ne2 PHPExcel_Reade&_ExcelC();
o!"Reade&#$setRead*ilte&( ne2 K)Read*ilte&() );
o!"PHPExcel 7 o!"Reade&#$load(=;Rla&gescale.xls=);
8.5.2. PHPExcel/0riter/Excel7
0ritin) a sprea!s"eet
Kou can write a .xl! )ile u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_ExcelC(o!"PHPExcel);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xls=);
PHPExcel Developer Documentation 27
&emporar$ stora)e
5nternally6 Excel& writer !tore! temporary )ile! on (i!-. ?%en your !erver a(mini!trator (i!a4le( )ile
acce!! on t%e operatin' !y!temF! temporary (irectory6 you can overri(e t%e (e)ault 4y u!in' t%e
)ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_ExcelC(o!"PHPExcel);
o!">&ite&#$setTe,p'i&(=-9/te,p/=);
o!">&ite&#$sa%e(=;C(eatu&ede,o.xls=);
).'. 5S6 /5omma Separated 6alues2
B, /omma Beparate( ,alue!0 are o)ten u!e( a! an import7export )ile )ormat wit% ot%er !y!tem!.
PHPExcel allow! rea(in' an( writin' to B, )ile!.
!3V limitations
Plea!e note t%at B, )ile )ormat %a! !ome limit! re'ar(in' to !tylin' cell!6 num4er )ormattin'6 H
8.7.1. PHPExcel/6ea!er/CD
6ea!in) a CD ,ile
Kou can rea( a .c!v )ile u!in' t%e )ollowin' co(e+
o!"Reade& 7 ne2 PHPExcel_Reade&_-S.();
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.cs%=);
ettin) CD options
I)ten6 B, )ile! are not really Qcomma !eparate(R6 or u!e !emicolon /P0 a! a !eparator. Kou can
in!truct PHPExcel2*ea(er2B, !ome option! 4e)ore rea(in' a B, )ile+
o!"Reade& 7 ne2 PHPExcel_Reade&_-S.();
o!"Reade&#$set'eli,ite&(';');
o!"Reade&#$setEnclosu&e('');
o!"Reade&#$setLineEnding(=@&@n=);
o!"Reade&#$setSheet8ndex(;);
o!"PHPExcel 7 o!"Reade&#$load(=;C(eatu&ede,o.cs%=);
6ea! a speci,ic (or%s"eet
B, )ile! can only contain one wor-!%eet. 1%ere)ore6 you can !peci)y w%ic% !%eet to rea( )rom B,+
o!"Reade&#$setSheet8ndex(;);
6ea! into existin) sprea!s"eet
?%en wor-in' wit% B, )ile!6 it mi'%t occur t%at you want to import B, (ata into an exi!tin'
PHPExcel o4Gect. 1%e )ollowin' co(e loa(! a B, )ile into an exi!tin' Uo4GPHPExcel containin' !ome
!%eet!6 an( import! onto t%e 6
t%
!%eet+
o!"Reade& 7 ne2 PHPExcel_Reade&_-S.();
o!"Reade&#$set'eli,ite&(';');
o!"Reade&#$setEnclosu&e('');
o!"Reade&#$setLineEnding(=@&@n=);
o!"Reade&#$setSheet8ndex(C);
o!"Reade&#$load8ntoExisting(=;C(eatu&ede,o.cs%=1 o!"PHPExcel);
8.7.2. PHPExcel/0riter/CD
0ritin) a CD ,ile
Kou can write a .c!v )ile u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_-S.(o!"PHPExcel);
o!">&ite&#$sa%e(=;C(eatu&ede,o.cs%=);
PHPExcel Developer Documentation 2<
ettin) CD options
I)ten6 B, )ile! are not really Qcomma !eparate(R6 or u!e !emicolon /P0 a! a !eparator. Kou can
in!truct PHPExcel2?riter2B, !ome option! 4e)ore writin' a B, )ile+
o!">&ite& 7 ne2 PHPExcel_>&ite&_-S.(o!"PHPExcel);
o!">&ite&#$set'eli,ite&(';');
o!">&ite&#$setEnclosu&e('');
o!">&ite&#$setLineEnding(=@&@n=);
o!">&ite&#$setSheet8ndex(;);
o!">&ite&#$sa%e(=;C(eatu&ede,o.cs%=);
0rite a speci,ic (or%s"eet
B, )ile! can only contain one wor-!%eet. 1%ere)ore6 you can !peci)y w%ic% !%eet to write to B,+
o!">&ite&#$setSheet8ndex(;);
#ormula preCcalculation
By (e)ault6 t%i! writer pre8calculate! all )ormula! in t%e !prea(!%eet. 1%i! can 4e !low on lar'e
!prea(!%eet!6 an( may4e even unwante(. Kou can %owever (i!a4le )ormula pre8calculation+
o!">&ite& 7 ne2 PHPExcel_>&ite&_-S.(o!"PHPExcel);
o!">&ite&#$setP&e-alculate*o&,ulas((alse);
o!">&ite&#$sa%e(=;C(eatu&ede,o.cs%=);
0ritin) 9&#C; CD ,iles
$ B, )ile can 4e mar-e( a! @1.8< 4y writin' a BIM )ile %ea(er. 1%i! can 4e ena4le( 4y u!in' t%e
)ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_-S.(o!"PHPExcel);
o!">&ite&#$setQse/OK(t&ue);
o!">&ite&#$sa%e(=;C(eatu&ede,o.cs%=);
).). !T01
PHPExcel allow! you to write a !prea(!%eet into H1ME )ormat6 )or "uic- repre!entation o) t%e (ata
in it to anyone w%o (oe! not %ave a !prea(!%eet application on t%eir P.
+,ML limitations
Plea!e note t%at H1ME )ile )ormat %a! !ome limit! re'ar(in' to !tylin' cell!6 num4er )ormattin'6 H
8.8.1. PHPExcel/0riter/H&ML
Plea!e note t%at PHPExcel2?riter2H1ME only output! t%e )ir!t wor-!%eet 4y (e)ault.
0ritin) a sprea!s"eet
Kou can write a .%tm )ile u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_HTKL(o!"PHPExcel);
o!">&ite&#$sa%e(=;C(eatu&ede,o.ht,=);
0rite all (or%s"eets
H1ME )ile! can contain one or more wor-!%eet!. 5) you want to write all !%eet! into a !in'le H1ME
)ile6 u!e t%e )ollowin' co(e+
o!">&ite&#$2&iteAllSheets();
0rite a speci,ic (or%s"eet
H1ME )ile! can contain one or more wor-!%eet!. 1%ere)ore6 you can !peci)y w%ic% !%eet to write to
H1ME+
o!">&ite&#$setSheet8ndex(;);
PHPExcel Developer Documentation 29
ettin) t"e ima)es root o, t"e H&ML ,ile
1%ere mi'%t 4e !ituation! w%ere you want to explicitly !et t%e inclu(e( ima'e! root. .or example6
one mi'%t want to !ee 5i,g st)le7=position9 &elati%e; le(t9 ;px; top9 ;px; 2idth9
3G;px; height9 <0px;= s&c7=http://www.domain.com/i,ages/logo."pg= !o&de&7=;=$ in!tea(
o) 5i,g st)le7=position9 &elati%e; le(t9 ;px; top9 ;px; 2idth9 3G;px; height9
<0px;= s&c7=./i,ages/logo."pg= !o&de&7=;=$.
Kou can u!e t%e )ollowin' co(e to ac%ieve t%i! re!ult+
o!">&ite&#$set8,agesRoot('http9//222.exa,ple.co,');
#ormula preCcalculation
By (e)ault6 t%i! writer pre8calculate! all )ormula! in t%e !prea(!%eet. 1%i! can 4e !low on lar'e
!prea(!%eet!6 an( may4e even unwante(. Kou can %owever (i!a4le )ormula pre8calculation+
o!">&ite& 7 ne2 PHPExcel_>&ite&_HTKL(o!"PHPExcel);
o!">&ite&#$setP&e-alculate*o&,ulas((alse);
o!">&ite&#$sa%e(=;C(eatu&ede,o.ht,=);
Em'e!!in) )enerate! H&ML in a (e' pa)e
1%ere mi'%t 4e a !ituation w%ere you want to em4e( t%e 'enerate( H1ME in an exi!tin' we4!ite.
PHPExcel2?riter2H1ME provi(e! !upport to 'enerate only !peci)ic part! o) t%e H1ME co(e6 w%ic%
allow! you to u!e t%e!e part! in your we4!ite.
Bupporte( met%o(!+
'enerateH1MEHea(er/0
'enerateBtyle!/0
'enerateB%eetData/0
'enerateH1ME.ooter/0
HereF! an example w%ic% retrieve! all part! in(epen(ently an( mer'e! t%em into a re!ultin' H1ME
pa'e+
56php
o!">&ite& 7 ne2 PHPExcel_>&ite&_HTKL(o!"PHPExcel);
echo o!">&ite&#$gene&ateHTKLHeade&();
6$
5st)le$
5T##
ht,l A
(ont#(a,il)9 Ti,es +e2 Ro,an;
(ont#siVe9 Ipt;
!ac?g&ound#colo&9 2hite;
B
56php
echo o!">&ite&#$gene&ateSt)les((alse); // do not 2&ite 5st)le$ and 5/st)le$
6$
##$
5/st)le$
56php
echo o!">&ite&#$gene&ateSheet'ata();
echo o!">&ite&#$gene&ateHTKL*oote&();
6$
0ritin) 9&#C; H&ML ,iles
$ H1ME )ile can 4e mar-e( a! @1.8< 4y writin' a BIM )ile %ea(er. 1%i! can 4e ena4le( 4y u!in' t%e
)ollowin' co(e+
PHPExcel Developer Documentation #0
o!">&ite& 7 ne2 PHPExcel_>&ite&_HTKL(o!"PHPExcel);
o!">&ite&#$setQse/OK(t&ue);
o!">&ite&#$sa%e(=;C(eatu&ede,o.ht,=);
)... 7,
PHPExcel allow! you to write a !prea(!%eet into PD. )ormat6 )or )a!t (i!tri4ution o) repre!ente(
(ata.
#DF limitations
Plea!e note t%at PD. )ile )ormat %a! !ome limit! re'ar(in' to !tylin' cell!6 num4er )ormattin'6 H
8...1. PHPExcel/0riter/PD#
Plea!e note t%at PHPExcel2?riter2PD. only output! t%e )ir!t wor-!%eet 4y (e)ault.
0ritin) a sprea!s"eet
Kou can write a .p() )ile u!in' t%e )ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_P'*(o!"PHPExcel);
o!">&ite&#$sa%e(=;C(eatu&ede,o.pd(=);
0rite all (or%s"eets
PD. )ile! can contain one or more wor-!%eet!. 5) you want to write all !%eet! into a !in'le PD. )ile6
u!e t%e )ollowin' co(e+
o!">&ite&#$2&iteAllSheets();
0rite a speci,ic (or%s"eet
PD. )ile! can contain one or more wor-!%eet!. 1%ere)ore6 you can !peci)y w%ic% !%eet to write to
PD.+
o!">&ite&#$setSheet8ndex(;);
#ormula preCcalculation
By (e)ault6 t%i! writer pre8calculate! all )ormula! in t%e !prea(!%eet. 1%i! can 4e !low on lar'e
!prea(!%eet!6 an( may4e even unwante(. Kou can %owever (i!a4le )ormula pre8calculation+
o!">&ite& 7 ne2 PHPExcel_>&ite&_P'*(o!"PHPExcel);
o!">&ite&#$setP&e-alculate*o&,ulas((alse);
o!">&ite&#$sa%e(=;C(eatu&ede,o.pd(=);
&emporar$ stora)e
5nternally6 PD. writer !tore! temporary )ile! on (i!-. ?%en your !erver a(mini!trator (i!a4le( )ile
acce!! on t%e operatin' !y!temF! temporary (irectory6 you can overri(e t%e (e)ault 4y u!in' t%e
)ollowin' co(e+
o!">&ite& 7 ne2 PHPExcel_>&ite&_P'*(o!"PHPExcel);
o!">&ite&#$setTe,p'i&(=-9/te,p/=);
o!">&ite&#$sa%e(=;C(eatu&ede,o.pd(=);
PHPExcel Developer Documentation #1
.. Cre!its
Plea!e re)er to t%e internet pa'e %ttp+77www.co(eplex.com7PHPExcel7?i-i7,iew.a!px:
title;re(it!=re)errin'1itle;Home )or up8to8(ate cre(it!.
PHPExcel Developer Documentation #2
+ppen!ix +E Dali! arra$ %e$s ,or st$le appl$#rom+rra$AB
1%e )ollowin' ta4le li!t! t%e vali( array -ey! )or PHPExcel2Btyle apply.rom$rray/0 cla!!e!. 5) t%e
QMap! to propertyR column map! a -ey to a !etter6 t%e value provi(e( )or t%at -ey will 4e applie(
(irectly. 5) t%e QMap! to propertyR column map! a -ey to a 'etter6 t%e value provi(e( )or t%at -ey
will 4e applie( a! anot%er !tyle array.
#+#Excel93t"le
$rray -ey+ Map! to property+
)ill
)ont
4or(er!
ali'nment
num4er)ormat
protection
'et.ill/0
'et.ont/0
'etBor(er!/0
'et$li'nment/0
'etDum4er.ormat/0
'etProtection/0
#+#Excel93t"le9Fill
$rray -ey+ Map! to property+
type
rotation
!tartcolor
en(color
color
!et.ill1ype/0
!et*otation/0
'etBtartolor/0
'etEn(olor/0
'etBtartolor/0
#+#Excel93t"le9Font
$rray -ey+ Map! to property+
name
4ol(
italic
un(erline
!tri-e
color
!i3e
!uperBcript
!u4Bcript
!etDame/0
!etBol(/0
!et5talic/0
!et@n(erline/0
!etBtri-et%rou'%/0
'etolor/0
!etBi3e/0
!etBuperBcript/0
!etBu4Bcript/0
#+#Excel93t"le96orders
$rray -ey+ Map! to property+
all4or(er!
le)t
ri'%t
top
4ottom
(ia'onal
vertical
%ori3ontal
(ia'onal(irection
outline
'etEe)t/0P 'et*i'%t/0P 'et1op/0P 'etBottom/0
'etEe)t/0
'et*i'%t/0
'et1op/0
'etBottom/0
'etDia'onal/0
'et,ertical/0
'etHori3ontal/0
!etDia'onalDirection/0
!etIutline/0
#+#Excel93t"le96order
$rray -ey+ Map! to property+
!tyle
color
!etBor(erBtyle/0
'etolor/0
#+#Excel93t"le9Ali*nment
$rray -ey+ Map! to property+
%ori3ontal
vertical
!etHori3ontal/0
!et,ertical/0
PHPExcel Developer Documentation ##
rotation
wrap
!%rin-1o.it
in(ent
!et1ext*otation/0
!et?rap1ext/0
!etB%rin-1o.it/0
!et5n(ent/0
#+#Excel93t"le9/umberFormat
$rray -ey+ Map! to property+
co(e !et.ormato(e/0
#+#Excel93t"le9#rotection
$rray -ey+ Map! to property+
loc-e(
%i((en
!etEoc-e(/0
!etHi((en/0
PHPExcel Developer Documentation #4

You might also like