Professional Documents
Culture Documents
PHPExcel Documentation Formatos
PHPExcel Documentation Formatos
1.
Contents
PHPExcel Developer Documentation............................................................................1 1. Content .......................................................................................................... 2 2. Prere!ui ite ..................................................................................................... 3 3. "rc#itecture..................................................................................................... $ %. Creatin& a prea' #eet.........................................................................................9 (. Per)ormin& )ormula calculation ............................................................................3$ $. *ea'in& an' writin& to )ile...................................................................................3+ 7. Cre'it ........................................................................................................... %9 "ppen'ix ", -ali' array .ey )or tyle apply/rom"rray01...................................................(0
2.
2.1.
Prerequisites
Software requirements
2#e )ollowin& o)tware i re!uire' to 'evelop u in& PHPExcel, PHP ver ion (.2.0 or newer PHP exten ion p#p34ip ena5le' 61 PHP exten ion p#p3xml ena5le' PHP exten ion p#p3&'2 ena5le' 0i) not compile' in1
*) php_zip i only nee'e' 5y PHPExcel_Reader_Excel200 7 PHPExcel_!riter_Excel200 an' PHPExcel_Reader_""#alc. 8n ot#er wor' 7 i) you nee' PHPExcel to #an'le .xl x or .o' )ile you will nee' t#e 4ip exten ion7 5ut ot#erwi e not. 9ou can remove t#i 'epen'ency )or writin& Excel2007 )ile 0t#ou&# not yet )or rea'in&1 5y u in& t#e PC:;ip li5rary t#at i 5un'le' wit# PHPExcel. <ee t#e /"= ection o) t#i 'ocument 02.%.21 )or 'etail a5out t#i . PC:;ip 'oe #ave a 'epen'ency on PHP> 4li5 exten ion 5ein& ena5le'.
2.2.
Installation instructions
8n tallation i !uite ea y, copy t#e content o) t#e Cla e )ol'er to any location wit#in your application ource 'irectorie .
Example: 8) your we5 root )ol'er i ?var?www? you may want to create a u5)ol'er calle' ?var?www?Cla e ? an' copy t#e )ile into t#at )ol'er o you en' up wit# )ile , ?var?www?Cla e ?PHPExcel.p#p ?var?www?Cla e ?PHPExcel?Calculation.p#p ?var?www?Cla e ?PHPExcel?Cell.p#p ...
2.3.
Getting started
" &oo' way to &et tarte' i to run ome o) t#e te t inclu'e' in t#e 'ownloa'.
Copy t#e @Example @ )ol'er next to your @Cla e @ )ol'er )rom a5ove o you en' up wit#, ?var?www? Example ?01 imple.p#p ?var?www? Example ?02type .p#p ... <tart runnin& t#e te t 5y pointin& your 5row er to t#e te t cript , #ttp,??example.com? Example ?01 imple.p#p #ttp,??example.com? Example ?02type .p#p ... Aote, 8t may 5e nece ary to mo'i)y t#e inclu'e?re!uire tatement at t#e 5e&innin& o) eac# o) t#e te t cript i) your @Cla e @ )ol'er )rom a5ove i name' 'i))erently.
2.4.
2#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 Co'ePlex pa&e )or an up'ate ver ion o) t#e li t 5elow.
2.4.1.
OpenXML / prea!s"eetML
$ile %or&at docu&entation #ttp,??www.ecmaBinternational.or&?new ?2C%(3current3wor.?2C%(3availa5le3'oc .#tm "pen'() Explained e*+oo, #ttp,??openxml'eveloper.or&?article ?1970.a px (icroso%t "%%ice #o&pati+ilit- Pac, %or !ord. Excel. and Po/erPoint 200 $ile $or&ats #ttp,??www.micro o)t.com?'ownloa' ?'etail .a pxC)amilyi'D9%153%70B3ae9B%aeeB+)%3B
2.4.2.
2#e upBtoB'ate /.".=. pa&e )or PHPExcel can 5e )oun' on #ttp,??www.co'eplex.com?PHPExcel?Fi.i?-iew.a pxCtitleD/"=Ere)errin&2itleD*e!uirement .
before callin& t#e ave met#o' o) t#e Excel2007 Friter. 9ou can revert to u in& ;ip"rc#ive 5y callin&,
PHPExcel_Settings::setZipClass(PHPExcel_Settings::ZIPARCHIVE);
"t pre ent7 t#i only allow you to write Excel2007 )ile wit#out t#e nee' )or ;ip"rc#ive 0not to rea' Excel2007 or IICalc1
Excel 2--. cannot 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? f you trust the source of this workbook! click "es.# <ome ol'er ver ion o) t#e (.2.x p#p34ip exten ion on Fin'ow contain an error w#en creatin& ;8P )ile . 2#e ver ion t#at can 5e )oun' on #ttp,?? nap .p#p.net?win32?p#p(.2Bwin32Blate t.4ip #oul' wor. at all time . "lternatively7 up&ra'in& to at lea t PHP (.2.9 #oul' olve t#e pro5lem. 8) you can>t locate a clean copy o) ;ip"rc#ive7 t#en you can u e t#e PC:;ip li5rary a an alternative w#en writin& Excel2007 )ile 7 a 'e cri5e' a5ove.
#atal error1 +llo(e! memor$ si2e o, xxx '$tes ex"auste! 3trie! to allocate $$$ '$tes4 in 222 on line aaa
PHPExcel #ol' an @in memory@ repre entation o) a prea' #eet7 o it i re tricte' 5y PHPH memory limitation . 2#e memory ma'e availa5le to PHP can 5e increa e' 5y e'itin& t#e value o) t#e memory3limit 'irective in your p#p.ini )ile7 or 5y u in& ini3 et0Hmemory3limitH7 H12+MH1 wit#in your co'e 08<P permittin&1. <ome *ea'er an' Friter are )a ter t#an ot#er 7 an' t#ey al o u e 'i))erin& amount o) memory. 9ou can )in' ome in'ication o) t#e relative per)ormance an' memory u a&e )or t#e 'i))erent *ea'er an' Friter 7 over t#e 'i))erent ver ion o) PHPExcel7 on t#e 'i cu ion 5oar'. 8) youHve alrea'y increa e' memory to a maximum7 or canHt c#an&e your memory limit7 t#en t#i 'i cu ion on t#e 5oar' 'e cri5e ome o) t#e met#o' t#at can 5e applie' to re'uce t#e memory u a&e o) your cript u in& PHPExcel. PHPExcel Developer Documentation
2.4.9.
&utorials
En0lish PHPExcel tutorial #ttp,??openxml'eveloper.or& $rench PHPExcel tutorial #ttp,??&Bernael ten.'eveloppe4.com?tutoriel ?excel2007? Russian PHPExcel 1lo0 Postin0s #ttp,??www.we5BXunior.net? o4'anieBexcelB)aXXlovB Bpomo ##yuBp#pexcel? A 2apanese*lan0ua0e introduction to PHPExcel #ttp,??Xournal.mycom.co.Xp?article ?2009?03?0$?p#pexcel?in'ex.#tml
9.
3.1.
+rc"itecture
Schematical
3.2.
Laz Loader
PHPExcel implement an autoloa'er or Jla4y loa'erK7 w#ic# mean t#at it i not nece ary to inclu'e every )ile wit#in PHPExcel. 8t i only nece ary to inclu'e t#e initial PHPExcel cla )ile7 t#en t#e autoloa'er will inclu'e ot#er cla )ile a an' w#en re!uire'7 o only t#o e )ile t#at are actually re!uire' 5y your cript will 5e loa'e' into PHP memory. 2#e main 5ene)it o) t#i i t#at it re'uce t#e memory )ootprint o) PHPExcel it el)7 o t#at it u e le PHP memory. 8) your own cript alrea'y 'e)ine an autoloa' )unction7 t#en t#i may 5e overwritten 5y t#e PHPExcel autoloa' )unction. /or example7 i) you #ave,
&%nction'__a%toloa(($class)') ''''*** +
Do t#i in tea',
&%nction',-A%toloa(($class)') ''''*** + spl_a%toloa(_$egiste$(.,-A%toloa(.);
3.3.
S!readsheet in memor
PHPExcel> arc#itecture i 5uilt in a way t#at it can erve a an inBmemory prea' #eet. 2#i mean t#at7 i) one woul' want to create a we5 5a e' view o) a prea' #eet w#ic# communicate wit# PHPExcel> o5Xect mo'el7 #e woul' only #ave to write t#e )rontBen' co'e. Lu t li.e 'e .top prea' #eet o)tware7 PHPExcel repre ent a prea' #eet containin& one or more wor. #eet 7 w#ic# contain cell wit# 'ata7 )ormula 7 ima&e 7 Y
3.4.
In it own7 PHPExcel 'oe not provi'e t#e )unctionality to rea' )rom or write to a per i te' prea' #eet 0on 'i . or in a 'ata5a e1. 2o provi'e t#at )unctionality7 rea'er an' writer can 5e u e'.
By 'e)ault7 t#e PHPExcel pac.a&e provi'e ome rea'er an' writer 7 inclu'in& one )or t#e Ipen ZM: prea' #eet )ormat 0a...a. Excel 2007 )ile )ormat1. 9ou are not limite' to t#e 'e)ault rea'er an' writer 7 a you are )ree to implement t#e PHPExcel3Friter38*ea'er an' PHPExcel3Friter38Friter inter)ace in a cu tom cla .
3.#.
$luent interfaces
PHPExcel upport )luent inter)ace in mo t location . 2#i mean t#at you can ea ily Jc#ainK call to peci)ic met#o' wit#out re!uirin& a new PHP tatement. /or example7 ta.e t#e )ollowin& co'e,
$objPHPExcel !getP$ope$ties() !setC$eato$(/0aa$ten'1allia%2/); $objPHPExcel !getP$ope$ties() !setLast0o(i&ie(1-(/0aa$ten'1allia%2/);
$objPHPExcel !getP$ope$ties() !set3itle(/4&&ice'5667'8LS8'3est'9oc%,ent/); $objPHPExcel !getP$ope$ties() !setS%bject(/4&&ice'5667'8LS8'3est'9oc%,ent/); $objPHPExcel !getP$ope$ties() !set9esc$iption(/3est'(oc%,ent'&o$'4&&ice'5667'8LS8:' gene$ate('%sing'PHP'classes*/); $objPHPExcel !getP$ope$ties() !set;e-2o$(s(/o&&ice'5667'openx,l'p#p/); $objPHPExcel !getP$ope$ties() !setCatego$-(/3est'$es%lt'&ile/);
3sin0 %luent inter%aces is not re4uired /luent inter)ace #ave 5een implemente' to provi'e a convenient pro&rammin& "P8. G e o) t#em i not re!uire'7 5ut can ma.e your co'e ea ier to rea' an' maintain. 8t can al o improve per)ormance7 a you are re'ucin& t#e overall num5er o) call to PHPExcel met#o' .
4.
4.1.
Creatin) a sprea!s"eet
%he &'&()cel class
2#e PHPExcel cla i t#e core o) PHPExcel. 8t contain re)erence to t#e containe' wor. #eet 7 'ocument ecurity ettin& an' 'ocument meta 'ata. 2o impli)y t#e PHPExcel concept, t#e PHPExcel cla repre ent your wor.5oo.. 2ypically7 you will create a wor.5oo. in one o) two way 7 eit#er 5y loa'in& it )rom a prea' #eet )ile7 or creatin& it manually. " t#ir' option7 t#ou&# le commonly u e'7 i clonin& an exi tin& wor.5oo. t#at #a 5een create' u in& one o) t#e previou two met#o' .
4.1.1.
Detail o) t#e 'i))erent prea' #eet )ormat upporte'7 an' t#e option availa5le to rea' t#em into a PHPExcel o5Xect are 'e cri5e' )ully in t#e JPHPExcel G er Documentation B *ea'in& <prea' #eet /ile K 'ocument. $inp%t<ile=a,e'>'.*?sa,ple9ata?exa,ple@*xls.; ?AA'Loa('$inp%t<ile=a,e'to'a'PHPExcel'4bject''AA? $objPHPExcel'>'PHPExcel_I4<acto$-::loa(($inp%t<ile=a,e);
4.1.2.
8) you want to create a new wor.5oo.7 rat#er t#an loa' one )rom )ile7 t#en you imply nee' to in tantiate it a a new PHPExcel o5Xect. ?AA'C$eate'a'ne2'PHPExcel'4bject''AA? $objPHPExcel'>'ne2'PHPExcel(); " new wor.5oo. will alway 5e create' wit# a in&le wor. #eet.
4.2.
*onfiguration Settings
Ince you #ave inclu'e' t#e PHPExcel )ile in your cript7 5ut 5e)ore in tantiatin& a PHPExcel o5Xect or loa'in& a wor.5oo. )ile7 t#ere are a num5er o) con)i&uration option t#at can 5e et w#ic# will a))ect t#e u5 e!uent 5e#aviour o) t#e cript.
4.2.1.
Cell Cac"in)
PHPExcel u e an avera&e o) a5out 1.?cell in your wor. #eet 7 o lar&e wor.5oo. can !uic.ly u e up availa5le memory. Cell cac#in& provi'e a mec#ani m t#at allow PHPExcel to maintain t#e cell o5Xect in a maller i4e o) memory7 on 'i .7 or in "PC7 memcac#e or Fincac#e7 rat#er t#an in PHP memory. 2#i allow you to re'uce t#e memory u a&e )or lar&e wor.5oo. 7 alt#ou&# at a co t o) pee' to acce cell 'ata. By 'e)ault7 PHPExcel till #ol' all cell o5Xect in memory7 5ut you can peci)y alternative . 2o ena5le cell cac#in&7 you mu t call t#e PHPExcel3<ettin& ,, etCac#e<tora&eMet#o'01 met#o'7 pa in& in t#e cac#in& met#o' t#at you wi # to u e.
$cac#e0et#o('>'PHPExcel_Cac#e(4bjectSto$age<acto$-::cac#e_in_,e,o$-; PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o();
etCac#e<tora&eMet#o'01 will return a 5oolean true on ucce 7 )al e on )ailure 0)or example i) tryin& to cac#e to "PC w#en "PC i not ena5le'1.
" eparate cac#e i maintaine' )or eac# in'ivi'ual wor. #eet7 an' i automatically create' w#en t#e wor. #eet i in tantiate' 5a e' on t#e cac#in& met#o' an' ettin& t#at you #ave con)i&ure'. 9ou cannot c#an&e t#e con)i&uration ettin& once you #ave tarte' to rea' a wor.5oo.7 or #ave create' your )ir t wor. #eet. Currently7 t#e )ollowin& cac#in& met#o' are availa5le. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3in3memory[ 2#e 'e)ault. 8) you 'on>t initiali e any cac#in& met#o'7 t#en t#i i t#e met#o' t#at PHPExcel will u e. Cell o5Xect are maintaine' in PHP memory a at pre ent. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3in3memory3 eriali4e'[ G in& t#i cac#in& met#o'7 cell are #el' in PHP memory a an array o) eriali4e' o5Xect 7 w#ic# re'uce t#e memory )ootprint wit# minimal per)ormance over#ea'. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3in3memory3&4ip[ :i.e cac#e3in3memory3 eriali4e'7 t#i met#o' #ol' cell in PHP memory a an array o) eriali4e' o5Xect 7 5ut &4ippe' to re'uce t#e memory u a&e till )urt#er7 alt#ou&# acce to rea' or write a cell i li&#tly lower. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3i&5inary[ G e PHP> i&5inary exten ion 0i) it> availa5le1 to eriali4e cell o5Xect in memory. 2#i i normally )a ter an' u e le memory t#an tan'ar' PHP eriali4ation7 5ut i n>t availa5le in mo t #o tin& environment . PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3'i c8<"M[ F#en u in& cac#e3to3'i c8<"M all cell are #el' in a temporary 'i . )ile7 wit# only an in'ex to t#eir location in t#at )ile maintaine' in PHP memory. 2#i i lower t#an any o) t#e cac#e3in3memory met#o' 7 5ut i&ni)icantly re'uce t#e memory )ootprint. By 'e)ault7 PHPExcel will u e PHP> temp 'irectory )or t#e cac#e )ile7 5ut you can peci)y a 'i))erent 'irectory w#en initiali in& cac#e3to3'i c8<"M.
$cac#e0et#o('>'PHPExcel_Cac#e(4bjectSto$age<acto$-::'cac#e_to_(iscISA0; $cac#eSettings'>'a$$a-('.(i$.''>!'.?%s$?local?t,p. ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);
2#e temporary 'i . )ile i automatically 'elete' w#en your cript terminate . PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3p#p2emp[ :i.e cac#e3to3'i c8<"M7 w#en u in& cac#e3to3p#p2emp all cell are #el' in t#e p#p,??temp 8?I tream7 wit# only an in'ex to t#eir location maintaine' in PHP memory. 8n PHP7 t#e p#p,??memory wrapper tore 'ata in t#e memory, p#p,??temp 5e#ave imilarly7 5ut u e a temporary )ile )or torin& t#e 'ata w#en a certain memory limit i reac#e'. 2#e 'e)ault i 1 MB7 5ut you can c#an&e t#i w#en initiali in& cac#e3to3p#p2emp.
$cac#e0et#o('>'PHPExcel_Cac#e(4bjectSto$age<acto$-::'cac#e_to_p#p3e,p; $cac#eSettings'>'a$$a-('.,e,o$-Cac#eSiBe.''>!'.C01. ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);
2#e p#p,??temp )ile i automatically 'elete' w#en your cript terminate . PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3apc[ F#en u in& cac#e3to3apc7 cell o5Xect are maintaine' in "PC 1 wit# only an in'ex maintaine' in PHP memory to i'enti)y t#at t#e cell exi t . By 'e)ault7 an "PC cac#e timeout o) $00 econ' i u e'7 w#ic# #oul' 5e enou&# )or mo t application , alt#ou&# it i po i5le to c#an&e t#i w#en initiali in& cac#e3to3"PC.
$cac#e0et#o('>'PHPExcel_Cac#e(4bjectSto$age<acto$-::cac#e_to_APC; $cac#eSettings'>'a$$a-('.cac#e3i,e.''''''''>!'D66
1
''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);
F#en your cript terminate all entrie will 5e cleare' )rom "PC7 re&ar'le value7 o it cannot 5e u e' )or per i tent tora&e u in& t#i mec#ani m. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3memcac#e
o) t#e cac#e2ime
F#en u in& cac#e3to3memcac#e7 cell o5Xect are maintaine' in memcac#e 2 wit# only an in'ex maintaine' in PHP memory to i'enti)y t#at t#e cell exi t . By 'e)ault7 PHPExcel loo. )or a memcac#e erver on local#o t at port 11211. 8t al o et a memcac#e timeout limit o) $00 econ' . 8) you are runnin& memcac#e on a 'i))erent erver or port7 t#en you can c#an&e t#e e 'e)ault w#en you initiali e cac#e3to3memcac#e,
$cac#e0et#o('>'PHPExcel_Cac#e(4bjectSto$age<acto$-::cac#e_to_,e,cac#e; $cac#eSettings'>'a$$a-('.,e,cac#eSe$"e$.''>!'.local#ost.: ''''''''''''''''''''''''.,e,cac#ePo$t.''''>!'@@5@@: ''''''''''''''''''''''''.cac#e3i,e.'''''''>!'D66 ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);
F#en your cript terminate all entrie will 5e cleare' )rom memcac#e7 re&ar'le o) t#e cac#e2ime value7 o it cannot 5e u e' )or per i tent tora&e u in& t#i mec#ani m. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3wincac#e[ F#en u in& cac#e3to3wincac#e7 cell o5Xect are maintaine' in Fincac#e 3 wit# only an in'ex maintaine' in PHP memory to i'enti)y t#at t#e cell exi t . By 'e)ault7 a Fincac#e cac#e timeout o) $00 econ' i u e'7 w#ic# #oul' 5e enou&# )or mo t application , alt#ou&# it i po i5le to c#an&e t#i w#en initiali in& cac#e3to3wincac#e.
$cac#e0et#o('>'PHPExcel_Cac#e(4bjectSto$age<acto$-::cac#e_to_2incac#e; $cac#eSettings'>'a$$a-('.cac#e3i,e.''''''''>!'D66 ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);
F#en your cript terminate all entrie will 5e cleare' )rom Fincac#e7 re&ar'le value7 o it cannot 5e u e' )or per i tent tora&e u in& t#i mec#ani m. PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3 !lite[
o) t#e cac#e2ime
G e an <=:ite 2 inBmemory 'ata5a e )or cac#in& cell 'ata. Gnli.e ot#er cac#in& met#o' 7 neit#er cell nor an in'ex are #el' in PHP memory B an in'exe' 'ata5a e ta5le ma.e it unnece ary to #ol' any in'ex in PHP memory \ ma.in& t#i t#e mo t memoryBe))icient o) t#e cell cac#in& met#o' . PHPExcel3Cac#e'I5Xect<tora&e/actory,,cac#e3to3 !lite3[ G e an <=:ite 3 inBmemory 'ata5a e )or cac#in& cell 'ata. Gnli.e ot#er cac#in& met#o' 7 neit#er cell nor an in'ex are #el' in PHP memory B an in'exe' 'ata5a e ta5le ma.e it unnece ary to #ol' any in'ex in PHP memory \ ma.in& t#i t#e mo t memoryBe))icient o) t#e cell cac#in& met#o' .
4.2.2.
Lan)ua)e/Locale
<ome locali ation element #ave 5een inclu'e' in PHPExcel. 9ou can et a locale 5y c#an&in& t#e ettin& . 2o et t#e locale to Bra4ilian Portu&ue e you woul' u e,
$locale'>'.pt_b$.; $"ali(Locale'>'PHPExcel_Settings::setLocale($locale); i&'(E$"ali(Locale)') ec#o'.Fnable'to'set'locale'to'.*$locale*/' '$e"e$ting'to'en_%sGb$'?!Hn/; +
9ou mu t #ave a memcac#e erver runnin&7 an' #ave ena5le' memcac#e )or your PHP to u e t#i option. 3 9ou mu t #ave Fincac#e ena5le' )or PHP to u e t#i option. PHPExcel Developer Documentation 11
8) Bra4ilian Portu&ue e lan&ua&e )ile aren>t availa5le7 t#en t#e Portu&ue e will 5e ena5le' in tea', i) Portu&ue e lan&ua&e )ile aren>t availa5le7 t#en t#e et:ocale01 met#o' will return an error7 an' "merican En&li # 0en3u 1 ettin& will 5e u e' t#rou&#out. More 'etail o) t#e )eature availa5le once a locale #a 5een et7 inclu'in& a li t o) t#e lan&ua&e an' locale currently upporte'7 can 5e )oun' in ection %.$.( :ocale <ettin& )or /ormulae.
4.3.
2#e PHPExcel o5Xect contain cyclic re)erence 0e.&. t#e wor.5oo. i lin.e' to t#e wor. #eet 7 an' t#e wor. #eet are lin.e' to t#eir parent wor.5oo.1 w#ic# cau e pro5lem w#en PHP trie to clear t#e o5Xect )rom memory w#en t#ey are un et017 or at t#e en' o) a )unction w#en t#ey are in local cope. 2#e re ult o) t#i i Jmemory lea. K7 w#ic# can ea ily u e a lar&e amount o) PHP> limite' memory. 2#i can only 5e re olve' manually, i) you nee' to un et a wor.5oo.7 t#en you al o nee' to J5rea.K t#e e cyclic re)erence 5e)ore 'oin& o. PHPExcel provi'e t#e 'i connectFor. #eet 01 met#o' )or t#i purpo e.
$objPHPExcel !(isconnectIo$Js#eets(); %nset($objPHPExcel);
4.4.
+orksheets
" wor. #eet i a collection o) cell 7 )ormula> 7 ima&e 7 &rap# 7 Y 8t #ol' all 'ata nece ary to repre ent a a prea' #eet wor. #eet. F#en you loa' a wor.5oo. )rom a prea' #eet )ile7 it will 5e loa'e' wit# all it exi tin& wor. #eet 0unle you peci)ie' t#at only certain #eet #oul' 5e loa'e'1. F#en you loa' )rom nonB prea' #eet )ile 0 uc# a a C<- or H2M: )ile1 or )rom prea' #eet )ormat t#at 'on>t i'enti)y wor. #eet 5y name 0 uc# a <9:M17 t#en a in&le wor. #eet calle' JFor.<#eetK will 5e create' containin& t#e 'ata )rom t#at )ile. F#en you in tantiate a new wor.5oo.7 PHPExcel will create it wit# a in&le wor. #eet calle' JFor.<#eetK. 2#e &et<#eetCount01 met#o' will tell you t#e num5er o) wor. #eet in t#e wor.5oo.[ w#ile t#e &et<#eetAame 01 met#o' will return a li t o) all wor. #eet in t#e wor.5oo.7 in'exe' 5y t#e or'er in w#ic# t#eir Jta5 K woul' appear w#en opene' in M< Excel 0or ot#er appropriate <prea' #eet pro&ram1. 8n'ivi'ual wor. #eet can 5e acce e' 5y name7 or 5y t#eir in'ex po ition in t#e wor.5oo.. 2#e in'ex po ition repre ent t#e or'er t#at eac# wor. #eet Jta5K i #own w#en t#e wor.5oo. i opene' in M< Excel 0or ot#er appropriate <prea' #eet pro&ram1. 2o acce a #eet 5y it in'ex7 u e t#e &et<#eet01 met#o'.
??'Ket't#e'secon('s#eet'in't#e'2o$JbooJ ??'=ote't#at's#eets'a$e'in(exe('&$o,'6 $objPHPExcel !getS#eet(@);
8) you 'on>t peci)y a #eet in'ex7 t#en t#e )ir t wor. #eet will 5e returne'. Met#o' al o exi t allowin& you to reor'er t#e wor. #eet in t#e wor.5oo.. 2o acce a #eet 5y name7 u e t#e &et<#eetByAame01 met#o'7 peci)yin& t#e name o) t#e wor. #eet t#at you want to acce .
??'Ret$ie"e't#e'2o$Js#eet'calle('.Io$Js#eet'@. $objPHPExcel !getS#eet1-=a,e(.Io$Js#eet'@.);
"lternatively7 one wor. #eet i alway t#e currently active wor. #eet7 an' you can acce t#at 'irectly. 2#e currently active wor. #eet i t#e one t#at will 5e active w#en t#e wor.5oo. i opene' in M< Excel 0or ot#er appropriate <prea' #eet pro&ram1.
??'Ret$ie"e't#e'c%$$ent'acti"e'2o$Js#eet $objPHPExcel !getActi"eS#eet();
12
9ou can c#an&e t#e currently active #eet 5y in'ex or 5y name u in& t#e et"ctive<#eet8n'ex01 an' et"ctive<#eet8n'exByAame01met#o' .
4.4.1.
9ou can a'' a new wor. #eet to t#e wor.5oo. u in& t#e create<#eet01 met#o' o) t#e PHPExcel o5Xect. By 'e)ault7 t#i will 5e create' a a new Jla tK #eet[ 5ut you can al o peci)y an in'ex po ition a an ar&ument7 an' t#e wor. #eet will 5e in erte' at t#at po ition7 #u))lin& all u5 e!uent wor. #eet in t#e collection 'own a place.
$objPHPExcel !c$eateS#eet();
" new wor. #eet create' u in& t#i met#o' will 5e calle' JFor. #eetK or JFor. #eet]n^K w#ere J]n^K i t#e lowe t num5er po i5le to &uarantee t#at t#e title i uni!ue. "lternatively7 you can in tantiate a new wor. #eet 0 ettin& t#e title to w#atever you c#oo e1 an' t#en in ert it into your wor.5oo. u in& t#e a''<#eet01 met#o'.
??'C$eate'a'ne2'2o$Js#eet'calle('L0-'9ataM $,-Io$JS#eet'>'ne2'PHPExcel_Io$Js#eet($objPHPExcel:'.0-'9ata.); ??'Attac#'t#e'L0-'9ataM'2o$Js#eet'as't#e'&i$st'2o$Js#eet'in't#e'PHPExcel'object $objPHPExcel !a((S#eet($,-Io$JS#eet:'6);
8) you 'on>t peci)y an in'ex po ition a t#e econ' ar&ument7 t#en t#e new wor. #eet will 5e a''e' a)ter t#e la t exi tin& wor. #eet.
4.4.2.
Cop$in) 0or%s"eets
<#eet wit#in t#e ame wor.5oo. can 5e copie' 5y creatin& a clone o) t#e wor. #eet you wi # to copy7 an' t#en u in& t#e a''<#eet01 met#o' to in ert t#e clone into t#e wor.5oo..
$objClone(Io$Js#eet'>'clone'$objPHPExcel !getS#eet1-=a,e(.Io$Js#eet'@.); $objClone(Io$Js#eet !set3itle(.Cop-'o&'Io$Js#eet'@.) $objPHPExcel !a((S#eet($objClone(Io$Js#eet);
9ou can al o copy wor. #eet )rom one wor.5oo. to anot#er7 t#ou&# t#i i more complex a PHPExcel al o #a to replicate t#e tylin& 5etween t#e two wor.5oo. . 2#e a''External<#eet01 met#o' i provi'e' )or t#i purpo e.
$objClone(Io$Js#eet'>'clone'$objPHPExcel@ !getS#eet1-=a,e(.Io$Js#eet'@.); $objPHPExcel !a((Exte$nalS#eet($objClone(Io$Js#eet);
8n 5ot# ca e 7 it i t#e 'eveloper> re pon i5ility to en ure t#at wor. #eet name are not 'uplicate'. PHPExcel will t#row an exception i) you attempt to copy wor. #eet t#at will re ult in a 'uplicate name.
4.4.9.
6emovin) a 0or%s"eet
9ou can 'elete a wor. #eet )rom a wor.5oo.7 i'enti)ie' 5y it in'ex po ition7 u in& t#e remove<#eetBy8n'ex01 met#o'
$s#eetIn(ex'>'$objPHPExcel !getIn(ex($objPHPExcel !'getS#eet1-=a,e(.Io$Js#eet'@.)); $objPHPExcel !$e,o"eS#eet1-In(ex($s#eetIn(ex);
8) t#e currently active wor. #eet i 'elete'7 t#en t#e #eet at t#e previou in'ex po ition will 5ecome t#e currently active #eet.
4.#.
-ccessing cells
ome o)
"cce in& cell in a PHPExcel wor. #eet #oul' 5e pretty trai&#t)orwar'. 2#i topic li t t#e option to acce a cell.
4.=.1.
<ettin& a cell value 5y coor'inate can 5e 'one u in& t#e wor. #eet> setCellVal%e met#o'.
$objPHPExcel !getActi"eS#eet() !setCellVal%e(.1C.:'.So,e'"al%e.);
13
4.=.2.
2o retrieve t#e value o) a cell7 t#e cell #oul' )ir t 5e retrieve' )rom t#e wor. #eet u in& t#e getCell met#o'. " cell> value can 5e rea' a&ain u in& t#e )ollowin& line o) co'e,
$objPHPExcel !getActi"eS#eet() !getCell(.1C.) !getVal%e();
8) you nee' t#e calculate' value o) a cell7 u e t#e )ollowin& co'e. 2#i i )urt#er explaine' in %.$.%1.
$objPHPExcel !getActi"eS#eet() !getCell(.1C.) !getCalc%late(Val%e();
4.=.9.
<ettin& a cell value 5y coor'inate can 5e 'one u in& t#e wor. #eet> setCellVal%e1-Col%,nAn(Ro2 met#o'.
??'Set'cell'1C $objPHPExcel !getActi"eS#eet() !setCellVal%e1-Col%,nAn(Ro2(@:'C:'.So,e'"al%e.);
4.=.4.
2o retrieve t#e value o) a cell7 t#e cell #oul' )ir t 5e retrieve' )rom t#e wor. #eet u in& t#e getCell1-Col%,nAn(Ro2 met#o'. " cell> value can 5e rea' a&ain u in& t#e )ollowin& line o) co'e,
??'Ket'cell'1C $objPHPExcel !getActi"eS#eet() !getCell1-Col%,nAn(Ro2(@:'C) !getVal%e();
8) you nee' t#e calculate' value o) a cell7 u e t#e )ollowin& co'e. 2#i i )urt#er explaine' in %.$.%1
??'Ket'cell'1C $objPHPExcel !getActi"eS#eet() !getCell1-Col%,nAn(Ro2(@:'C) !getCalc%late(Val%e();
4.=.=.
Loopin) cells
1%
Aote t#at we #ave et t#e cell iterator> setIte$ate4nl-ExistingCells() to )al e. 2#i ma.e t#e iterator loop all cell 7 even i) t#ey were not et 5e)ore.
5he cell iterator /ill return null as the cell i% it is not set in the /or,sheet6 <ettin& t#e cell iterator> setIte$ate4nl-ExistingCells()to &alse will loop all cell in t#e wor. #eet t#at can 5e availa5le at t#at moment. 2#i will create new cell i) re!uire' an' increa e memory u a&e_ Inly u e it i) it i inten'e' to loop all cell t#at are po i5ly availa5le.
Aote, 8n PHPExcel column in'ex i 0B5a e' w#ile row in'ex i 1B5a e'. 2#at mean H"1H ` 00711
Below i an example w#ere we rea' all t#e value in a wor. #eet an' 'i play t#em in a ta5le.
GNp#p $objRea(e$'>'PHPExcel_I4<acto$-::c$eateRea(e$(.Excel5667.); $objRea(e$ !setRea(9ata4nl-(t$%e); $objPHPExcel'>'$objRea(e$ !loa((/test*xlsx/); $objIo$Js#eet'>'$objPHPExcel !getActi"eS#eet(); $#ig#estRo2'>'$objIo$Js#eet !getHig#estRo2();'??'e*g*'@6 $#ig#estCol%,n'>'$objIo$Js#eet !getHig#estCol%,n();'??'e*g'.<. $#ig#estCol%,nIn(ex'>'PHPExcel_Cell::col%,nIn(ex<$o,St$ing($#ig#estCol%,n);'??'e*g*' O ec#o'.Gtable!.'*'/Hn/; &o$'($$o2'>'@;'$$o2'G>'$#ig#estRo2;'PP$$o2)') ''ec#o'.Gt$!.'*'/Hn/; ''&o$'($col'>'6;'$col'G>'$#ig#estCol%,nIn(ex;'PP$col)') ''''ec#o'.Gt(!.'*'$objIo$Js#eet !getCell1-Col%,nAn(Ro2($col:'$$o2) !getVal%e()'*' .G?t(!.'*'/Hn/; ''+ ''ec#o'.G?t$!.'*'/Hn/; + ec#o'.G?table!.'*'/Hn/; N!
4.=.>.
8nternally7 PHPExcel u e a 'e)ault PHPExcel_Cell_IVal%e1in(e$ implementation 0PHPExcel_Cell_9e&a%ltVal%e1in(e$1 to 'etermine 'ata type o) entere' 'ata u in& a cell> setVal%e() met#o'. Iptionally7 t#e 'e)ault 5e#aviour o) PHPExcel can 5e mo'i)ie'7 allowin& ea ier 'ata entry. /or example7 a PHPExcel_Cell_A("ance(Val%e1in(e$ cla i pre ent. 8t automatically convert percenta&e an' 'ate entere' a trin& to t#e correct )ormat7 al o ettin& t#e cell> tyle in)ormation. 2#e )ollowin& example 'emon trate #ow to et t#e value 5in'er in PHPExcel,
?AA'PHPExcel'A? $eQ%i$e_once'.PHPExcel*p#p.; ?AA'PHPExcel_Cell_A("ance(Val%e1in(e$'A? $eQ%i$e_once'.PHPExcel?Cell?A("ance(Val%e1in(e$*p#p.; ?AA'PHPExcel_I4<acto$-'A? $eQ%i$e_once'.PHPExcel?I4<acto$-*p#p.;
1(
??'Set'"al%e'bin(e$ PHPExcel_Cell::setVal%e1in(e$('ne2'PHPExcel_Cell_A("ance(Val%e1in(e$()'); ??'C$eate'ne2'PHPExcel'object $objPHPExcel'>'ne2'PHPExcel(); ??'*** ??'A(('so,e'(ata:'$ese,bling'so,e'(i&&e$ent'(ata't-pes $objPHPExcel !getActi"eS#eet() !setCellVal%e(.AR.:'.Pe$centage'"al%e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1R.:'.@6S.); ''''''''''''''??'Con"e$ts'to'6*@'an('sets'pe$centage'cell'st-le $objPHPExcel !getActi"eS#eet() !setCellVal%e(.AO.:'.9ate?ti,e'"al%e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1O.:'.5@'9ece,be$'@TCU.); ''''''''''''''??'Con"e$ts'to'(ate'an('sets'(ate'&o$,at'cell'st-le
#reatin0 -our o/n 7alue +inder is eas-6 F#en a'vance' value 5in'in& i re!uire'7 you can implement t#e PHPExcel_Cell_IVal%e1in(e$ inter)ace or exten' t#e PHPExcel_Cell_9e&a%ltVal%e1in(e$ or PHPExcel_Cell_A("ance(Val%e1in(e$ cla e .
4...
&'&()cel reci!es
2#e )ollowin& pa&e o))er you ome wi'elyBu e' PHPExcel recipe . Plea e note t#at t#e e 'o AI2 o))er complete 'ocumentation on peci)ic PHPExcel "P8 )unction 7 5ut Xu t a 5ump to &et you tarte'. 8) you nee' peci)ic "P8 )unction 7 plea e re)er to t#e "P8 'ocumentation. /or example7 %.$.9 <ettin& a wor. #eet> pa&e orientation an' i4e cover ettin& a pa&e orientation to "%. It#er paper )ormat 7 li.e G< :etter7 are not covere' in t#i 'ocument7 5ut in t#e PHPExcel "P8 'ocumentation.
4.>.1.
PHPExcel allow an ea y way to et a prea' #eet> meta'ata7 u in& 'ocument property acce or . <prea' #eet meta'ata can 5e u e)ul )or )in'in& a peci)ic 'ocument in a )ile repo itory or a 'ocument mana&ement y tem. /or example Micro o)t <#arepoint u e 'ocument meta'ata to earc# )or a peci)ic 'ocument in it 'ocument li t . <ettin& prea' #eet meta'ata i 'one a )ollow ,
$objPHPExcel !getP$ope$ties() !setC$eato$(/0aa$ten'1allia%2/); $objPHPExcel !getP$ope$ties() !setLast0o(i&ie(1-(/0aa$ten'1allia%2/); $objPHPExcel !getP$ope$ties() !set3itle(/4&&ice'5667'8LS8'3est'9oc%,ent/); $objPHPExcel !getP$ope$ties() !setS%bject(/4&&ice'5667'8LS8'3est'9oc%,ent/); $objPHPExcel !getP$ope$ties() !set9esc$iption(/3est'(oc%,ent'&o$'4&&ice'5667'8LS8:' gene$ate('%sing'PHP'classes*/); $objPHPExcel !getP$ope$ties() !set;e-2o$(s(/o&&ice'5667'openx,l'p#p/); $objPHPExcel !getP$ope$ties() !setCatego$-(/3est'$es%lt'&ile/);
4.>.2.
2#e )ollowin& line o) co'e et t#e active #eet in'ex to t#e )ir t #eet,
$objPHPExcel !setActi"eS#eetIn(ex(6);
4.>.9.
8n Excel7 'ate an' 2ime are tore' a numeric value countin& t#e num5er o) 'ay elap e' ince 1900B01B01. /or example7 t#e 'ate H200+B12B31H i repre ente' a 39+13. 9ou can veri)y t#i in Micro o)t I))ice Excel 5y enterin& t#at 'ate in a cell an' a)terwar' c#an&in& t#e num5er )ormat to HaeneralH o t#e true numeric value i reveale'. :i.ewi e7 H3,1( "MH i repre ente' a 0.13(%17. PHPExcel wor. wit# G<2 0Gniver al <tan'ar' 2ime1 'ate an' 2ime value 7 5ut 'oe no internal conver ion [ o it i up to t#e 'eveloper to en ure t#at value pa e' to t#e 'ate?time conver ion )unction are G<2.
1$
Fritin& a 'ate value in a cell con i t o) 2 line o) co'e. <elect t#e met#o' t#at uit you t#e 5e t. Here are ome example ,
?A'PHPExcel_Cell_A("anceVal%e1in(e$'$eQ%i$e('&o$'t#is'sa,ple'A? $eQ%i$e_once'.PHPExcel?Cell?A("ance(Val%e1in(e$*p#p.; ??'0-SVL liJe'ti,esta,p'.566C @5 U@.'o$'(ate'st$ing PHPExcel_Cell::setVal%e1in(e$('ne2'PHPExcel_Cell_A("ance(Val%e1in(e$()'); $objPHPExcel !getActi"eS#eet() '''''''''''' !setCellVal%e(.9@.:'.566C @5 U@.); $objPHPExcel !getActi"eS#eet() '''''''''''' !getSt-le(.9@.) '''''''''''' !get=%,be$<o$,at() '''''''''''' !set<o$,atCo(e(PHPExcel_St-le_=%,be$<o$,at::<4R0A3_9A3E_WWWW0099SLASH) ??'PHP ti,e'(Fnix'ti,e) $ti,e'>'g,,Jti,e(6:6:6:@5:U@:566C);'??'int(@5U6DC@D66) $objPHPExcel !getActi"eS#eet() '''''''''''' !setCellVal%e(.9@.:'PHPExcel_S#a$e(_9ate::PHP3oExcel($ti,e)); $objPHPExcel !getActi"eS#eet() '''''''''''' !getSt-le(.9@.) '''''''''''' !get=%,be$<o$,at() '''''''''''' !set<o$,atCo(e(PHPExcel_St-le_=%,be$<o$,at::<4R0A3_9A3E_WWWW0099SLASH) ??'Excel (ate?ti,e $objPHPExcel !getActi"eS#eet() '''''''''''' !setCellVal%e(.9@.:'UTC@U) $objPHPExcel !getActi"eS#eet() '''''''''''' !getSt-le(.9@.) '''''''''''' !get=%,be$<o$,at() '''''''''''' !set<o$,atCo(e(PHPExcel_St-le_=%,be$<o$,at::<4R0A3_9A3E_WWWW0099SLASH)
2#e a5ove met#o' )or enterin& a 'ate all yiel' t#e ame re ult. PHPExcel3<tyle3Aum5er/ormat provi'e a lot o) preB'e)ine' 'ate )ormat . 2#e PHPExcel3<#are'3Date,,PHP2oExcel01 met#o' will al o wor. wit# a PHP Date2ime o5Xect. <imilarly7 time 0or 'ate an' time value 1 can 5e entere' in t#e ame )a #ion, Xu t remem5er to u e an appropriate )ormat co'e. 8otes: 1. <ee ection @G in& value 5in'er to )acilitate 'ata entry@ to learn more a5out t#e "'vance'-alueBin'er u e' in t#e )ir t example. 2. 8n previou ver ion o) PHPExcel up to an' inclu'in& 1.$.$7 w#en a cell #a' a 'ateBli.e num5er )ormat co'e7 it wa po i5le to enter a 'ate 'irectly u in& an inte&er PHPBtime wit#out convertin& to Excel 'ate )ormat. <tartin& wit# PHPExcel 1.$.7 t#i i no lon&er upporte'. 3. Excel can al o operate in a 190%B5a e' calen'ar 0'e)ault )or wor.5oo. Aormally7 you 'o not #ave to worry a5out t#i w#en u in& PHPExcel. ave' on Mac1.
4.>.4.
8n i'e t#e Excel )ile7 )ormula are alway tore' a t#ey woul' appear in an En&li # ver ion o) Micro o)t I))ice Excel7 an' PHPExcel #an'le all )ormulae internally in t#i )ormat. 2#i mean t#at t#e )ollowin& rule #ol', Decimal eparator i H.H 0perio'1 /unction ar&ument eparator i H7H 0comma1 Matrix row eparator i H[H 0 emicolon1
17
En&li # )unction name mu t 5e u e' 2#i i re&ar'le o) w#ic# lan&ua&e ver ion o) Micro o)t I))ice Excel may #ave 5een u e' to create t#e Excel )ile. F#en t#e )inal wor.5oo. i opene' 5y t#e u er7 Micro o)t I))ice Excel will ta.e care o) 'i playin& t#e )ormula accor'in& t#e application lan&ua&e. 2ran lation i ta.en care o) 5y t#e application_ 2#e )ollowin& line o) co'e write t#e )ormula JD8/0C%^(007@pro)it@7@lo @1K into t#e cell B+. Aote t#at t#e )ormula mu t tart wit# JDK to ma.e PHPExcel reco&ni e t#i a a )ormula.
$objPHPExcel !getActi"eS#eet() !setCellVal%e(.1C.:.>I<(CR!O66:/p$o&it/:/loss/).);
8) you want to write a trin& 5e&innin& wit# an JDK to a cell7 t#en you #oul' u e t#e etCell-alueExplicit01 met#o'.
$objPHPExcel !getActi"eS#eet() '''''''''''' !setCellVal%eExplicit(.1C.: '''''''''''''''''''''''''''''''''''.>I<(CR!O66:/p$o&it/:/loss/).: '''''''''''''''''''''''''''''''''''PHPExcel_Cell_9ata3-pe::3WPE_S3RI=K '''''''''''''''''''''''''''''''''');
" cell> )ormula can 5e rea' a&ain u in& t#e )ollowin& line o) co'e,
$&o$,%la'>'$objPHPExcel !getActi"eS#eet() !getCell(.1C.) !getVal%e();
8) you nee' t#e calculate' value o) a cell7 u e t#e )ollowin& co'e. 2#i i )urt#er explaine' in %.$.%1.
$"al%e'>'$objPHPExcel !getActi"eS#eet() !getCell(.1C.) !getCalc%late(Val%e();
4.>.=.
<ome locali ation element #ave 5een inclu'e' in PHPExcel. 9ou can et a locale 5y c#an&in& t#e ettin& . 2o et t#e locale to *u ian you woul' u e,
$locale'>'.$%.; $"ali(Locale'>'PHPExcel_Settings::setLocale($locale); i&'(E$"ali(Locale)') ec#o'.Fnable'to'set'locale'to'.*$locale*/' '$e"e$ting'to'en_%sGb$'?!Hn/; +
8) *u ian lan&ua&e )ile aren>t availa5le7 t#e et:ocale01 met#o' will return an error7 an' En&li # ettin& will 5e u e' t#rou&#out. Ince you #ave et a locale7 you can tran late a )ormula )rom it internal En&li # co'in&.
$&o$,%la'>'$objPHPExcel !getActi"eS#eet() !getCell(.1C.) !getVal%e(); $t$anslate(<o$,%la'>' PHPExcel_Calc%lation::getInstance() !_t$anslate<o$,%la3oLocale($&o$,%la);
9ou can al o create a )ormula u in& t#e )unction name an' ar&ument eparator appropriate to t#e 'e)ine' locale[ t#en tran late it to En&li # 5e)ore ettin& t#e cell value,
$&o$,%la'>'.>XYZ[UD6(X\]\(56@6;5;O);X\]\(56@6;@5;U@);^_]^Y\).; $inte$nal<o$,%la'>' PHPExcel_Calc%lation::getInstance() !t$anslate<o$,%la3oEnglis#($&o$,%la); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1C.:$inte$nal<o$,%la);
Currently7 )ormula tran lation only tran late t#e )unction name 7 t#e con tant 2*GE an' /":<E7 an' t#e )unction ar&ument eparator . "t pre ent7 t#e )ollowin& locale ettin& are upporte', )an0ua0e C4ec# Dani # aerman bectina Dan . Deut c# )ocale #ode c 'a 'e
1+
)an0ua0e <pani # /inni # /renc# Hun&arian 8talian Dutc# Aorwe&ian Poli # Portu&ue e Bra4ilian Portu&ue e *u ian <we'i # 2ur.i #
E padol <uomi /raneai Ma&yar 8taliano Ae'erlan' Aor . Lf4y. pol .i Portu&ug Portu&ug Bra ileiro ShiijVk Wlmj <ven .a 2nr.ee
4.>.>.
8n Micro o)t I))ice Excel you &et a line 5rea. in a cell 5y #ittin& AL3P/Ente$/. F#en you 'o t#at7 it automatically turn on @wrap text@ )or t#e cell. Here i #ow to ac#ieve t#i in PHPExcel,
$objPHPExcel !getActi"eS#eet() !getCell(.A@.) !setVal%e(/#elloHn2o$l(/); $objPHPExcel !getActi"eS#eet() !getSt-le(.A@.) !getAlign,ent() !setI$ap3ext(t$%e);
5ip *ea' more a5out )ormattin& cell u in& getSt-le() el ew#ere. 5ip A("ance(Val%ebin(e$*p#p automatically turn on @wrap text@ )or t#e cell w#en it ee a newline c#aracter in a trin& t#at you are in ertin& in a cell. Lu t li.e Micro o)t I))ice Excel. 2ry t#i , $eQ%i$e_once'.PHPExcel?Cell?A("ance(Val%e1in(e$*p#p.; PHPExcel_Cell::setVal%e1in(e$('ne2'PHPExcel_Cell_A("ance(Val%e1in(e$()'); $objPHPExcel !getActi"eS#eet() !getCell(.A@.) !setVal%e(/#elloHn2o$l(/); *ea' more a5out A("ance(Val%e1in(e$*p#p el ew#ere.
4.>...
9ou can et a cell> 'atatype explicitly 5y u in& t#e cell> setVal%eExplicit met#o'7 or t#e setCellVal%eExplicit met#o' o) a wor. #eet. Here> an example,
$objPHPExcel !getActi"eS#eet() !getCell(.A@.) !setVal%eExplicit(.5O.:' PHPExcel_Cell_9ata3-pe::3WPE_=F0ERIC);
4.>.C.
8) you want to ma.e a #yperlin. to anot#er wor. #eet?cell7 u e t#e )ollowin& co'e,
$objPHPExcel !getActi"eS#eet() !setCellVal%e(.E5D.:'.222*p#pexcel*net.); $objPHPExcel !getActi"eS#eet() !getCell(.E5D.) !getH-pe$linJ() !setF$l(Ls#eet:??.S#eetna,e.EA@M);
4.>.D.
<ettin& a wor. #eet> pa&e orientation an' i4e can 5e 'one u in& t#e )ollowin& line o) co'e,
19
Aote t#at t#ere are a''itional pa&e ettin& availa5le. Plea e re)er to t#e "P8 'ocumentation )or all po i5le option .
4.>.1-.
2#e pa&e etup calin& option in PHPExcel relate 'irectly to t#e calin& option in t#e @Pa&e <etup@ 'ialo& a #own in t#e illu tration. De)ault value in PHPExcel corre pon' to 'e)ault value in M< I))ice Excel a #own in illu tration
=ote
Example
Here i #ow to %it to 9 pa0e /ide +- in%inite pa0es tall,
$objPHPExcel !getActi"eS#eet() !getPageSet%p() !set<it3oIi(t#(@); $objPHPExcel !getActi"eS#eet() !getPageSet%p() !set<it3oHeig#t(6);
" you can ee7 it i not nece ary to call set<it3oPage(t$%e) ince set<it3oIi(t#(`) an' set<it3oHeig#t(`) tri&&er t#i .
20
8) you u e set<it3oIi(t#() you #oul' in &eneral al o peci)y set<it3oHeig#t() explicitly li.e in t#e example. Be care)ul relyin& on t#e initial value . 2#i i e pecially true i) you are up&ra'in& )rom PHPExcel 1.7.0 to 1.7.1 w#ere t#e 'e)ault value )or )itBtoB#ei&#t an' )itBtoBwi't# c#an&e' )rom 0 to 1.
4.>.11.
Pa)e mar)ins
!getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getPage0a$gins() !getPage0a$gins() !getPage0a$gins() !getPage0a$gins() !set3op(@); !setRig#t(6*7O); !setLe&t(6*7O); !set1otto,(@);
4.>.12.
4.>.19.
<ettin& a wor. #eet> print #ea'er an' )ooter can 5e 'one u in& t#e )ollowin& line o) co'e,
$objPHPExcel !getActi"eS#eet() !getHea(e$<oote$() !set4((Hea(e$(.aCaHPlease't$eat' t#is'(oc%,ent'as'con&i(entialE.); $objPHPExcel !getActi"eS#eet() !getHea(e$<oote$() !set4((<oote$(.aLa1.'*' $objPHPExcel !getP$ope$ties() !get3itle()'*'.aRPage'aP'o&'a=.);
<u5 titution an' )ormattin& co'e 0 tartin& wit# E1 can 5e u e' in i'e #ea'er an' )ooter . 2#ere i no re!uire' or'er in w#ic# t#e e co'e mu t appear. 2#e )ir t occurrence o) t#e )ollowin& co'e turn t#e )ormattin& IA7 t#e econ' occurrence turn it I// a&ain, <tri.et#rou&# <uper cript <u5 cript
21
<uper cript an' u5 cript cannot 5ot# 5e IA at ame time. F#ic#ever come )ir t win an' t#e ot#er i i&nore'7 w#ile t#e )ir t i IA. 2#e )ollowin& co'e are upporte' 5y Excel2007, Co'e )or @le)t ection@ 0t#ere are t#ree #ea'er ? )ooter location 7 @le)t@7 @center@7 an' @ri&#t@1. F#en two or more occurrence o) t#i ection mar.er exi t7 t#e content )rom all mar.er are concatenate'7 in t#e or'er o) appearance7 an' place' into t#e le)t ection. &P Co'e )or @current pa&e o@ &N Co'e )or @total pa&e @ &font size Co'e )or @text )ont i4e@7 w#ere )ont i4e i a )ont i4e in point . &K Co'e )or @text )ont color@
&L
*aB Color i peci)ie' a **aaBB 2#eme Color i peci)e' a 22<AA w#ere 22 i t#e t#eme color 8'7 < i eit#er @p@ or @B@ o) t#e tint? #a'e value7 AA i t#e tint? #a'e value. Co'e )or @text tri.et#rou&#@ on ? o)) Co'e )or @text uper cript@ on ? o)) Co'e )or @text u5 cript@ on ? o)) Co'e )or @center ection@. F#en two or more occurrence o) t#i ection mar.er exi t7 t#e content )rom all mar.er are concatenate'7 in t#e or'er o) appearance7 an' place' into t#e center ection. Co'e )or @'ate@ Co'e )or @time@ Co'e )or @picture a 5ac.&roun'@ Plea e ma.e ure to a'' t#e ima&e to t#e #ea'er?)ooter,
$obj9$a2ing'>'ne2' PHPExcel_Io$Js#eet_Hea(e$<oote$9$a2ing(); $obj9$a2ing !set=a,e(.PHPExcel'logo.); $obj9$a2ing !setPat#(.*?i,ages?p#pexcel_logo*gi&.); $obj9$a2ing !setHeig#t(UD); $objPHPExcel !getActi"eS#eet() !getHea(e$<oote$() !a((I,age($obj9$a2ing:' PHPExcel_Io$Js#eet_Hea(e$<oote$::I0AKE_HEA9ER_LE<3);
Co'e )or @text in&le un'erline@ Co'e )or @'ou5le un'erline@ Co'e )or @ri&#t ection@. F#en two or more occurrence o) t#i ection mar.er exi t7 t#e content )rom all mar.er are concatenate'7 in t#e or'er o) appearance7 an' place' into t#e ri&#t ection. Co'e )or @t#i wor.5oo.H )ile pat#@ Co'e )or @t#i wor.5oo.H )ile name@ Co'e )or @ #eet ta5 name@ Co'e )or a'' to pa&e o Co'e )or u5tract )rom pa&e o Co'e )or @text )ont name@ an' @text )ont type@7 w#ere )ont name an' )ont type are trin& peci)yin& t#e name an' type o) t#e )ont7 eparate' 5y a comma. F#en a #yp#en appear in )ont name7 it mean @none peci)ie'@. Bot# o) )ont name an' )ont type can 5e locali4e' value . Co'e )or @5ol' )ont tyle@ Co'e )or @5ol' )ont tyle@ Co'e )or @re&ular )ont tyle@ Co'e )or @italic )ont tyle@ Co'e )or @italic )ont tyle@
22
Co'e )or @5ol' italic )ont tyle@ Co'e )or @outline tyle@ Co'e )or @ #a'ow tyle@
5ip 2#e a5ove ta5le o) co'e may eem overw#elmin& )ir t time you are tryin& to )i&ure out #ow to write ome #ea'er or )ooter. :uc.ily7 t#ere i an ea ier way. :et Micro o)t I))ice Excel 'o t#e wor. )or you. /or example7 create in Micro o)t I))ice Excel an xlsx )ile w#ere you in ert t#e #ea'er an' )ooter a 'e ire' u in& t#e pro&ram own inter)ace. <ave )ile a test*xlsx. Aow7 ta.e t#at )ile an' rea' o)) t#e value u in& PHPExcel a )ollow , $objPHPexcel'>'PHPExcel_I4<acto$-::loa((.test*xlsx.); $objIo$Js#eet'>'$objPHPexcel !getActi"eS#eet(); "a$_(%,p($objIo$Js#eet !getHea(e$<oote$() !get4((<oote$()); "a$_(%,p($objIo$Js#eet !getHea(e$<oote$() !getE"en<oote$()); "a$_(%,p($objIo$Js#eet !getHea(e$<oote$() !get4((Hea(e$()); "a$_(%,p($objIo$Js#eet !getHea(e$<oote$() !getE"enHea(e$()); 2#at reveal t#e co'e )or t#e even?o'' #ea'er an' )ooter. Experience' u er may )in' it ea ier to rename test*xlsx to test*Bip7 un4ip it7 an' in pect 'irectly t#e content o) t#e relevant xl?2o$Js#eets?s#eet8*x,l to )in' t#e co'e )or #ea'er?)ooter.
4.>.14.
2o et a print 5rea.7 u e t#e )ollowin& co'e7 w#ic# et a row 5rea. on row 10.
$objPHPExcel !getActi"eS#eet() !set1$eaJ('.A@6.':'PHPExcel_Io$Js#eet::1REA;_R4I');
4.>.1=.
4.>.1>.
PHPExcel can repeat peci)ic row ?cell at top?le)t o) a pa&e. 2#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,
$objPHPExcel !getActi"eS#eet() !getPageSet%p() !setRo2s3oRepeatAt3op1-Sta$tAn(En((@:'O);
4.>.1..
4.>.1C.
#ormattin) cells
" cell can 5e )ormatte' wit# )ont7 5or'er7 )ill7 Y tyle in)ormation. /or example7 one can et t#e )ore&roun' colour o) a cell to re'7 ali&ne' to t#e ri&#t7 an' t#e 5or'er to 5lac. an' t#ic. 5or'er tyle. :et> 'o t#at on cell B2,
$objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get<ont() !getColo$() !setARK1(PHPExcel_St-le_Colo$::C4L4R_RE9); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !getAlign,ent() !setHo$iBontal(PHPExcel_St-le_Align,ent::H4RIZ4=3AL_RIKH3);
23
$objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get1o$(e$s() !set1o$(e$St-le(PHPExcel_St-le_1o$(e$::14R9ER_3HIC;); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get1o$(e$s() !set1o$(e$St-le(PHPExcel_St-le_1o$(e$::14R9ER_3HIC;); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get1o$(e$s() !set1o$(e$St-le(PHPExcel_St-le_1o$(e$::14R9ER_3HIC;); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get1o$(e$s() !set1o$(e$St-le(PHPExcel_St-le_1o$(e$::14R9ER_3HIC;);
$objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get<ill() !set<ill3-pe(PHPExcel_St-le_<ill::<ILL_S4LI9); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get<ill() !getSta$tColo$() !setARK1(.<<<<6666.);
<tartin& wit# PHPExcel 1.7.0 &et<tyle01 al o accept a cell ran&e a a parameter. /or example7 you can et a re' 5ac.&roun' color on a ran&e o) cell ,
$objPHPExcel !getActi"eS#eet() !getSt-le(.1U:17.) !get<ill() !set<ill3-pe(PHPExcel_St-le_<ill::<ILL_S4LI9) !getSta$tColo$() !setARK1(.<<<<6666.);
5ip 8t i recommen'e' to tyle many cell at once7 u in& e.&. &et<tyle0H"1,M(00H17 rat#er t#an tylin& t#e cell in'ivi'ually in a loop. 2#i i muc# )a ter compare' to loopin& t#rou&# cell an' tylin& t#em in'ivi'ually.
2#ere i al o an alternative manner to et tyle . 2#e )ollowin& co'e et a cell> ali&nment ri&#t7 top 5or'er t#in an' a &ra'ient )ill,
$st-leA$$a-'>'a$$a-( .&ont.'>!'a$$a-( .bol(.'>!'t$%e: ): .align,ent.'>!'a$$a-( .#o$iBontal.'>!'PHPExcel_St-le_Align,ent::H4RIZ4=3AL_RIKH3: ): .bo$(e$s.'>!'a$$a-( .top.'>!'a$$a-( .st-le.'>!'PHPExcel_St-le_1o$(e$::14R9ER_3HI=: ): ): .&ill.'>!'a$$a-( .t-pe.'>!'PHPExcel_St-le_<ill::<ILL_KRA9IE=3_LI=EAR: .$otation.'>!'T6: .sta$tcolo$.'>!'a$$a-( .a$gb.'>!'.<<A6A6A6.: ): .en(colo$.'>!'a$$a-( .a$gb.'>!'.<<<<<<<<.: ): ): ); $objPHPExcel !getActi"eS#eet() !getSt-le(.AU.) !appl-<$o,A$$a-($st-leA$$a-);
2#i alternative met#o' u in& array #oul' 5e )a ter in term o) execution w#enever you are ettin& more t#an one tyle property. But t#e 'i))erence may 5arely 5e mea ura5le unle you #ave many 'i))erent tyle in your wor.5oo..
2%
Prior to PHPExcel 1.7.0 'uplicate<tyle"rray01 wa t#e recommen'e' met#o' )or tylin& a cell ran&e7 5ut t#i met#o' #a now 5een 'eprecate' ince &et<tyle01 #a tarte' to accept a cell ran&e.
4.>.1D.
Eum'er ,ormats
9ou o)ten want to )ormat num5er in Excel. /or example you may want a t#ou an' eparator plu a )ixe' num5er o) 'ecimal a)ter t#e 'ecimal eparator. Ir per#ap you want ome num5er to 5e 4eroBpa''e'. 8n Micro o)t I))ice Excel you may 5e )amiliar wit# electin& a num5er )ormat )rom t#e @/ormat Cell @ 'ialo&. Here t#ere are ome pre'e)ine' num5er )ormat availa5le inclu'in& ome )or 'ate . 2#e 'ialo& i 'e i&ne' in a way o you 'onHt #ave to interact wit# t#e un'erlyin& raw num5er )ormat co'e unle you nee' a cu tom num5er )ormat. 8n PHPExcel7 you can al o apply variou pre'e)ine' num5er )ormat . Example,
$objPHPExcel !getActi"eS#eet() !getSt-le(.A@.) !get=%,be$<o$,at() !set<o$,atCo(e(PHPExcel_St-le_=%,be$<o$,at::<4R0A3_=F01ER_C400A_SEPARA3E9@);
2#i will )ormat a num5er e.&. 1(+7.2 o it #ow up a 17(+7.20 w#en you open t#e wor.5oo. in M< I))ice Excel. 0Depen'in& on ettin& )or 'ecimal an' t#ou an' eparator in Micro o)t I))ice Excel it may #ow up a 1.(+77201 9ou can ac#ieve exactly t#e ame a t#e a5ove 5y u in& t#i ,
$objPHPExcel !getActi"eS#eet() !getSt-le(.A@.) !get=%,be$<o$,at() !set<o$,atCo(e(.b:bb6*66.);
8n Micro o)t I))ice Excel7 a well a in PHPExcel7 you will #ave to interact wit# raw num5er )ormat co'e w#enever you nee' ome pecial cu tom num5er )ormat. Example,
$objPHPExcel !getActi"eS#eet() !getSt-le(.A@.) !get=%,be$<o$,at() !set<o$,atCo(e(.c1l%edc!>U666d$b:bb6;cRe(dcG6d$b:bb6;$b:bb6.);
"not#er example i w#en you want num5er 4eroBpa''e' wit# lea'in& 4ero to a )ixe' len&t#,
$objPHPExcel !getActi"eS#eet() !getCell(.A@.) !setVal%e(@T); $objPHPExcel !getActi"eS#eet() !getSt-le(.A@.) !get=%,be$<o$,at() !set<o$,atCo(e(.6666.);'??'2ill's#o2'as'66@T'in'Excel
5ip 2#e rule )or compo in& a num5er )ormat co'e in Excel can 5e rat#er complicate'. <ometime you .now #ow to create ome num5er )ormat in Micro o)t I))ice Excel7 5ut 'onHt .now w#at t#e un'erlyin& num5er )ormat co'e loo. li.e. How 'o you )in' itC 2#e rea'er #ippe' wit# PHPExcel come to t#e re cue. :oa' your template wor.5oo. u in& e.&. Excel2007 rea'er to reveal t#e num5er )ormat co'e. Example #ow rea' a num5er )ormat co'e )or cell "1, $objRea(e$'>'PHPExcel_I4<acto$-::c$eateRea(e$(.Excel5667.); $objPHPExcel'>'$objRea(e$ !loa((.te,plate*xlsx.); "a$_(%,p($objPHPExcel !getActi"eS#eet() !getSt-le(.A@.) !get=%,be$<o$,at() !get<o$,atCo(e()); "'vance' u er may )in' it )a ter to in pect t#e num5er )ormat co'e 'irectly 5y renamin& template.xl x to template.4ip7 un4ippin&7 an' loo.in& )or t#e relevant piece o) ZM: co'e #ol'in& t#e num5er )ormat co'e in xl$styles.xml.
4.>.2-.
:et>
2(
4.>.21.
4.>.22.
8n PHPExcel it i ea y to apply variou 5or'er on a rectan&ular election. Here i #ow to apply a t#ic. re' 5or'er outline aroun' cell B2,a+.
$st-leA$$a-'>'a$$a-( .bo$(e$s.'>!'a$$a-( .o%tline.'>!'a$$a-( .st-le.'>!'PHPExcel_St-le_1o$(e$::14R9ER_3HIC;: .colo$.'>!'a$$a-(.a$gb.'>!'.<<<<6666.): ): ): ); $objIo$Js#eet !getSt-le(.15:KC.) !appl-<$o,A$$a-($st-leA$$a-);
8n Micro o)t I))ice Excel7 t#e a5ove operation woul' corre pon' to electin& t#e cell B2,a+7 launc#in& t#e tyle 'ialo&7 c#oo in& a t#ic. re' 5or'er7 an' clic.in& on t#e @Iutline@ 5or'er component.
Aote t#at t#e 5or'er outline i applie' to t#e rectan&ular election B2,a+ a a w#ole7 not on eac# cell in'ivi'ually.
9ou can ac#ieve any 5or'er e))ect 5y u in& Xu t t#e ( 5a ic 5or'er an' operatin& on a in&le cell at a time, Arra- ,ele)t ri&#t top 5ottom 'ia&onal (aps to propert&et:e)t01 &et*i&#t01 &et2op01 &etBottom01 &etDia&onal01
"''itional #ortcut 5or'er come in #an'y li.e in t#e example a5ove. 2#e e are t#e #ortcut 5or'er availa5le, Arra- ,eall5or'er outline in i'e vertical #ori4ontal (aps to propert&et"llBor'er 01 &etIutline01 &et8n i'e01 &et-ertical01 &etHori4ontal01
"n overview o) all 5or'er #ortcut can 5e een in t#e )ollowin& ima&e,
2$
8) you imultaneou ly et e.&. all5or'er an' vertical7 t#en we #ave @overlappin&@ 5or'er 7 an' one o) t#e component #a to win over t#e ot#er w#ere t#ere i 5or'er overlap. 8n PHPExcel7 )rom wea.e t to tron&e t 5or'er 7 t#e li t i a )ollow , all5or'er 7 outline?in i'e7 vertical?#ori4ontal7 le)t?ri&#t?top?5ottom?'ia&onal. 2#i 5or'er #ierarc#y can 5e utili4e' to ac#ieve variou e))ect in an ea y manner.
4.>.29.
" cell can 5e )ormatte' con'itionally7 5a e' on a peci)ic rule. /or example7 one can et t#e )ore&roun' colour o) a cell to re' i) it value i 5elow 4ero7 an' to &reen i) it value i 4ero or more. Ine can et a con'itional tyle rule et to a cell u in& t#e )ollowin& co'e,
$objCon(itional@'>'ne2'PHPExcel_St-le_Con(itional(); $objCon(itional@ !setCon(ition3-pe(PHPExcel_St-le_Con(itional::C4=9I3I4=_CELLIS); $objCon(itional@ !set4pe$ato$3-pe(PHPExcel_St-le_Con(itional::4PERA34R_LESS3HA=); $objCon(itional@ !a((Con(ition(.6.); $objCon(itional@ !getSt-le() !get<ont() !getColo$() !setARK1(PHPExcel_St-le_Colo$::C4L4R_RE9); $objCon(itional@ !getSt-le() !get<ont() !set1ol((t$%e); $objCon(itional5'>'ne2'PHPExcel_St-le_Con(itional(); $objCon(itional5 !setCon(ition3-pe(PHPExcel_St-le_Con(itional::C4=9I3I4=_CELLIS); $objCon(itional5 !set4pe$ato$3-pe(PHPExcel_St-le_Con(itional::4PERA34R_KREA3ER3HA=4REVFAL); $objCon(itional5 !a((Con(ition(.6.); $objCon(itional5 !getSt-le() !get<ont() !getColo$() !setARK1(PHPExcel_St-le_Colo$::C4L4R_KREE=); $objCon(itional5 !getSt-le() !get<ont() !set1ol((t$%e); $con(itionalSt-les'>'$objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !getCon(itionalSt-les(); a$$a-_p%s#($con(itionalSt-les:'$objCon(itional@); a$$a-_p%s#($con(itionalSt-les:'$objCon(itional5); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !setCon(itionalSt-les($con(itionalSt-les);
8) you want to copy t#e rule et to ot#er cell 7 you can 'uplicate t#e tyle o5Xect,
$objPHPExcel !getActi"eS#eet() !(%plicateSt-le('$objPHPExcel !getActi"eS#eet() !getSt-le(.15.):'.1U:17.');
4.>.24.
2o a'' a comment to a cell7 u e t#e )ollowin& co'e. 2#e example 5elow a'' a comment to cell E11,
27
$objPHPExcel !getActi"eS#eet() !getCo,,ent(.E@@.) !setA%t#o$(.PHPExcel.); $objCo,,entRic#3ext'>'$objPHPExcel !getActi"eS#eet() !getCo,,ent(.E@@.) !get3ext() !c$eate3extR%n(.PHPExcel:.); $objCo,,entRic#3ext !get<ont() !set1ol((t$%e); $objPHPExcel !getActi"eS#eet() !getCo,,ent(.E@@.) !get3ext() !c$eate3extR%n(/H$Hn/); $objPHPExcel !getActi"eS#eet() !getCo,,ent(.E@@.) !get3ext() !c$eate3extR%n(.3otal' a,o%nt'on't#e'c%$$ent'in"oice:'excl%(ing'VA3*.);
4.>.2=.
(a,e sure that -ou al/a-s include the co&plete %ilter ran0e: Excel 'oe upport ettin& only t#e caption row7 5ut t#atH not a 5e t practice...
4.>.2>.
Excel o))er 3 level o) JprotectionK, 'ocument ecurity7 #eet ecurity an' cell ecurity. B Document ecurity allow you to et a pa wor' on a complete prea' #eet7 allowin& c#an&e to 5e ma'e only w#en t#at pa wor' i entere'. B For. #eet ecurity o))er ot#er ecurity option , you can 'i allow in ertin& row on a peci)ic #eet7 'i allow ortin&7 Y B Cell ecurity o))er t#e option to loc.?unloc. a cell a well a #ow?#i'e t#e internal )ormula "n example on ettin& 'ocument ecurity,
$objPHPExcel !getSec%$it-() !setLocJIin(o2s(t$%e); $objPHPExcel !getSec%$it-() !setLocJSt$%ct%$e(t$%e); $objPHPExcel !getSec%$it-() !setIo$JbooJPass2o$((/PHPExcel/);
2+
(a,e sure -ou ena+le /or,sheet protection i% -ou need an- o% the /or,sheet protection %eatures: 2#i can 5e 'one u in& t#e )ollowin& co'e, qo5XPHPExcelB^&et"ctive<#eet01B^&etProtection01B^ et<#eet0true1[
4.>.2..
Data vali'ation i a power)ul )eature o) Excel2007. 8t allow to peci)y an input )ilter on t#e 'ata t#at can 5e in erte' in a peci)ic cell. 2#i )ilter can 5e a ran&e 0i.e. value mu t 5e 5etween 0 an' 1017 a li t 0i.e. value mu t 5e pic.e' )rom a li t17 Y 2#e )ollowin& piece o) co'e only allow num5er 5etween 10 an' 20 to 5e entere' in cell B3,
$objVali(ation'>'$objPHPExcel !getActi"eS#eet() !getCell(.1U.) !get9ataVali(ation(); $objVali(ation !set3-pe('PHPExcel_Cell_9ataVali(ation::3WPE_IH4LE'); $objVali(ation !setE$$o$St-le('PHPExcel_Cell_9ataVali(ation::S3WLE_S34P'); $objVali(ation !setAllo21lanJ(t$%e); $objVali(ation !setS#o2Inp%t0essage(t$%e); $objVali(ation !setS#o2E$$o$0essage(t$%e); $objVali(ation !setE$$o$3itle(.Inp%t'e$$o$.); $objVali(ation !setE$$o$(.=%,be$'is'not'allo2e(E.); $objVali(ation !setP$o,pt3itle(.Allo2e('inp%t.); $objVali(ation !setP$o,pt(.4nl-'n%,be$s'bet2een'@6'an('56'a$e'allo2e(*.); $objVali(ation !set<o$,%la@(@6); $objVali(ation !set<o$,%la5(56);
2#e )ollowin& piece o) co'e only allow an item pic.e' )rom a li t o) 'ata to 5e entere' in cell B3,
$objVali(ation'>'$objPHPExcel !getActi"eS#eet() !getCell(.1O.) !get9ataVali(ation(); $objVali(ation !set3-pe('PHPExcel_Cell_9ataVali(ation::3WPE_LIS3'); $objVali(ation !setE$$o$St-le('PHPExcel_Cell_9ataVali(ation::S3WLE_I=<4R0A3I4='); $objVali(ation !setAllo21lanJ(&alse); $objVali(ation !setS#o2Inp%t0essage(t$%e); $objVali(ation !setS#o2E$$o$0essage(t$%e); $objVali(ation !setS#o29$op9o2n(t$%e); $objVali(ation !setE$$o$3itle(.Inp%t'e$$o$.); $objVali(ation !setE$$o$(.Val%e'is'not'in'list*.); $objVali(ation !setP$o,pt3itle(.PicJ'&$o,'list.); $objVali(ation !setP$o,pt(.Please'picJ'a'"al%e'&$o,'t#e'($op (o2n'list*.); $objVali(ation !set<o$,%la@(./Ite,'A:Ite,'1:Ite,'C/.);
F#en u in& a 'ata vali'ation li t li.e a5ove7 ma.e ure you put t#e li t 5etween @ an' @ an' t#at you plit t#e item wit# a comma 071. 8t i important to remem5er t#at any trin& participatin& in an Excel )ormula i allowe' to 5e maximum 2(( c#aracter 0not 5yte 1. 2#i et a limit on #ow many item you can #ave in t#e trin& @8tem "78tem B78tem C@. 2#ere)ore it i normally a 5etter i'ea to type t#e item value 'irectly in ome cell ran&e7 ay "1,"37 an' in tea' u e7 ay7 qo5X-ali'ationB^ et/ormula10H<#eet_q"q1,q"q3H1[. "not#er 5ene)it i t#at t#e item value t#em elve can contain t#e comma r7> c#aracter it el).
8) you nee' 'ata vali'ation on multiple cell 7 one can clone t#e rule et,
$objPHPExcel !getActi"eS#eet() !getCell(.1C.) !set9ataVali(ation(clone' $objVali(ation);
4.>.2C.
8) you want PHPExcel to per)orm an automatic wi't# calculation7 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.
$objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.1.) !setA%toSiBe(t$%e);
29
2#e mea ure )or column wi't# in PHPExcel 'oe not corre pon' exactly to t#e mea ure you may 5e u e' to in Micro o)t I))ice Excel. Column wi't# are 'i))icult to 'eal wit# in Excel7 an' t#ere are everal mea ure )or t#e column wi't#. 11 ;nner /idth in character units 0e.&. +.%3 t#i i pro5a5ly w#at you are )amiliar wit# in Excel1 21 $ull /idth in pixels 0e.&. $% pixel 1 31 $ull /idth in character units 0e.&. 9.1%0$2(7 value B1 in'icate un et wi't#1 PHPExcel al/a-s operates /ith <) =$ull /idth in character units= w#ic# i in )act t#e only value t#at i tore' in any Excel )ile7 #ence t#e mo t relia5le mea ure. Gn)ortunately7 (icroso%t "%%ice Excel does not present -ou /ith this &easure . 8n tea' mea ure 11 an' 21 are compute' 5y t#e application w#en t#e )ile i opene' an' t#e e value are pre ente' in variou 'ialo&ue an' tool tip . 2#e c#aracter wi't# unit i t#e wi't# o) a H0H 04ero1 &lyp# in t#e wor.5oo. 'e)ault )ont. 2#ere)ore column wi't# mea ure' in c#aracter unit in two 'i))erent wor.5oo. can only 5e compare' i) t#ey #ave t#e ame 'e)ault wor.5oo. )ont. 8) you #ave ome Excel )ile an' nee' to .now t#e column wi't# in mea ure 317 you can rea' t#e Excel )ile wit# PHPExcel an' ec#o t#e retrieve' value .
4.>.2D.
"o(/"i!e a column
2o et a wor. #eet> column vi i5ility7 you can u e t#e )ollowin& co'e. 2#e )ir t line explicitly #ow t#e column C7 t#e econ' line #i'e column D.
$objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.C.) !setVisible(t$%e); $objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.9.) !setVisible(&alse);
4.>.9-.
Froup/outline a column
9ou can al o collap e t#e column. Aote t#at you #oul' al o et t#e column invi i5le7 ot#erwi e t#e collap e will not 5e vi i5le in Excel 2007.
$objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.E.) !setCollapse((t$%e); $objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.E.) !setVisible(&alse);
Plea e re)er to t#e part J&roup?outline a rowK )or a complete example on collap in&. 9ou can in truct PHPExcel to a'' a ummary to t#e ri&#t 0'e)ault17 or to t#e le)t. 2#e )ollowin& co'e a'' t#e ummary to t#e le)t,
$objPHPExcel !getActi"eS#eet() !setS#o2S%,,a$-Rig#t(&alse);
4.>.91.
4.>.92.
"o(/"i!e a ro(
2o et a wor. #eet> row vi i5ility7 you can u e t#e )ollowin& co'e. 2#e )ollowin& example #i'e row num5er 10.
$objPHPExcel !getActi"eS#eet() !getRo29i,ension(.@6.) !setVisible(&alse);
30
Aote t#at i) you apply active )ilter u in& an "uto/ilter7 t#en t#i will overri'e any row t#at you #i'e or un#i'e manually wit#in t#at "uto/ilter ran&e i) you ave t#e )ile.
4.>.99.
Froup/outline a ro(
9ou can al o collap e t#e row. Aote t#at you #oul' al o et t#e row invi i5le7 ot#erwi e t#e collap e will not 5e vi i5le in Excel 2007.
$objPHPExcel !getActi"eS#eet() !getRo29i,ension(.O.) !setCollapse((t$%e); $objPHPExcel !getActi"eS#eet() !getRo29i,ension(.O.) !setVisible(&alse);
$objPHPExcel !getActi"eS#eet() !getRo29i,ension($i) !set4%tlineLe"el(@); $objPHPExcel !getActi"eS#eet() !getRo29i,ension($i) !setVisible(&alse); + $objPHPExcel !getActi"eS#eet() !getRo29i,ension(C@) !setCollapse((t$%e);
9ou can in truct PHPExcel to a'' a ummary 5elow t#e collap i5le row 0'e)ault17 or a5ove. 2#e )ollowin& co'e a'' t#e ummary a5ove,
$objPHPExcel !getActi"eS#eet() !setS#o2S%,,a$-1elo2(&alse);
4.>.94.
Mer)e/unmer)e cells
8) you #ave a 5i& piece o) 'ata you want to 'i play in a wor. #eet7 you can mer&e two or more cell to&et#er7 to 5ecome one cell. 2#i can 5e 'one u in& t#e )ollowin& co'e,
$objPHPExcel !getActi"eS#eet() !,e$geCells(.A@C:E55.);
4.>.9=.
;nsertin) ro(s/columns
9ou can in ert?remove row ?column at a peci)ic po ition. 2#e )ollowin& co'e in ert 2 new row 7 ri&#t 5e)ore row 7,
$objPHPExcel !getActi"eS#eet() !inse$t=e2Ro21e&o$e(7:'5);
4.>.9>.
" 'rawin& i alway repre ente' a a eparate o5Xect7 w#ic# can 5e a''e' to a wor. #eet. 2#ere)ore7 you mu t )ir t in tantiate a new PHPExcel_Io$Js#eet_9$a2ing7 an' a i&n it propertie a meanin&)ul value,
$obj9$a2ing'>'ne2'PHPExcel_Io$Js#eet_9$a2ing(); $obj9$a2ing !set=a,e(.Logo.); $obj9$a2ing !set9esc$iption(.Logo.); $obj9$a2ing !setPat#(.*?i,ages?o&&icelogo*jpg.); $obj9$a2ing !setHeig#t(UD);
2o a'' t#e a5ove 'rawin& to t#e wor. #eet7 u e t#e )ollowin& nippet o) co'e. PHPExcel create t#e lin. 5etween t#e 'rawin& an' t#e wor. #eet,
$obj9$a2ing !setIo$Js#eet($objPHPExcel !getActi"eS#eet());
31
9ou can al o a'' ima&e create' u in& aD )unction wit#out nee'in& to ave t#em to 'i . )ir t a 8nB Memory 'rawin& .
??''Fse'K9'to'c$eate'an'in ,e,o$-'i,age $g(I,age'>'ei,agec$eatet$%ecolo$(@56:'56)'o$'(ie(.Cannot'InitialiBe'ne2'K9'i,age' st$ea,.); $textColo$'>'i,agecolo$allocate($g(I,age:'5OO:'5OO:'5OO); i,agest$ing($g(I,age:'@:'O:'O:''.C$eate('2it#'PHPExcel.:'$textColo$); ??''A(('t#e'In 0e,o$-'i,age'to'a'2o$Js#eet $obj9$a2ing'>'ne2'PHPExcel_Io$Js#eet_0e,o$-9$a2ing(); $obj9$a2ing !set=a,e(.In 0e,o$-'i,age'@.); $obj9$a2ing !set9esc$iption(.In 0e,o$-'i,age'@.); $obj9$a2ing !setCoo$(inates(.A@.); $obj9$a2ing !setI,ageReso%$ce($g(I,age); $obj9$a2ing !setRen(e$ing<%nction( ''''PHPExcel_Io$Js#eet_0e,o$-9$a2ing::RE=9ERI=K_fPEK ); $obj9$a2ing !set0i,e3-pe(PHPExcel_Io$Js#eet_0e,o$-9$a2ing::0I0E3WPE_9E<AFL3); $obj9$a2ing !setHeig#t(UD); $obj9$a2ing !setIo$Js#eet($objPHPExcel !getActi"eS#eet());
4.>.9..
" commonly a .e' !ue tion i #ow to retrieve t#e ima&e )rom a wor.5oo. t#at #a 5een loa'e'7 an' ave t#em a in'ivi'ual ima&e )ile to 'i .. 2#e )ollowin& co'e extract ima&e )rom t#e current active wor. #eet7 an' write eac# a a eparate )ile.
$i'>'6; &o$eac#'($objPHPExcel !getActi"eS#eet() !get9$a2ingCollection()'as'$($a2ing)') i&'($($a2ing'instanceo&'PHPExcel_Io$Js#eet_0e,o$-9$a2ing)') ob_sta$t(); call_%se$_&%nc( $($a2ing !getRen(e$ing<%nction(): $($a2ing !getI,ageReso%$ce() ); $i,ageContents'>'ob_get_contents(); ob_en(_clean(); s2itc#'($($a2ing !get0i,e3-pe())') case'PHPExcel_Io$Js#eet_0e,o$-9$a2ing::0I0E3WPE_P=K': $extension'>'.png.;'b$eaJ; case'PHPExcel_Io$Js#eet_0e,o$-9$a2ing::0I0E3WPE_KI<: $extension'>'.gi&.;'b$eaJ; case'PHPExcel_Io$Js#eet_0e,o$-9$a2ing::0I0E3WPE_fPEK': $extension'>'.jpg.;'b$eaJ; + +'else') $BipRea(e$'>'&open($($a2ing !getPat#():.$.); $i,ageContents'>'..; 2#ile'(E&eo&($BipRea(e$))') $i,ageContents'*>'&$ea(($BipRea(e$:@65R); + &close($BipRea(e$); $extension'>'$($a2ing !getExtension(); + $,-<ile=a,e'>'.66_I,age_.*PP$i*.*.*$extension;
32
&ile_p%t_contents($,-<ile=a,e:$i,ageContents); +
4.>.9C.
"''in& ric# text to a cell can 5e 'one u in& PHPExcel_Ric#3ext in tance . Here> an example7 w#ic# create t#e )ollowin& ric# text trin&, 2#i invoice i payable within thirty days after the end of the month unle on t#e invoice.
$objRic#3ext'>'ne2'PHPExcel_Ric#3ext(); $objRic#3ext !c$eate3ext(.3#is'in"oice'is'.); $objPa-able'>'$objRic#3ext !c$eate3extR%n(.pa-able'2it#in't#i$t-'(a-s'a&te$'t#e'en(' o&'t#e',ont#.); $objPa-able !get<ont() !set1ol((t$%e); $objPa-able !get<ont() !setItalic(t$%e); $objPa-able !get<ont() !setColo$('ne2' PHPExcel_St-le_Colo$('PHPExcel_St-le_Colo$::C4L4R_9AR;KREE=')'); $objRic#3ext !c$eate3ext(.:'%nless'speci&ie('ot#e$2ise'on't#e'in"oice*.); $objPHPExcel !getActi"eS#eet() !getCell(.A@C.) !setVal%e($objRic#3ext);
peci)ie' ot#erwi e
4.>.9D.
PHPExcel upport t#e 'e)inition o) name' ran&e . 2#e e can 5e 'e)ine' u in& t#e )ollowin& co'e,
??'A(('so,e'(ata $objPHPExcel !setActi"eS#eetIn(ex(6); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.A@.:'.<i$stna,e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.A5.:'.Lastna,e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1@.:'.0aa$ten.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.15.:'.1allia%2.); ??'9e&ine'na,e('$anges $objPHPExcel !a((=a,e(Range('ne2'PHPExcel_=a,e(Range(.Pe$son<=.:'$objPHPExcel !getActi"eS#eet():'.1@.)'); $objPHPExcel !a((=a,e(Range('ne2'PHPExcel_=a,e(Range(.Pe$sonL=.:'$objPHPExcel !getActi"eS#eet():'.15.)');
Iptionally7 a )ourt# parameter can 5e pa e' 'e)inin& t#e name' ran&e local 0i.e. only u a5le on t#e current wor. #eet1. Aame' ran&e are &lo5al 5y 'e)ault.
4.>.4-.
<ometime 7 one really want to output a )ile to a client> 5row er7 e pecially w#en creatin& prea' #eet onBt#eB)ly. 2#ere are ome ea y tep t#at can 5e )ollowe' to 'o t#i , 1. Create your PHPExcel prea' #eet 2. Iutput H22P #ea'er )or t#e type o) 'ocument you wi # to output 3. G e t#e PHPExcel3Friter36 o) your c#oice7 an' ave to Jp#p,??outputK PHPExcel3Friter3Excel2007 u e temporary tora&e w#en writin& to p#p,??output. By 'e)ault7 temporary )ile are tore' in t#e cript> wor.in& 'irectory. F#en t#ere i no acce 7 it )all 5ac. to t#e operatin& y tem> temporary )ile location.
5his &a- not +e sa%e %or unauthorized 7ie/in0: Depen'in& on t#e con)i&uration o) your operatin& y tem7 temporary tora&e can 5e rea' 5y anyone u in& t#e ame temporary tora&e )ol'er. F#en con)i'entiality o) your 'ocument i nee'e'7 it i recommen'e' not to u e p#p,??output.
33
H&&P "ea!ers
Example o) a cript re'irectin& an Excel 2007 )ile to t#e clientH 5row er,
GNp#p ?A'He$e't#e$e'2ill'be'so,e'co(e'2#e$e'-o%'c$eate'$objPHPExcel'A? ??'$e(i$ect'o%tp%t'to'client'b$o2se$ #ea(e$(.Content 3-pe:'application?"n(*openx,l&o$,ats o&&ice(oc%,ent*sp$ea(s#eet,l*s#eet.); #ea(e$(.Content 9isposition:'attac#,ent;&ilena,e>/,-&ile*xlsx/.); #ea(e$(.Cac#e Cont$ol:',ax age>6.); $objI$ite$'>'PHPExcel_I4<acto$-::c$eateI$ite$($objPHPExcel:'.Excel5667.); $objI$ite$ !sa"e(.p#p:??o%tp%t.);' N!
Caution, Ma.e ure not to inclu'e any ec#o tatement or output any ot#er content t#an t#e Excel )ile. 2#ere #oul' 5e no w#ite pace 5e)ore t#e openin& ]Cp#p ta& an' at mo t one line 5rea. a)ter t#e clo in& C^ ta& 0w#ic# can al o 5e omitte' to avoi' pro5lem 1. Ma.e ure t#at your cript i ave' wit#out a BIM 0ByteBor'er mar.1. 0Becau e t#i count a ec#oin& output1 <ame t#in& apply to all inclu'e' )ile /ailin& to )ollow t#e a5ove &ui'eline may re ult in corrupt Excel )ile arrivin& at t#e client 5row er7 an'?or t#at #ea'er cannot 5e et 5y PHP 0re ultin& in warnin& me a&e 1.
4.>.41.
4.>.42.
4.>.49.
2#ere mi&#t 5e a ituation w#ere you want to &enerate an inBmemory ima&e u in& aD an' a'' it to a PHPExcel wor. #eet wit#out )ir t #avin& to ave t#i )ile to a temporary location. Here> an example w#ic# &enerate an ima&e in memory an' a'' it to t#e active wor. #eet,
??'Kene$ate'an'i,age $g(I,age'>'ei,agec$eatet$%ecolo$(@56:'56)'o$'(ie(.Cannot'InitialiBe'ne2'K9'i,age' st$ea,.); $textColo$'>'i,agecolo$allocate($g(I,age:'5OO:'5OO:'5OO); i,agest$ing($g(I,age:'@:'O:'O:''.C$eate('2it#'PHPExcel.:'$textColo$);
3%
??'A(('a'($a2ing'to't#e'2o$Js#eet $obj9$a2ing'>'ne2'PHPExcel_Io$Js#eet_0e,o$-9$a2ing(); $obj9$a2ing !set=a,e(.Sa,ple'i,age.); $obj9$a2ing !set9esc$iption(.Sa,ple'i,age.); $obj9$a2ing !setI,ageReso%$ce($g(I,age); $obj9$a2ing !setRen(e$ing<%nction(PHPExcel_Io$Js#eet_0e,o$-9$a2ing::RE=9ERI=K_fPEK); $obj9$a2ing !set0i,e3-pe(PHPExcel_Io$Js#eet_0e,o$-9$a2ing::0I0E3WPE_9E<AFL3); $obj9$a2ing !setHeig#t(UD); $obj9$a2ing !setIo$Js#eet($objPHPExcel !getActi"eS#eet());
4.>.44.
4.>.4=.
<ometime you want to et a color )or #eet ta5. /or example you can #ave a re' #eet ta5,
$objIo$Js#eet !get3abColo$() !setRK1(.<<6666.);
4.>.4>.
8) you nee' to create more wor. #eet in t#e wor.5oo.7 #ere i #ow,
$objIo$Js#eet@'>'$objPHPExcel !c$eateS#eet(); $objIo$Js#eet@ !set3itle(.Anot#e$'s#eet.); 2#in. o) create<#eet01 a t#e @8n ert #eet@ 5utton in Excel. F#en you #it t#at 5utton a new #eet i appen'e' to t#e exi tin& collection o) wor. #eet in t#e wor.5oo..
4.>.4..
<ometime you may even want t#e wor. #eet to 5e >7er- hidden?. 2#e availa5le #eet tate are ,
PHPExcel_Io$Js#eet::SHEE3S3A3E_VISI1LE PHPExcel_Io$Js#eet::SHEE3S3A3E_HI99E= PHPExcel_Io$Js#eet::SHEE3S3A3E_VERWHI99E= 8n Excel t#e #eet tate Jvery #i''enK can only 5e et pro&rammatically7 e.&. wit# -i ual Ba ic Macro. 8t i not po i5le to ma.e uc# a #eet vi i5le via t#e u er inter)ace.
4.>.4C.
6i)"tGtoGle,t (or%s"eet
For. #eet can 5e et in'ivi'ually w#et#er column r"> #oul' tart at le)t or ri&#t i'e. De)ault i le)t. Here i #ow to et column )rom ri&#tBtoBle)t.
??'$ig#t to le&t'2o$Js#eet $objPHPExcel !getActi"eS#eet() !setRig#t3oLe&t(t$%e);
3(
=.
#.1.
" PHPExcel repre ent an inBmemory prea' #eet7 it al o o))er )ormula calculation capa5ilitie . " cell can 5e o) a value type 0containin& a num5er or text17 or a )ormula type 0containin& a )ormula w#ic# can 5e evaluate'1. /or example7 t#e )ormula @D<GM0"1,"101@ evaluate to t#e um o) value in "17 "27 ...7 "10. 2o calculate a )ormula7 you can call t#e cell containin& t#e )ormula> met#o' getCalc%late(Val%e()7 )or example,
$objPHPExcel !getActi"eS#eet() !getCell(.E@@.) !getCalc%late(Val%e();
8) you write t#e )ollowin& line o) co'e in t#e invoice 'emo inclu'e' wit# PHPExcel7 it evaluate to t#e value @$%@,
"not#er nice )eature o) PHPExcelH )ormula par er7 i t#at it can automatically a'Xu t a )ormula w#en in ertin&?removin& row ?column . HereH an example,
9ou ee t#at t#e )ormula containe' in cell E11 i @<GM0E%,E91@. Aow7 w#en 8 write t#e )ollowin& line o) co'e7 two new pro'uct line are a''e',
$objPHPExcel !getActi"eS#eet() !inse$t=e2Ro21e&o$e(7:'5);
3$
Di' you noticeC 2#e )ormula in t#e )ormer cell E11 0now E137 a 8 in erte' 2 new row 17 c#an&e' to @<GM0E%,E111@. "l o7 t#e in erte' cell 'uplicate tyle in)ormation o) t#e previou cell7 Xu t li.e ExcelH 5e#aviour. Aote t#at you can 5ot# in ert row an' column .
#.2.
/nown limitations
2#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 5e)ore 5ein& execute'. 2#i mean t#at Excel )ormula calculation i u5Xect to PHP> lan&ua&e c#aracteri tic .
=.2.1.
Operator prece!ence
8n Excel HpH win over HEH7 Xu t li.e H6H win over HpH in or'inary al&e5ra. 2#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,?? upport.micro o)t.com?.5?2(1+9 *e)erence )or operator prece'ence in PHP, #ttp,??www.p#p.net?operator
=.2.2.
/ormula involvin& num5er an' text may pro'uce unexpecte' re ult or even unrea'a5le )ile content . /or example7 t#e )ormula HD3p@Hello @H i expecte' to pro'uce an error in Excel 0o-":GE_1. Due to t#e )act t#at PHP convert JHelloK to a numeric value 04ero17 t#e re ult o) t#i )ormula i evaluate' a 3 in tea' o) evaluatin& a an error. 2#i al o cau e t#e Excel 'ocument 5ein& &enerate' a containin& unrea'a5le content. *e)erence )or t#i 5e#aviour in PHP, #ttp,??5e.p#p.net?manual?en?lan&ua&e.type . trin&.p#polan&ua&e.type . trin&.conver ion
37
>.
" you alrea'y .now )rom part 3.% *ea'er an' writer 7 rea'in& an' writin& to a per i te' tora&e i not po i5le u in& t#e 5a e PHPExcel cla e . /or t#i purpo e7 PHPExcel provi'e rea'er an' writer 7 w#ic# are implementation o) PHPExcel3Friter38*ea'er an' PHPExcel3Friter38Friter.
..1.
&'&()cel0I1$actor
2#e PHPExcel "P8 o))er multiple met#o' to create a PHPExcel3Friter38*ea'er or PHPExcel3Friter38Friter in tance, Direct creation -ia PHPExcel38I/actory "ll example un'erneat# 'emon trate t#e 'irect creation met#o'. Aote t#at you can al o u e t#e PHPExcel38I/actory cla to 'o t#i .
>.1.1.
2#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 PHPExcel3*ea'er38*ea'er 'i tri5ute' wit# PHPExcel. 8) one o) t#em can loa' t#e peci)ie' )ile name7 t#e )ile i loa'e' u in& t#at PHPExcel3*ea'er38*ea'er. Explicit mo'e re!uire you to peci)y w#ic# PHPExcel3*ea'er38*ea'er #oul' 5e u e'. 9ou can create a PHPExcel3*ea'er38*ea'er in tance u in& PHPExcel38I/actory in automatic )ile type re olvin& mo'e u in& t#e )ollowin& co'e ample,
$objPHPExcel'>'PHPExcel_I4<acto$-::loa((/6O&eat%$e(e,o*xlsx/);
" typical u e o) t#i )eature i w#en you nee' to rea' )ile uploa'e' 5y your u er 7 an' you 'on>t .now w#et#er t#ey are uploa'in& xl or xl x )ile . 8) you nee' to et ome propertie on t#e rea'er7 0e.&. to only rea' 'ata7 ee more a5out t#i later17 t#en you may in tea' want to u e t#i variant,
$objRea(e$'>'PHPExcel_I4<acto$-::c$eateRea(e$<o$<ile(/6O&eat%$e(e,o*xlsx/); $objRea(e$ !setRea(9ata4nl-(t$%e); $objRea(e$ !loa((/6O&eat%$e(e,o*xlsx/);
9ou can create a PHPExcel3*ea'er38*ea'er in tance u in& PHPExcel38I/actory in explicit mo'e u in& t#e )ollowin& co'e ample,
$objRea(e$'>'PHPExcel_I4<acto$-::c$eateRea(e$(/Excel5667/); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*xlsx/);
>.1.2.
..2.
Excel2007 )ile )ormat i t#e main )ile )ormat o) PHPExcel. 8t allow outputtin& t#e inBmemory prea' #eet to a .xl x )ile.
3+
>.2.1.
PHPExcel/6ea!er/Excel2--.
6ea!in) a sprea!s"eet
9ou can rea' an .xl x )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_Excel5667(); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*xlsx/);
>.2.2.
PHPExcel/0riter/Excel2--.
0ritin) a sprea!s"eet
9ou can write an .xl x )ile u in& t#e )ollowin& co'e,
$objI$ite$'>'ne2'PHPExcel_I$ite$_Excel5667($objPHPExcel); $objI$ite$ !sa"e(/6O&eat%$e(e,o*xlsx/);
#ormula preGcalculation
By 'e)ault7 t#i writer preBcalculate all )ormula in t#e prea' #eet. 2#i can 5e low on lar&e prea' #eet 7 an' may5e even unwante'. 9ou can #owever 'i a5le )ormula preBcalculation,
$objI$ite$'>'ne2'PHPExcel_I$ite$_Excel5667($objPHPExcel); $objI$ite$ !setP$eCalc%late<o$,%las(&alse); $objI$ite$ !sa"e(/6O&eat%$e(e,o*xlsx/);
39
"%%ice200< co&pati+ilit- should onl- +e used /hen needed I))ice2003 compati5ility option #oul' only 5e u e' w#en nee'e'. 2#i option 'i a5le everal I))ice2007 )ile )ormat option 7 re ultin& in a lowerB)eature' I))ice2007 prea' #eet w#en t#i option i u e'.
..3.
Excel( )ile )ormat i t#e ol' Excel )ile )ormat7 implemente' in PHPExcel to provi'e a uni)orm manner to create 5ot# .xl x an' .xl )ile . 8t i 5a ically a mo'i)ie' ver ion o) PE"* <prea' #eet3Excel3Friter7 alt#ou&# it #a 5een exten'e' an' #a )ewer limitation an' more )eature t#an t#e ol' PE"* li5rary. 2#i can rea' all B8// ver ion t#at u e I:E2, B8//( 0intro'uce' wit# o))ice 9(1 t#rou&# B8//+7 5ut cannot rea' earlier ver ion . Excel( )ile )ormat will not 5e 'evelope' any )urt#er7 it Xu t provi'e an a''itional )ile )ormat )or PHPExcel.
Excel@ A1;$$) li&itations Plea e note t#at B8// )ile )ormat #a PHP.
ome limit re&ar'in& to tylin& cell an' #an'lin& lar&e prea' #eet via
>.9.1.
PHPExcel/6ea!er/Excel=
6ea!in) a sprea!s"eet
9ou can rea' an .xl )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_ExcelO(); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*xls/);
%0
??'Rea('title'$o2'an('$o2s'56' 'U6 i&'($$o2'>>'@'gg'($$o2'!>'56'aa'$$o2'G>'U6))') $et%$n't$%e; + $et%$n'&alse; + + $objRea(e$'>'ne2'PHPExcel_Rea(e$_ExcelO(); $objRea(e$ !setRea(<ilte$('ne2'0-Rea(<ilte$()'); $objPHPExcel'>'$objRea(e$ !loa((/6Dla$gescale*xls/);
>.9.2.
PHPExcel/0riter/Excel=
0ritin) a sprea!s"eet
9ou can write an .xl )ile u in& t#e )ollowin& co'e,
$objI$ite$'>'ne2'PHPExcel_I$ite$_ExcelO($objPHPExcel); $objI$ite$ !sa"e(/6O&eat%$e(e,o*xls/);
..4.
Excel 2003 ZM: )ile )ormat i a )ile )ormat w#ic# can 5e u e' in ol'er ver ion o) Micro o)t Excel.
Excel 200< '() li&itations Plea e note t#at Excel 2003 ZM: )ormat #a prea' #eet via PHP.
>.4.1.
PHPExcel/6ea!er/Excel2--9XML
6ea!in) a sprea!s"eet
9ou can rea' an .xml )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_Excel566U80L(); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*x,l/);
%1
..#.
<ym5olic :in. 0<9:M1 i a Micro o)t )ile )ormat typically u e' to exc#an&e 'ata 5etween application 7 peci)ically prea' #eet . <9:M )ile conventionally #ave a . l. u))ix. Compo e' o) only 'i playa5le "A<8 c#aracter 7 it can 5e ea ily create' an' proce e' 5y ot#er application 7 uc# a 'ata5a e .
BC)D li&itations Plea e note t#at <9:M )ile )ormat #a via PHP.
ome limit re&ar'in& to tylin& cell an' #an'lin& lar&e prea' #eet
>.=.1.
PHPExcel/6ea!er/ 7LH
6ea!in) a sprea!s"eet
9ou can rea' an . l. )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_SWL;(); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*slJ/);
....
Ipen I))ice or :i5re I))ice .o' )ile are t#e tan'ar' )ile )ormat )opr Ipen I))ice or :i5re I))ice Calc )ile .
>.>.1.
PHPExcel/6ea!er/OOCalc
6ea!in) a sprea!s"eet
9ou can rea' an .o' )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_44Calc(); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*o(s/);
class'0-Rea(<ilte$'i,ple,ents'PHPExcel_Rea(e$_IRea(<ilte$ ) p%blic'&%nction'$ea(Cell($col%,n:'$$o2:'$2o$Js#eet=a,e'>'..)') ??'Rea('title'$o2'an('$o2s'56' 'U6 i&'($$o2'>>'@'gg'($$o2'!>'56'aa'$$o2'G>'U6))') $et%$n't$%e; + $et%$n'&alse; + + $objRea(e$'>'ne2'PHPExcel_Rea(e$_44calc(); $objRea(e$ !setRea(<ilte$('ne2'0-Rea(<ilte$()'); $objPHPExcel'>'$objRea(e$ !loa((/6Dla$gescale*o(s/);
..3.
C<- 0Comma <eparate' -alue 1 are o)ten u e' a an import?export )ile )ormat wit# ot#er y tem . PHPExcel allow rea'in& an' writin& to C<- )ile .
>...1.
PHPExcel/6ea!er/C I
6ea!in) a C I ,ile
9ou can rea' a .c v )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_CSV(); $objPHPExcel'>'$objRea(e$ !loa((/sa,ple*cs"/);
ettin) C I options
I)ten7 C<- )ile are not really Jcomma eparate'K7 or u e emicolon 0[1 a a eparator. 9ou can in truct PHPExcel3*ea'er3C<- ome option 5e)ore rea'in& a C<- )ile. Aote t#at PHPExcel3*ea'er3C<- 5y 'e)ault a ume t#at t#e loa'e' C<- )ile i G2/B+ enco'e'. 8) you are rea'in& C<- )ile t#at were create' in Micro o)t I))ice Excel t#e correct input enco'in& may rat#er 5e Fin'ow B12(2 0CP12(21. "lway ma.e ure t#at t#e input enco'in& i et appropriately.
$objRea(e$'>'ne2'PHPExcel_Rea(e$_CSV(); $objRea(e$ !setInp%tEnco(ing(.CP@5O5.); $objRea(e$ !set9eli,ite$(.;.); $objRea(e$ !setEnclos%$e(..); $objRea(e$ !setLineEn(ing(/H$Hn/); $objRea(e$ !setS#eetIn(ex(6); $objPHPExcel'>'$objRea(e$ !loa((/sa,ple*cs"/);
%3
>...2.
PHPExcel/0riter/C I
0ritin) a C I ,ile
9ou can write a .c v )ile u in& t#e )ollowin& co'e,
$objI$ite$'>'ne2'PHPExcel_I$ite$_CSV($objPHPExcel); $objI$ite$ !sa"e(/6O&eat%$e(e,o*cs"/);
ettin) C I options
I)ten7 C<- )ile are not really Jcomma eparate'K7 or u e emicolon 0[1 a a eparator. 9ou can in truct PHPExcel3Friter3C<- ome option 5e)ore writin& a C<- )ile,
$objI$ite$'>'ne2'PHPExcel_I$ite$_CSV($objPHPExcel); $objI$ite$ !set9eli,ite$(.;.); $objI$ite$ !setEnclos%$e(..); $objI$ite$ !setLineEn(ing(/H$Hn/); $objI$ite$ !setS#eetIn(ex(6); $objI$ite$ !sa"e(/6O&eat%$e(e,o*cs"/);
#ormula preGcalculation
By 'e)ault7 t#i writer preBcalculate all )ormula in t#e prea' #eet. 2#i can 5e low on lar&e prea' #eet 7 an' may5e even unwante'. 9ou can #owever 'i a5le )ormula preBcalculation,
$objI$ite$'>'ne2'PHPExcel_I$ite$_CSV($objPHPExcel); $objI$ite$ !setP$eCalc%late<o$,%las(&alse); $objI$ite$ !sa"e(/6O&eat%$e(e,o*cs"/);
Aote t#at t#e a5ove co'e et 'ecimal an' t#ou an' eparator a &lo5al option . 2#i al o a))ect #ow H2M: an' PD/ i exporte'.
%%
..<.
'%5L
PHPExcel allow you to rea' or write a prea' #eet a H2M: )ormat7 )or !uic. repre entation o) t#e 'ata in it to anyone w#o 'oe not #ave a prea' #eet application on t#eir PC7 or loa'in& )ile ave' 5y ot#er cript t#at imply create H2M: mar.up an' &ive it a .xl )ile exten ion.
>.C.1.
PHPExcel/6ea!er/H&ML
6ea!in) a sprea!s"eet
9ou can rea' an .#tml or .#tm )ile u in& t#e )ollowin& co'e,
$objRea(e$'>'ne2'PHPExcel_Rea(e$_H30L(); $objPHPExcel'>'$objRea(e$ !loa((/6O&eat%$e(e,o*#t,l/);
till experimental an' 'oe not yet upport mer&e' cell or ne te' ta5le
>.C.2.
PHPExcel/0riter/H&ML
Plea e note t#at PHPExcel3Friter3H2M: only output t#e )ir t wor. #eet 5y 'e)ault.
0ritin) a sprea!s"eet
9ou can write a .#tm )ile u in& t#e )ollowin& co'e,
$objI$ite$'>'ne2'PHPExcel_I$ite$_H30L($objPHPExcel); $objI$ite$ !sa"e(/6O&eat%$e(e,o*#t,/);
#ormula preGcalculation
By 'e)ault7 t#i writer preBcalculate all )ormula in t#e prea' #eet. 2#i can 5e low on lar&e prea' #eet 7 an' may5e even unwante'. 9ou can #owever 'i a5le )ormula preBcalculation,
$objI$ite$'>'ne2'PHPExcel_I$ite$_H30L($objPHPExcel); $objI$ite$ !setP$eCalc%late<o$,%las(&alse); $objI$ite$ !sa"e(/6O&eat%$e(e,o*#t,/);
%(
..=.
&>$
PHPExcel allow you to write a prea' #eet into PD/ )ormat7 )or )a t 'i tri5ution o) repre ente' 'ata.
>.D.1.
PHPExcel/0riter/PD#
PHPExcel> PD/ Friter i a wrapper )or a 3r'BParty PD/ *en'erin& li5rary uc# a tcPD/7 mPD/ or DomPD/. Prior to ver ion 1.7.+ o) PHPExcel7 t#e tcPD/ li5rary wa 5un'le' wit# PHPExcel[ 5ut )rom
%$
ver ion 1.7.+ t#i wa remove'. 8n tea'7 you mu t now in tall a PD/ *en'erin& li5rary your el)[ 5ut PHPExcel will wor. wit# a num5er o) 'i))erent li5rarie . Currently7 t#e )ollowin& li5rarie are upporte',
Do/nloada+le %ro&
#ttp,??www.tcp').or&? #ttp,??www.mp')1.com?mp')? #ttp,??co'e.&oo&le.com?p?'omp')?
2#e 'i))erent li5rarie #ave 'i))erent tren&t# an' wea.ne e . <ome &enerate 5etter )ormatte' output t#an ot#er 7 ome are )a ter or u e le memory t#an ot#er 7 w#ile ome &enerate maller .p') )ile . 8t i t#e 'eveloper c#oice w#ic# one t#ey wi # to u e7 appropriate to t#eir own circum tance . Be)ore in tantiatin& a Friter to &enerate PD/ output7 you will nee' to in'icate w#ic# *en'erin& li5rary you are u in&7 an' w#ere it i locate'.
$$en(e$e$=a,e'>'PHPExcel_Settings::P9<_RE=9ERER_0P9<; $$en(e$e$Lib$a$-'>'.,P9<O*R.; $$en(e$e$Lib$a$-Pat#'>'(i$na,e(__<ILE__)*.?**?**?**?lib$a$ies?P9<?.'*' $$en(e$e$Lib$a$-; i&'(EPHPExcel_Settings::setP(&Ren(e$e$( $$en(e$e$=a,e: $$en(e$e$Lib$a$-Pat# ))') (ie( .Please'set't#e'$$en(e$e$=a,e'an('$$en(e$e$Lib$a$-Pat#'"al%es.'* PHP_E4L'* .'as'app$op$iate'&o$'-o%$'(i$ecto$-'st$%ct%$e. ); +
0ritin) a sprea!s"eet
Ince you #ave i'enti)ie' t#e *en'erer t#at you wi # to u e )or PD/ &eneration7 you can write a .p') )ile u in& t#e )ollowin& co'e,
$objI$ite$'>'ne2'PHPExcel_I$ite$_P9<($objPHPExcel); $objI$ite$ !sa"e(/6O&eat%$e(e,o*p(&/);
Plea e note t#at PHPExcel3Friter3PD/ only output t#e )ir t wor. #eet 5y 'e)ault.
#ormula preGcalculation
By 'e)ault7 t#i writer preBcalculate all )ormula in t#e prea' #eet. 2#i can 5e low on lar&e prea' #eet 7 an' may5e even unwante'. 9ou can #owever 'i a5le )ormula preBcalculation,
$objI$ite$'>'ne2'PHPExcel_I$ite$_P9<($objPHPExcel); $objI$ite$ !setP$eCalc%late<o$,%las(&alse); $objI$ite$ !sa"e(/6O&eat%$e(e,o*p(&/);
%7
..12.
*ea'er an' writer are t#e tool t#at allow you to &enerate Excel )ile )rom template . 2#i re!uire le co'in& e))ort t#an &eneratin& t#e Excel )ile )rom cratc#7 e pecially i) your template #a many tyle 7 pa&e etup propertie 7 #ea'er etc. Here i an example #ow to open a template )ile7 )ill in a couple o) )iel' an' ave it a&ain,
$objPHPexcel'>'PHPExcel_I4<acto$-::loa((.te,plate*xlsx.); $objIo$Js#eet'>'$objPHPexcel !getActi"eS#eet(); $objIo$Js#eet !getCell(.A@.) !setVal%e(.fo#n.); $objIo$Js#eet !getCell(.A5.) !setVal%e(.S,it#.); $objI$ite$'>'PHPExcel_I4<acto$-::c$eateI$ite$($objPHPexcel:'.ExcelO.); $objI$ite$ !sa"e(.2$ite*xls.);
%+
..
Cre!its
Plea e re)er to t#e internet pa&e #ttp,??www.co'eplex.com?PHPExcel?Fi.i?-iew.a pxC titleDCre'it Ere)errin&2itleDHome )or upBtoB'ate cre'it .
%9
(0
rotation wrap #rin.2o/it in'ent PHPExcel_Bt-le_8u&+er$or&at "rray .ey, co'e PHPExcel_Bt-le_Protection "rray .ey, loc.e' #i''en
et2ext*otation01 etFrap2ext01 et<#rin.2o/it01 et8n'ent01 Map to property, et/ormatCo'e01 Map to property, et:oc.e'01 etHi''en01
(1