PHPExcel Developer Documentation

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 46

PHPExcel Developer Documentation

Author: Version: Date:

Maarten Balliauw 1.7.4 26 February 2011

1.

Contents

PHPExcel Developer Docu entation............................................................................1 1. !ontent".......................................................................................................... 2 2. Prere#ui"ite"..................................................................................................... $ $. %rc&itecture..................................................................................................... 6 4. !reatin' a "prea("&eet.........................................................................................) *. Per+or in' +or ula calculation"............................................................................$$ 6. ,ea(in' an( writin' to +ile...................................................................................$* 7. !re(it"........................................................................................................... 44 %ppen(ix %- .ali( array /ey" +or "tyle applyFro %rray01...................................................4*

PHPExcel Developer Docu entation

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 enable( 51 PHP exten"ion p&p3x l enable( PHP exten"ion p&p3'(2 enable( 0i+ not co pile( in1
*) php_zip i" only nee(e( by PHPExcel_Reader_Excel200 6 PHPExcel_!riter_Excel200 an( PHPExcel_Reader_""#alc. 7n ot&er wor("6 i+ you nee( PHPExcel to &an(le .xl"x or .o(" +ile" you will nee( t&e 4ip exten"ion6 but ot&erwi"e not. 8ou can re ove t&i" (epen(ency +or writin' Excel2007 +ile" 0not +or rea(in'1 by u"in' t&e P!9:ip library t&at i" bun(le( wit& PHPExcel. ;ee t&e F%< "ection o+ t&i" (ocu ent 02.4.21 +or (etail" about t&i". P!9:ip (oe" &ave a (epen(ency on PHP=" 4lib exten"ion bein' enable(.

2.2.

Installation instructions

7n"tallation i" #uite ea"y- copy t&e content" o+ t&e !la""e" +ol(er to any location in your application re#uire(.
Example: 7+ your web root +ol(er i" >var>www> you t&at +ol(er "o you en( up wit& +ile"ay want to create a "ub+ol(er calle( >var>www>!la""e"> an( copy t&e +ile" into

>var>www>!la""e">PHPExcel.p&p >var>www>!la""e">PHPExcel>!alculation.p&p >var>www>!la""e">PHPExcel>!ell.p&p ...

2.3.

Getting started
above "o you en( up wit&-

% 'oo( way to 'et "tarte( i" to run "o e o+ t&e te"t" inclu(e( in t&e (ownloa(.
!opy t&e ?2e"t"? +ol(er next to your ?!la""e"? +ol(er +ro >var>www>2e"t">01"i ple.p&p >var>www>2e"t">02type".p&p ...

;tart runnin' t&e te"t" by pointin' your brow"er to t&e te"t "cript"&ttp->>exa ple.co >2e"t">01"i ple.p&p &ttp->>exa ple.co >2e"t">02type".p&p ... @ote- 7t ay be nece""ary to o(i+y t&e inclu(e>re#uire "tate ent" at t&e be'innin' o+ eac& o+ t&e te"t "cript" i+ your ?!la""e"? +ol(er +ro above i" na e( (i++erently.

2.4.

Useful links and tools

2&ere are "o e 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 below.

2.4.1.

OpenXML / prea!s"eetML
$ile %or&at docu&entation &ttp->>www.ec aAinternational.or'>new">2!4*3current3wor/>2!4*3available3(oc".&t "pen'() Explained e*+oo, &ttp->>openx l(eveloper.or'>article">1)70.a"px (icroso%t "%%ice #o&pati+ilit- Pac, %or !ord. Excel. and Po/erPoint 200 $ile $or&ats &ttp->>www. icro"o+t.co >(ownloa(">(etail".a"pxB+a ilyi(C)41b$470A$ae)A4aeeAD+4$A

PHPExcel Developer Docu entation

c6bb74c(1466E(i"playlan'Cen "pen'() Pac,a0e Explorer &ttp->>www.co(eplex.co >Pac/a'eExplorer>

2.4.2.

#requentl$ as%e! questions

2&e upAtoA(ate F.%.<. pa'e +or PHPExcel can be +oun( on &ttp->>www.co(eplex.co >PHPExcel>Fi/i>.iew.a"pxBtitleCF%<Ere+errin'2itleC,e#uire ent".

&"ere seems to 'e a pro'lem (it" c"aracter enco!in)...


7t i" nece""ary to u"e G2FAD enco(in' +or all text" in PHPExcel. 7+ t&e "cript u"e" (i++erent enco(in' t&en it i" po""ible to convert t&e text" wit& PHPH" iconv01 +unction.

PHP complains a'out *ip+rc"ive not 'ein) ,oun!


Ma/e "ure you eet all re#uire ent"6 e"pecially p&p34ip exten"ion "&oul( be enable(. 2&e :ip%rc&ive cla"" i" only re#uire( w&en rea(in' or writin' +or at" t&at u"e :ip co pre""ion 0Excel2007 an( II!alc1. ;ince ver"ion 1.7.6 t&e P!9:ip library &a" been bun(le( wit& PHPExcel a" an alternative to t&e :ip%rc&ive cla"". 2&i" can be enable( by callin'PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

be+ore callin' t&e "ave

et&o( o+ t&e Excel2007 Friter.

8ou can revert to u"in' :ip%rc&ive by callin'PHPExcel_Settings::setZipClass(PHPExcel_Settings::ZIPARCHIVE);

%t pre"ent6 t&i" only allow" you to write Excel2007 +ile" wit&out t&e nee( +or :ip%rc&ive 0not to rea( Excel2007 or II!alc1

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.# ;o e ver"ion" o+ t&e p&p34ip exten"ion on Fin(ow" contain an error w&en creatin' :7P +ile". 2&e ver"ion t&at can be +oun( on &ttp->>"nap".p&p.net>win$2>p&p*.2Awin$2Alate"t.4ip "&oul( wor/ at all ti e". %lternatively6 up'ra(in' to at lea"t PHP *.2.) "&oul( "olve t&e proble . 7+ you can=t locate a clean copy o+ :ip%rc&ive6 t&en you can u"e t&e P!9:ip library a" an alternative w&en writin' Excel2007 +ile"6 a" (e"cribe( above.

#atal error1 +llo(e! memor$ si2e o, xxx '$tes ex"auste! 3trie! to allocate $$$ '$tes4 in 222 on line aaa
PHPExcel &ol(" an ?in e ory? repre"entation o+ a "prea("&eet6 "o it i" "u"ceptible to PHPH" e ory li itation". 2&e e ory a(e available to PHP can be increa"e( by e(itin' t&e value o+ t&e e oryli it (irective in your p&p.ini +ile6 or by u"in' ini"et0H e ory3li itH6 H12DMH1 wit&in your co(e 07;P per ittin'1. ;o e ,ea(er" an( Friter" are +a"ter t&an ot&er"6 an( t&ey al"o u"e (i++erin' a ount" o+ e ory. 8ou can +in( "o e in(ication o+ t&e relative per+or ance an( e ory u"a'e +or t&e (i++erent ,ea(er" an( Friter"6 over t&e (i++erent ver"ion" o+ PHPExcel6 on t&e (i"cu""ion boar(. 7+ youHve alrea(y increa"e( e ory to a axi u 6 or canHt c&an'e your e ory li it6 t&en t&i" (i"cu""ion on t&e boar( (e"cribe" "o e o+ t&e et&o(" t&at can be applie( to re(uce t&e e ory u"a'e o+ your "cript" u"in' PHPExcel. PHPExcel Developer Docu entation

Protection on m$ (or%s"eet is not (or%in)5


F&en you a/e u"e o+ any o+ t&e wor/"&eet protection +eature" 0e.'. cell ran'e protection6 pro&ibitin' (eletin' row"6 ...16 a/e "ure you enable wor/"&eet "ecurity. 2&i" can +or exa ple be (one li/e t&i"$objPHPExcel !getActi"eS#eet() !getP$otection() !setS#eet(t$%e);

#eature X is not (or%in) (it" PHPExcel/6ea!er/7 / PHPExcel/0riter/*


@ot all +eature" o+ PHPExcel are i ple ente( in all o+ t&e ,ea(er > Friter cla""e". 2&i" i" o"tly (ue to un(erlyin' librarie" not "upportin' a "peci+ic +eature or not &avin' i ple ente( a "peci+ic +eature. For exa ple auto+ilter i" not i ple ente( in PE%, ;prea("&eet3Excel3writer6 w&ic& i" t&e ba"e o+ our Excel* writer. Fe are "lowly buil(in' up a li"t o+ +eature"6 to'et&er wit& t&e (i++erent rea(er" an( writer" t&at "upport t&e 6 in t&e ?Functionality !ro""A,e+erence.xl"? +ile in t&e >Docu entation +ol(er.

#ormulas !on8t seem to 'e calculate! in Excel2--9 usin) compati'ilit$ pac%5


2&i" i" nor al be&aviour o+ t&e co patibility pac/6 Excel2007 (i"play" t&i" correctly. G"e PHPExcel3Friter3Excel* i+ you really nee( calculate( value"6 or +orce recalculation in Excel200$.

ettin) column (i!t" is not 1--: accurate


2ryin' to "et colu n wi(t&6 7 experience one proble . F&en 7 open t&e +ile in Excel6 t&e actual wi(t& i" 0.71 le"" t&an it "&oul( be. 2&e "&ort an"wer i" t&at PHPExcel u"e" a a colu n=" wi(t&K +or ore (etail". ea"ure w&ere pa((in' i" inclu(e(. ;ee "ection- J;ettin'

Ho( !o ; use PHPExcel (it" m$ ,rame(or%


2&ere are "o e in"truction" +or u"in' PHPExcel wit& Loo la on t&e Loo la e""a'e boar( % pa'e o+ a(vice on u"in' PHPExcel in t&e 8ii +ra ewor/ 2&e Ba/ery &a" "o e &elper cla""e" +or rea(in' an( writin' wit& PHPExcel wit&in !a/ePHP 7nte'ratin' PHPExcel into Mo&ana &ttp->>www.+lyn"ar y.co >2010>07>p&pexcelA o(uleA +orA/o&anaA$> an( NOPQRSTUVW PHPExcel V Mo&ana Fra ewor/ G"in' PHPExcel wit& 2ypo$

<oomla +utoloa!er inter,eres (it" PHPExcel +utoloa!er


2&an/" to peterrlync& +or t&e +ollowin' a(vice on re"olvin' i""ue" between t&e PHPExcel autoloa(er an( Loo la %utoloa(er

2.4.9.

&utorials
En0lish PHPExcel tutorial &ttp->>openx l(eveloper $rench PHPExcel tutorial &ttp->>'Aernael"ten.(eveloppe4.co >tutoriel">excel2007> Russian PHPExcel 1lo0 Postin0s &ttp->>www.webAXunior.net>"o4(anieAexcelA+aXXlovA"Apo o"&&yuAp&pexcel> A 2apanese*lan0ua0e introduction to PHPExcel &ttp->>Xournal. yco .co.Xp>article">200)>0$>06>p&pexcel>in(ex.&t l

PHPExcel Developer Docu entation

9.
3.1.

+rc"itecture
Schematical

3.2.

Laz Loader

PHPExcel i ple ent" an autoloa(er or Jla4y loa(erK6 w&ic& ean" t&at it i" not nece""ary to inclu(e every +ile wit&in PHPExcel. 7t i" only nece""ary to inclu(e t&e initial PHPExcel cla"" +ile6 t&en t&e autoloa(er will inclu(e ot&er cla"" +ile" a" an( w&en re#uire(6 "o only t&o"e +ile" t&at are actually re#uire( by your "cript will be loa(e( into PHP e ory. 2&e ain bene+it o+ t&i" i" t&at it re(uce" t&e e ory +ootprint o+ PHPExcel it"el+6 "o t&at it u"e" le"" PHP e ory. 7+ your own "cript" alrea(y (e+ine an autoloa( +unction6 t&en t&i" PHPExcel autoloa( +unction. For exa ple6 i+ you &ave&%nction'__a%toloa(($class)') ''''*** +

ay be overwritten by t&e

Do t&i" in"tea(&%nction',-A%toloa(($class)') ''''*** + spl_a%toloa(_$egiste$(.,-A%toloa(.);

8our autoloa(er will t&en coAexi"t wit& t&e autoloa(er o+ PHPExcel.

3.3.

S!readsheet in memor

PHPExcel=" arc&itecture i" built in a way t&at it can "erve a" an inA e ory "prea("&eet. 2&i" ean" t&at6 i+ one woul( want to create a web ba"e( view o+ a "prea("&eet w&ic& co unicate" wit& PHPExcel=" obXect o(el6 &e woul( only &ave to write t&e +rontAen( co(e. Lu"t li/e (e"/top "prea("&eet "o+tware6 PHPExcel repre"ent" a "prea("&eet containin' one or wor/"&eet"6 w&ic& contain cell" wit& (ata6 +or ula"6 i a'e"6 Y ore

3.4.

"eaders and writers

In it" own6 PHPExcel (oe" not provi(e t&e +unctionality to rea( +ro or write to a per"i"te( "prea("&eet 0on (i"/ or in a (ataba"e1. 2o provi(e t&at +unctionality6 rea(er" an( writer" can be u"e(.

PHPExcel Developer Docu entation

By (e+ault6 t&e PHPExcel pac/a'e provi(e" "o e rea(er" an( writer"6 inclu(in' one +or t&e Ipen ZM9 "prea("&eet +or at 0a./.a. Excel 2007 +ile +or at1. 8ou are not li ite( to t&e (e+ault rea(er" an( writer"6 a" you are +ree to i ple ent t&e PHPExcel3Friter37,ea(er an( PHPExcel3Friter37Friter inter+ace in a cu"to cla"".

3.#.

$luent interfaces

PHPExcel "upport" +luent inter+ace" in o"t location". 2&i" ean" t&at you can ea"ily Jc&ainK call" to "peci+ic et&o(" wit&out re#uirin' a new PHP "tate ent. For exa ple6 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/);

PHPExcel Developer Docu entation

$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/);

2&i" can be rewritten a"$objPHPExcel !getP$ope$ties() '''''''''''' !setC$eato$(/0aa$ten'1allia%2/) '''''''''''' !setLast0o(i&ie(1-(/0aa$ten'1allia%2/) '''''''''''' !set3itle(/4&&ice'5667'8LS8'3est'9oc%,ent/) '''''''''''' !setS%bject(/4&&ice'5667'8LS8'3est'9oc%,ent/) '''''''''''' !set9esc$iption(/3est'(oc%,ent'&o$'4&&ice'5667'8LS8:'gene$ate('%sing' PHP'classes*/) '''''''''''' !set;e-2o$(s(/o&&ice'5667'openx,l'p#p/) '''''''''''' !setCatego$-(/3est'$es%lt'&ile/);

3sin0 %luent inter%aces is not re4uired Fluent inter+ace" &ave been i ple ente( to provi(e a convenient pro'ra re#uire(6 but can a/e your co(e ea"ier to rea( an( aintain.

in' %P7. G"e o+ t&e

i" not

PHPExcel Developer Docu entation

4.
4.1.

Creatin) a sprea!s"eet
%he &'&()cel class

2&e PHPExcel cla"" i" t&e core o+ PHPExcel. 7t contain" re+erence" to t&e containe( wor/"&eet"6 (ocu ent "ecurity "ettin'" an( (ocu ent eta (ata. 2o "i pli+y t&e PHPExcel concept- t&e PHPExcel cla"" repre"ent" your wor/boo/.

4.2.

*onfiguration Settings

Ince you &ave inclu(e( t&e PHPExcel +ile" in your "cript6 but be+ore in"tantiatin' a PHPExcel obXect or loa(in' a wor/boo/ +ile6 t&ere are a nu ber o+ con+i'uration option" t&at can be "et w&ic& will a++ect t&e "ub"e#uent be&aviour o+ t&e "cript.

4.2.1.

Cell Cac"in)

PHPExcel u"e" an avera'e o+ about 1/>cell in your wor/"&eet"6 "o lar'e wor/boo/" can #uic/ly u"e up available e ory. !ell cac&in' provi(e" a ec&ani" t&at allow" PHPExcel to aintain t&e cell obXect" in a " aller "i4e o+ e ory6 on (i"/6 or in %P!6 e cac&e or Fincac&e6 rat&er t&an in PHP e ory. 2&i" allow" you to re(uce t&e e ory u"a'e +or lar'e wor/boo/"6 alt&ou'& at a co"t o+ "pee( to acce"" cell (ata. By (e+ault6 PHPExcel "till &ol(" all cell obXect" in e ory6 but you can "peci+y alternative". 2o enable cell cac&in'6 you u"t call t&e PHPExcel3;ettin'"--"et!ac&e;tora'eMet&o(01 et&o(6 pa""in' in t&e cac&in' et&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();

"et!ac&e;tora'eMet&o(01 will return a boolean true on "ucce""6 +al"e on +ailure 0+or exa ple i+ tryin' to cac&e to %P! w&en %P! i" not enable(1. % "eparate cac&e i" aintaine( +or eac& in(ivi(ual wor/"&eet6 an( i" auto atically create( w&en t&e wor/"&eet i" in"tantiate( ba"e( on t&e cac&in' et&o( an( "ettin'" t&at you &ave con+i'ure(. 8ou cannot c&an'e t&e con+i'uration "ettin'" once you &ave "tarte( to rea( a wor/boo/6 or &ave create( your +ir"t wor/"&eet. !urrently6 t&e +ollowin' cac&in' et&o(" are available.

PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3in3 e ory[ 2&e (e+ault. 7+ you (on=t initiali"e any cac&in' et&o(6 t&en t&i" i" t&e u"e. !ell obXect" are aintaine( in PHP e ory a" at pre"ent. PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3in3 e ory3"eriali4e([ G"in' t&i" cac&in' et&o(6 cell" are &el( in PHP e ory a" an array o+ "eriali4e( obXect"6 w&ic& re(uce" t&e e ory +ootprint wit& ini al per+or ance over&ea(. PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3in3 e ory3'4ip[ 9i/e cac&e3in3 e ory3"eriali4e(6 t&i" et&o( &ol(" cell" in PHP e ory a" an array o+ "eriali4e( obXect"6 but '4ippe( to re(uce t&e e ory u"a'e "till +urt&er6 alt&ou'& acce"" to rea( or write a cell i" "li'&tly "lower. PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3to3(i"c7;%M[ et&o( t&at PHPExcel will

PHPExcel Developer Docu entation

F&en u"in' cac&e3to3(i"c7;%M all cell" are &el( in a te porary (i"/ +ile6 wit& only an in(ex to t&eir location in t&at +ile aintaine( in PHP e ory. 2&i" i" "lower t&an any o+ t&e cac&e3in3 e ory et&o("6 but "i'ni+icantly re(uce" t&e e ory +ootprint. 2&e te porary (i"/ +ile i" auto atically (elete( w&en your "cript ter inate". PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3to3p&p2e p[ 9i/e cac&e3to3(i"c7;%M6 w&en u"in' cac&e3to3p&p2e p all cell" are &el( in t&e p&p->>te p 7>I "trea 6 wit& only an in(ex to t&eir location aintaine( in PHP e ory. 7n PHP6 t&e p&p->> e ory wrapper "tore" (ata in t&e e ory- p&p->>te p be&ave" "i ilarly6 but u"e" a te porary +ile +or "torin' t&e (ata w&en a certain e ory li it i" reac&e(. 2&e (e+ault i" 1 MB6 but you can c&an'e t&i" w&en initiali"in' cac&e3to3p&p2e p.
$cac#e0et#o('<'PHPExcel_Cac#e(4bjectSto$age=acto$-::'cac#e_to_p#p3e,p; $cac#eSettings'<'a$$a-('.',e,o$-Cac#eSi>e'.''<!'.?01. ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);

2&e p&p->>te p +ile i" auto atically (elete( w&en your "cript ter inate". PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3to3apc[ F&en u"in' cac&e3to3apc6 cell obXect" are aintaine( in %P! 1 wit& only an in(ex aintaine( in PHP e ory to i(enti+y t&at t&e cell exi"t". By (e+ault6 an %P! cac&e ti eout o+ 600 "econ(" i" u"e(6 w&ic& "&oul( be enou'& +or o"t application"- alt&ou'& it i" po""ible to c&an'e t&i" w&en initiali"in' cac&e3to3%P!.
$cac#e0et#o('<'PHPExcel_Cac#e(4bjectSto$age=acto$-::cac#e_to_APC; $cac#eSettings'<'a$$a-('.cac#e3i,e.''''''''<!'@66 ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);

F&en your "cript ter inate" all entrie" will be cleare( +ro %P!6 re'ar(le"" o+ t&e cac&e2i e value6 "o it cannot be u"e( +or per"i"tent "tora'e u"in' t&i" ec&ani" . PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3to3 e cac&e F&en u"in' cac&e3to3 e cac&e6 cell obXect" are aintaine( in e cac&e 2 wit& only an in(ex aintaine( in PHP e ory to i(enti+y t&at t&e cell exi"t". By (e+ault6 PHPExcel loo/" +or a e cac&e "erver on local&o"t at port 11211. 7t al"o "et" a e cac&e ti eout li it o+ 600 "econ(". 7+ you are runnin' e cac&e on a (i++erent "erver or port6 t&en you can c&an'e t&e"e (e+ault" w&en you initiali"e cac&e3to3 e cac&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.''''<!'AA5AA: ''''''''''''''''''''''''.cac#e3i,e.'''''''<!'@66 ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);

F&en your "cript ter inate" all entrie" will be cleare( +ro e cac&e6 re'ar(le"" o+ t&e cac&e2i e value6 "o it cannot be u"e( +or per"i"tent "tora'e u"in' t&i" ec&ani" . PHPExcel3!ac&e(IbXect;tora'eFactory--cac&e3to3wincac&e[ F&en u"in' cac&e3to3wincac&e6 cell obXect" are aintaine( in Fincac&e $ wit& only an in(ex aintaine( in PHP e ory to i(enti+y t&at t&e cell exi"t". By (e+ault6 a Fincac&e cac&e ti eout o+ 600 "econ(" i" u"e(6 w&ic& "&oul( be enou'& +or o"t application"- alt&ou'& it i" po""ible 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.''''''''<!'@66 ''''''''''''''''''''''); PHPExcel_Settings::setCac#eSto$age0et#o(($cac#e0et#o(:'$cac#eSettings);
1 2

8ou u"t &ave %P! enable( +or PHP to u"e t&i" option. 8ou u"t &ave a e cac&e "erver runnin'6 an( &ave enable( option. $ 8ou u"t &ave Fincac&e enable( +or PHP to u"e t&i" option. PHPExcel Developer Docu entation

e cac&e +or your PHP to u"e t&i"

10

F&en your "cript ter inate" all entrie" will be cleare( +ro Fincac&e6 re'ar(le"" o+ t&e cac&e2i e value6 "o it cannot be u"e( +or per"i"tent "tora'e u"in' t&i" ec&ani" .

4.2.2.

Lan)ua)e/Locale

;o e locali"ation ele ent" &ave been inclu(e( in PHPExcel. 8ou can "et a locale by 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&'(B$"ali(Locale)') ec#o'.Cnable'to'set'locale'to'.*$locale*/' '$e"e$ting'to'en_%sDb$'E!Fn/; +

7+ Bra4ilian Portu'ue"e lan'ua'e +ile" aren=t available6 t&en t&e Portu'ue"e will be enable( in"tea(i+ Portu'ue"e lan'ua'e +ile" aren=t available6 t&en t&e "et9ocale01 et&o( will return an error6 an( % erican En'li"& 0en3u"1 "ettin'" will be u"e( t&rou'&out. More (etail" o+ t&e +eature" available once a locale &a" been "et6 inclu(in' a li"t o+ t&e lan'ua'e" an( locale" currently "upporte(6 can be +oun( in "ection 4.6.* 9ocale ;ettin'" +or For ulae.

4.3.

*learing a +ork,ook from memor

2&e PHPExcel obXect contain" cyclic re+erence" 0e.'. t&e wor/boo/ i" lin/e( to t&e wor/"&eet"6 an( t&e wor/"&eet" are lin/e( to t&eir parent wor/boo/1 w&ic& cau"e proble " w&en PHP trie" to clear t&e obXect" +ro e ory w&en t&ey are un"et016 or at t&e en( o+ a +unction w&en t&ey are in local "cope. 2&e re"ult o+ t&i" i" J e ory lea/"K6 w&ic& can ea"ily u"e a lar'e a ount o+ PHP=" li ite( e ory. 2&i" can only be re"olve( anually- i+ you nee( to un"et a wor/boo/6 t&en you al"o nee( to Jbrea/K t&e"e cyclic re+erence" be+ore (oin' "o. PHPExcel provi(e" t&e (i"connectFor/"&eet"01 et&o( +or t&i" purpo"e.
$objPHPExcel !(isconnectGo$Hs#eets(); %nset($objPHPExcel);

4.4.

+orksheets

% wor/"&eet i" a collection o+ cell"6 +or ula="6 i a'e"6 'rap&"6 Y 7t &ol(" all (ata you want to repre"ent a" a "prea("&eet wor/"&eet.

4.#.

-ccessing cells

%cce""in' cell" in a PHPExcel wor/"&eet "&oul( be pretty "trai'&t+orwar(. 2&i" topic li"t" "o e o+ t&e option" to acce"" a cell.

4.=.1.

ettin) a cell value '$ coor!inate


et&o(.

;ettin' a cell value by coor(inate can be (one u"in' t&e wor/"&eet=" setCellVal%e
$objPHPExcel !getActi"eS#eet() !setCellVal%e(.1?.:'.So,e'"al%e.);

4.=.2.

6etrievin) a cell '$ coor!inate

2o retrieve t&e value o+ a cell6 t&e cell "&oul( +ir"t be retrieve( +ro t&e wor/"&eet u"in' t&e getCell et&o(. % cell=" value can be rea( a'ain u"in' t&e +ollowin' line o+ co(e$objPHPExcel !getActi"eS#eet() !getCell(.1?.) !getVal%e();

7+ you nee( t&e calculate( value o+ a cell6 u"e t&e +ollowin' co(e. 2&i" i" +urt&er explaine( in 4.6.40.
$objPHPExcel !getActi"eS#eet() !getCell(.1?.) !getCalc%late(Val%e();

PHPExcel Developer Docu entation

11

4.=.9.

ettin) a cell value '$ column an! ro(

;ettin' a cell value by coor(inate can be (one u"in' t&e wor/"&eet=" setCellVal%e1-Col%,nAn(Ro2 et&o(.
EE'Set'cell'1? $objPHPExcel !getActi"eS#eet() !setCellVal%e1-Col%,nAn(Ro2(A:'?:'.So,e'"al%e.);

4.=.4.

6etrievin) a cell '$ column an! ro(

2o retrieve t&e value o+ a cell6 t&e cell "&oul( +ir"t be retrieve( +ro t&e wor/"&eet u"in' t&e getCell1-Col%,nAn(Ro2 et&o(. % cell=" value can be rea( a'ain u"in' t&e +ollowin' line o+ co(eEE'Iet'cell'1? $objPHPExcel !getActi"eS#eet() !getCell1-Col%,nAn(Ro2(A:'?) !getVal%e();

7+ you nee( t&e calculate( value o+ a cell6 u"e t&e +ollowin' co(e. 2&i" i" +urt&er explaine( in 4.6.40
EE'Iet'cell'1? $objPHPExcel !getActi"eS#eet() !getCell1-Col%,nAn(Ro2(A:'?) !getCalc%late(Val%e();

4.=.=.

Loopin) cells

Loopin) cells usin) iterators


2&e ea"ie"t way to loop cell" i" by u"in' iterator". G"in' iterator"6 one can u"e +oreac& to loop wor/"&eet"6 row" an( cell". Below i" an exa ple w&ere we rea( all t&e value" in a wor/"&eet an( (i"play t&e
DJp#p $objRea(e$'<'PHPExcel_I4=acto$-::c$eateRea(e$(.Excel5667.); $objRea(e$ !setRea(9ata4nl-(t$%e); $objPHPExcel'<'$objRea(e$ !loa((/test*xlsx/); $objGo$Hs#eet'<'$objPHPExcel !getActi"eS#eet(); ec#o'.Dtable!.'*'/Fn/; &o$eac#'($objGo$Hs#eet !getRo2Ite$ato$()'as'$$o2)') ''ec#o'.Dt$!.'*'/Fn/; ''$cellIte$ato$'<'$$o2 !getCellIte$ato$(); ''$cellIte$ato$ !setIte$ate4nl-ExistingCells(&alse);'EE'3#is'loops'all'cells: '''''''''''''''''''''''''''''''''''''''''''''''''''''EE'e"en'i&'it'is'not'set* '''''''''''''''''''''''''''''''''''''''''''''''''''''EE'1-'(e&a%lt:'onl-'cells '''''''''''''''''''''''''''''''''''''''''''''''''''''EE't#at'a$e'set'2ill'be '''''''''''''''''''''''''''''''''''''''''''''''''''''EE'ite$ate(* ''&o$eac#'($cellIte$ato$'as'$cell)') ''''ec#o'.Dt(!.'*'$cell !getVal%e()'*'.DEt(!.'*'/Fn/; ''+ '' ''ec#o'.DEt$!.'*'/Fn/; + ec#o'.DEtable!.'*'/Fn/; J!

in a table.

@ote t&at we &ave "et t&e cell iterator=" setIte$ate4nl-ExistingCells() to +al"e. 2&i" t&e iterator loop all cell"6 even i+ t&ey were not "et be+ore.

a/e"

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 be available at t&at o ent. 2&i" will create new cell" i+ re#uire( an( increa"e e ory u"a'e\ Inly u"e it i+ it i" inten(e( to loop all cell" t&at are po""ibly available.

PHPExcel Developer Docu entation

12

Loopin) cells usin) in!exes


Ine can u"e t&e po""ibility to acce"" cell value" by colu n an( row in(ex li/e 00611 in"tea( o+ H%1H +or rea(in' an( writin' cell value" in loop".

@ote- 7n PHPExcel colu n in(ex i" 0Aba"e( w&ile row in(ex i" 1Aba"e(. 2&at

ean" H%1H ] 00611

Below i" an exa ple w&ere we rea( all t&e value" in a wor/"&eet an( (i"play t&e
DJp#p $objRea(e$'<'PHPExcel_I4=acto$-::c$eateRea(e$(.Excel5667.); $objRea(e$ !setRea(9ata4nl-(t$%e); $objPHPExcel'<'$objRea(e$ !loa((/test*xlsx/); $objGo$Hs#eet'<'$objPHPExcel !getActi"eS#eet(); $#ig#estRo2'<'$objGo$Hs#eet !getHig#estRo2();'EE'e*g*'A6 $#ig#estCol%,n'<'$objGo$Hs#eet !getHig#estCol%,n();'EE'e*g'.=.

in a table.

$#ig#estCol%,nIn(ex'<'PHPExcel_Cell::col%,nIn(ex=$o,St$ing($#ig#estCol%,n);'EE'e*g*' K ec#o'.Dtable!.'*'/Fn/; &o$'($$o2'<'A;'$$o2'D<'$#ig#estRo2;'LL$$o2)') ''ec#o'.Dt$!.'*'/Fn/; ''&o$'($col'<'6;'$col'D<'$#ig#estCol%,nIn(ex;'LL$col)') ''''ec#o'.Dt(!.'*'$objGo$Hs#eet !getCell1-Col%,nAn(Ro2($col:'$$o2) !getVal%e()'*' .DEt(!.'*'/Fn/; ''+ ''ec#o'.DEt$!.'*'/Fn/; + ec#o'.DEtable!.'*'/Fn/; J!

4.=.>.

?sin) value 'in!ers to ,acilitate !ata entr$

7nternally6 PHPExcel u"e" a (e+ault PHPExcel_Cell_IVal%e1in(e$ i ple entation 0PHPExcel_Cell_9e&a%ltVal%e1in(e$1 to (eter ine (ata type" o+ entere( (ata u"in' a cell=" setVal%e() et&o(. Iptionally6 t&e (e+ault be&aviour o+ PHPExcel can be o(i+ie(6 allowin' ea"ier (ata entry. For exa ple6 a PHPExcel_Cell_A("ance(Val%e1in(e$ cla"" i" pre"ent. 7t auto atically convert" percenta'e" an( (ate" entere( a" "trin'" to t&e correct +or at6 al"o "ettin' t&e cell=" "tyle in+or ation. 2&e +ollowin' exa ple (e on"trate" &ow to "et t&e value bin(er in PHPExcelEMM'PHPExcel'ME $eN%i$e_once'.PHPExcel*p#p.; EMM'PHPExcel_Cell_A("ance(Val%e1in(e$'ME $eN%i$e_once'.PHPExcelECellEA("ance(Val%e1in(e$*p#p.; EMM'PHPExcel_I4=acto$-'ME $eN%i$e_once'.PHPExcelEI4=acto$-*p#p.; EE'Set'"al%e'bin(e$ PHPExcel_Cell::setVal%e1in(e$('ne2'PHPExcel_Cell_A("ance(Val%e1in(e$()'); EE'C$eate'ne2'PHPExcel'object $objPHPExcel'<'ne2'PHPExcel(); EE'*** EE'A(('so,e'(ata:'$ese,bling'so,e'(i&&e$ent'(ata't-pes

PHPExcel Developer Docu entation

1$

$objPHPExcel !getActi"eS#eet() !setCellVal%e(.AO.:'.Pe$centage'"al%e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1O.:'.A6P.); ''''''''''''''EE'Con"e$ts'to'6*A'an('sets'pe$centage'cell'st-le $objPHPExcel !getActi"eS#eet() !setCellVal%e(.AK.:'.9ateEti,e'"al%e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1K.:'.5A'9ece,be$'AQ?R.); ''''''''''''''EE'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 bin(in' i" re#uire(6 you can i ple ent 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 "o e wi(elyAu"e( PHPExcel recipe". Plea"e note t&at t&e"e (o @I2 o++er co plete (ocu entation on "peci+ic PHPExcel %P7 +unction"6 but Xu"t a bu p to 'et you "tarte(. 7+ you nee( "peci+ic %P7 +unction"6 plea"e re+er to t&e %P7 (ocu entation. For exa ple6 4.6.) ;ettin' a wor/"&eet=" pa'e orientation an( "i4e cover" "ettin' a pa'e orientation to %4. It&er paper +or at"6 li/e G; 9etter6 are not covere( in t&i" (ocu ent6 but in t&e PHPExcel %P7 (ocu entation.

4.>.1.

ettin) a sprea!s"eet8s meta!ata

PHPExcel allow" an ea"y way to "et a "prea("&eet=" eta(ata6 u"in' (ocu ent property acce""or". ;prea("&eet eta(ata can be u"e+ul +or +in(in' a "peci+ic (ocu ent in a +ile repo"itory or a (ocu ent ana'e ent "y"te . For exa ple Micro"o+t ;&arepoint u"e" (ocu ent eta(ata to "earc& +or a "peci+ic (ocu ent in it" (ocu ent li"t". ;ettin' "prea("&eet eta(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.

ettin) a sprea!s"eet8s active s"eet

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.

0rite a !ate into a cell

7n Excel6 (ate" are "tore( a" nu eric value" countin' t&e nu ber o+ (ay" elap"e( "ince 1)00A01A01. For exa ple6 t&e (ate H200DA12A$1H i" repre"ente( a" $)D1$. 8ou can veri+y t&i" in Micro"o+t I++ice Excel by enterin' t&at (ate in a cell an( a+terwar(" c&an'in' t&e nu ber +or at to H^eneralH "o t&e true nu eric value i" reveale(. Fritin' a (ate value in a cell con"i"t" o+ 2 line" o+ co(e. ;elect t&e Here are "o e exa ple"et&o( t&at "uit" you t&e be"t.

EM'PHPExcel_Cell_A("anceVal%e1in(e$'$eN%i$e('&o$'t#is'sa,ple'ME $eN%i$e_once'.PHPExcelECellEA("ance(Val%e1in(e$*p#p.; EE'0-SSL liHe'ti,esta,p'.566? A5 RA. PHPExcel_Cell::setVal%e1in(e$('ne2'PHPExcel_Cell_A("ance(Val%e1in(e$()'); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.9A.:'.566? A5 RA.) $objPHPExcel !getActi"eS#eet() !getSt-le(.9A.) !getT%,be$=o$,at() !set=o$,atCo(e(PHPExcel_St-le_T%,be$=o$,at::=4R0A3_9A3E_UUUU0099SLASH)

PHPExcel Developer Docu entation

14

EE'PHP ti,e'(Cnix'ti,e) $ti,e'<'g,,Hti,e(6:6:6:A5:RA:566?);'EE'int(A5R6@?A@66) $objPHPExcel !getActi"eS#eet() !setCellVal%e(.9A.:' PHPExcel_S#a$e(_9ate::PHP3oExcel($ti,e)) $objPHPExcel !getActi"eS#eet() !getSt-le(.9A.) !getT%,be$=o$,at() !set=o$,atCo(e(PHPExcel_St-le_T%,be$=o$,at::=4R0A3_9A3E_UUUU0099SLASH) EE'Excel ti,e $objPHPExcel !getActi"eS#eet() !setCellVal%e(.9A.:'RQ?AR) $objPHPExcel !getActi"eS#eet() !getSt-le(.9A.) !getT%,be$=o$,at() !set=o$,atCo(e(PHPExcel_St-le_T%,be$=o$,at::=4R0A3_9A3E_UUUU0099SLASH)

2&e above et&o(" +or enterin' a (ate all yiel( t&e "a e re"ult. PHPExcel3;tyle3@u berFor at provi(e" a lot o+ preA(e+ine( (ate +or at". 8otes: 1. ;ee "ection ?G"in' value bin(er" to +acilitate (ata entry? to learn %(vance(.alueBin(er u"e( in t&e +ir"t exa ple. ore about t&e

2. 7n previou" ver"ion" o+ PHPExcel up to an( inclu(in' 1.6.66 w&en a cell &a( a (ateAli/e nu ber +or at co(e6 it wa" po""ible to enter a (ate (irectly u"in' an inte'er PHPAti e wit&out convertin' to Excel (ate +or at. ;tartin' wit& PHPExcel 1.6.7 t&i" i" no lon'er "upporte(. $. Excel can al"o operate in a 1)04Aba"e( calen(ar 0(e+ault +or wor/boo/" "ave( on Mac1. @or ally6 you (o not &ave to worry about t&i" w&en u"in' PHPExcel.

4.>.4.

0rite a ,ormula into a cell

7n"i(e t&e Excel +ile6 +or ula" are alway" "tore( a" t&ey woul( appear in an En'li"& ver"ion o+ Micro"o+t I++ice Excel6 an( PHPExcel &an(le" all +or ulae internally in t&i" +or at. 2&i" ean" t&at t&e +ollowin' rule" &ol( Deci al "eparator i" H.H 0perio(1 Function ar'u ent "eparator i" H6H 0co a1 Matrix row "eparator i" H[H 0"e icolon1 En'li"& +unction na e" u"t be u"e( 2&i" i" re'ar(le"" o+ w&ic& lan'ua'e ver"ion o+ Micro"o+t I++ice Excel ay &ave been u"e( to create t&e Excel +ile. F&en t&e +inal wor/boo/ i" opene( by t&e u"er6 Micro"o+t I++ice Excel will ta/e care o+ (i"playin' t&e +or ula accor(in' t&e application" lan'ua'e. 2ran"lation i" ta/en care o+ by t&e application\ 2&e +ollowin' line o+ co(e write" t&e +or ula JC7F0!4_*006?pro+it?6?lo""?1K into t&e cell BD. @ote t&at t&e +or ula u"t "tart wit& JCK to a/e PHPExcel reco'ni"e t&i" a" a +or ula.
$objPHPExcel !getActi"eS#eet() !setCellVal%e(.1?.:.<I=(CO!K66:/p$o&it/:/loss/).);

% cell=" +or ula can be rea( a'ain u"in' t&e +ollowin' line o+ co(e$&o$,%la'<'$objPHPExcel !getActi"eS#eet() !getCell(.1?.) !getVal%e();

7+ you nee( t&e calculate( value o+ a cell6 u"e t&e +ollowin' co(e. 2&i" i" +urt&er explaine( in 4.6.40.
$"al%e'<'$objPHPExcel !getActi"eS#eet() !getCell(.1?.) !getCalc%late(Val%e();

4.>.=.

Locale ettin)s ,or #ormulae

;o e locali"ation ele ent" &ave been inclu(e( in PHPExcel. 8ou can "et a locale by 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&'(B$"ali(Locale)')

PHPExcel Developer Docu entation

1*

ec#o'.Cnable'to'set'locale'to'.*$locale*/' '$e"e$ting'to'en_%sDb$'E!Fn/; +

7+ ,u""ian lan'ua'e +ile" aren=t available6 t&e "et9ocale01 "ettin'" will be u"e( t&rou'&out. Ince you &ave "et a locale6 you can tran"late a +or ula +ro

et&o( will return an error6 an( En'li"& it" internal En'li"& co(in'.

$&o$,%la'<'$objPHPExcel !getActi"eS#eet() !getCell(.1?.) !getVal%e(); $t$anslate(=o$,%la'<' PHPExcel_Calc%lation::getInstance() !_t$anslate=o$,%la3oLocale($&o$,%la);

8ou can al"o create a +or ula u"in' t&e +unction na e" an( ar'u ent "eparator" appropriate to t&e (e+ine( locale[ t&en tran"late it to En'li"& be+ore "ettin' t&e cell value$&o$,%la'<'.<VWXYR@6(VZ[Z(56A6;5;K);VZ[Z(56A6;A5;RA);\][\WZ).; $inte$nal=o$,%la'<' PHPExcel_Calc%lation::getInstance() !t$anslate=o$,%la3oEnglis#($&o$,%la); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1?.:$inte$nal=o$,%la);

!urrently6 +or ula tran"lation only tran"late" t&e +unction na e"6 t&e con"tant" 2,GE an( F%9;E6 an( t&e +unction ar'u ent "eparator". %t pre"ent6 t&e +ollowin' locale "ettin'" are "upporte()an0ua0e !4ec& Dani"& ^er an ;pani"& Finni"& Frenc& Hun'arian 7talian Dutc& @orwe'ian Poli"& Portu'ue"e Bra4ilian Portu'ue"e ,u""ian ;we(i"& `eatina Dan"/ Deut"c& E"pabol ;uo i Francai" Ma'yar 7taliano @e(erlan(" @or"/ Ld4y/ pol"/i Portu'ue" Portu'ue" Bra"ileiro SfgghVi Wjkh ;ven"/a )ocale #ode !" Da De E" Fi Fr Hu 7t @l @o Pl pt pt3br ru ;v

4.>.>.

0rite a ne(line c"aracter @An@ in a cell 3+L&B@Enter@4

7n Micro"o+t I++ice Excel you 'et a line brea/ in a cell by &ittin' AL3L/Ente$/. F&en you (o t&at6 it auto atically turn" on ?wrap text? +or t&e cell. Here i" &ow to ac&ieve t&i" in PHPExcel$objPHPExcel !getActi"eS#eet() !getCell(.AA.) !setVal%e(/#elloFn2o$l(/); $objPHPExcel !getActi"eS#eet() !getSt-le(.AA.) !getAlign,ent() !setG$ap3ext(t$%e);

,ea(

5ip ore about +or attin' cell" u"in' getSt-le() el"ew&ere.

5ip A("ance(Val%ebin(e$*p#p auto atically 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"$eN%i$e_once'.PHPExcelECellEA("ance(Val%e1in(e$*p#p.; PHPExcel_Cell::setVal%e1in(e$('ne2'PHPExcel_Cell_A("ance(Val%e1in(e$()'); $objPHPExcel !getActi"eS#eet() !getCell(.AA.) !setVal%e(/#elloFn2o$l(/);

PHPExcel Developer Docu entation

16

,ea(

ore about A("ance(Val%e1in(e$*p#p el"ew&ere.

4.>...

Explicitl$ set a cell8s !atat$pe


et&o(6 or t&e

8ou can "et a cell=" (atatype explicitly by u"in' t&e cell=" setVal%eExplicit setCellVal%eExplicit et&o( o+ a wor/"&eet. Here=" an exa ple-

$objPHPExcel !getActi"eS#eet() !getCell(.AA.) !setVal%eExplicit(.5K.:' PHPExcel_Cell_9ata3-pe::3UPE_TC0ERIC);

4.>.C.
8ou can

C"an)e a cell into a clic%a'le ?6L


a/e a cell a clic/able G,9 by "ettin' it" &yperlin/ property-

$objPHPExcel !getActi"eS#eet() !setCellVal%e(.E5@.:'.222*p#pexcel*net.); $objPHPExcel !getActi"eS#eet() !getCell(.E5@.) !getH-pe$linH() !setC$l(.#ttp:EE222*p#pexcel*net.);

7+ you want to

a/e a &yperlin/ to anot&er wor/"&eet>cell6 u"e t&e +ollowin' co(e-

$objPHPExcel !getActi"eS#eet() !setCellVal%e(.E5@.:'.222*p#pexcel*net.); $objPHPExcel !getActi"eS#eet() !getCell(.E5@.) !getH-pe$linH() !setC$l(^s#eet:EE.S#eetna,e.BAA_);

4.>.D.

ettin) a (or%s"eet8s pa)e orientation an! si2e

;ettin' a wor/"&eet=" pa'e orientation an( "i4e can be (one u"in' t&e +ollowin' line" o+ co(e$objPHPExcel !getActi"eS#eet() !getPageSet%p() !set4$ientation(PHPExcel_Go$Hs#eet_PageSet%p::4RIET3A3I4T_LAT9SCAPE); $objPHPExcel !getActi"eS#eet() !getPageSet%p() !setPape$Si>e(PHPExcel_Go$Hs#eet_PageSet%p::PAPERSIZE_AO);

@ote t&at t&ere are a((itional pa'e "ettin'" available. Plea"e re+er to t&e %P7 (ocu entation +or all po""ible option".

4.>.1-.

Pa)e etup1 calin) options

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

PHPExcel Developer Docu entation

17

,et#o( set=it3oPage(***) setScale(***) set=it3oGi(t#(***) set=it3oHeig#t(***)

initial'"al%e &alse A66 A A

calling',et#o('2ill' t$igge$ set=it3oPage(&alse) set=it3oPage(t$%e) set=it3oPage(t$%e)

Tote

"al%e'6',eans' (o not &it to 2i(t# "al%e'6',eans' (o not &it to #eig#t

Example
Here i" &ow to %it to 9 pa0e /ide +- in%inite pa0es tall$objPHPExcel !getActi"eS#eet() !getPageSet%p() !set=it3oGi(t#(A); $objPHPExcel !getActi"eS#eet() !getPageSet%p() !set=it3oHeig#t(6);

%" you can "ee6 it i" not nece""ary to call set=it3oPage(t$%e) "ince set=it3oGi(t#(`) an( set=it3oHeig#t(`) tri''er" t&i".

7+ you u"e set=it3oGi(t#() you "&oul( in 'eneral al"o "peci+y set=it3oHeig#t() explicitly li/e in t&e exa ple. Be care+ul relyin' on t&e initial value". 2&i" i" e"pecially true i+ you are up'ra(in' +ro PHPExcel 1.7.0 to 1.7.1 w&ere t&e (e+ault value" +or +itAtoA&ei'&t an( +itAtoAwi(t& c&an'e( +ro 0 to 1.

4.>.11.
2o "et pa'e

Pa)e mar)ins
ar'in" +or a wor/"&eet6 u"e t&i" co(e!getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getPage0a$gins() !getPage0a$gins() !getPage0a$gins() !getPage0a$gins() !set3op(A); !setRig#t(6*7K); !setLe&t(6*7K); !set1otto,(A);

$objPHPExcel $objPHPExcel $objPHPExcel $objPHPExcel

@ote t&at t&e

ar'in value" are "peci+ie( in inc&e".

PHPExcel Developer Docu entation

1D

4.>.12.

Center a pa)e "ori2ontall$/verticall$

2o center a pa'e &ori4ontally>vertically6 you can u"e t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !getPageSet%p() !setHo$i>ontalCente$e((t$%e); $objPHPExcel !getActi"eS#eet() !getPageSet%p() !setVe$ticalCente$e((&alse);

4.>.19.

ettin) t"e print "ea!er an! ,ooter o, a (or%s"eet

;ettin' a wor/"&eet=" print &ea(er an( +ooter can be (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(entialB.); $objPHPExcel !getActi"eS#eet() !getHea(e$=oote$() !set4((=oote$(.aLa1.'*' $objPHPExcel !getP$ope$ties() !get3itle()'*'.aRPage'aP'o&'aT.);

;ub"titution an( +or attin' co(e" 0"tartin' wit& E1 can be 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" u"t appear. 2&e +ir"t occurrence o+ t&e +ollowin' co(e" turn" t&e +or attin' I@6 t&e "econ( occurrence turn" it IFF a'ain ;tri/et&rou'& ;uper"cript ;ub"cript ;uper"cript an( "ub"cript cannot bot& be I@ at "a e ti e. F&ic&ever co e" +ir"t win" an( t&e ot&er i" i'nore(6 w&ile t&e +ir"t i" I@. 2&e +ollowin' co(e" are "upporte( by Excel2007!o(e +or ?le+t "ection? 0t&ere are t&ree &ea(er > +ooter location"6 ?le+t?6 ?center?6 an( ?ri'&t?1. F&en two or ore occurrence" o+ t&i" "ection ar/er exi"t6 t&e content" +ro all ar/er" are concatenate(6 in t&e or(er o+ appearance6 an( place( into t&e le+t "ection. &P !o(e +or ?current pa'e l? &N !o(e +or ?total pa'e"? &font size !o(e +or ?text +ont "i4e?6 w&ere +ont "i4e i" a +ont "i4e in point". &K !o(e +or ?text +ont color?
&L

&S &X &Y &C

&D &T &G

,^B !olor i" "peci+ie( a" ,,^^BB 2&e e !olor i" "peci+e( a" 22;@@ w&ere 22 i" t&e t&e e color 7(6 ; i" eit&er ?m? or ?A? o+ t&e tint>"&a(e value6 @@ i" t&e tint>"&a(e value. !o(e +or ?text "tri/et&rou'&? on > o++ !o(e +or ?text "uper "cript? on > o++ !o(e +or ?text "ub"cript? on > o++ !o(e +or ?center "ection?. F&en two or ore occurrence" o+ t&i" "ection ar/er exi"t6 t&e content" +ro all ar/er" are concatenate(6 in t&e or(er o+ appearance6 an( place( into t&e center "ection. !o(e +or ?(ate? !o(e +or ?ti e? !o(e +or ?picture a" bac/'roun(? Plea"e a/e "ure to a(( t&e i a'e to t&e &ea(er>+ooter$obj9$a2ing'<'ne2' PHPExcel_Go$Hs#eet_Hea(e$=oote$9$a2ing(); $obj9$a2ing !setTa,e(.PHPExcel'logo.); $obj9$a2ing !setPat#(.*Ei,agesEp#pexcel_logo*gi&.); $obj9$a2ing !setHeig#t(R@); $objPHPExcel !getActi"eS#eet() !getHea(e$=oote$() !a((I,age($obj9$a2ing:' PHPExcel_Go$Hs#eet_Hea(e$=oote$::I0AIE_HEA9ER_LE=3);

PHPExcel Developer Docu entation

1)

&U &E &R

&Z &F &A &+ && font n!"e#font t$%e

& -#&ol' && & -#Re(ul!) & -#*t!li+ &* & -#&ol' *t!li+ &, &-

!o(e +or ?text "in'le un(erline? !o(e +or ?(ouble un(erline? !o(e +or ?ri'&t "ection?. F&en two or ore occurrence" o+ t&i" "ection ar/er exi"t6 t&e content" +ro all ar/er" are concatenate(6 in t&e or(er o+ appearance6 an( place( into t&e ri'&t "ection. !o(e +or ?t&i" wor/boo/H" +ile pat&? !o(e +or ?t&i" wor/boo/H" +ile na e? !o(e +or ?"&eet tab na e? !o(e +or a(( to pa'e l !o(e +or "ubtract +ro pa'e l !o(e +or ?text +ont na e? an( ?text +ont type?6 w&ere +ont na e an( +ont type are "trin'" "peci+yin' t&e na e an( type o+ t&e +ont6 "eparate( by a co a. F&en a &yp&en appear" in +ont na e6 it ean" ?none "peci+ie(?. Bot& o+ +ont na e an( +ont type can be locali4e( value". !o(e +or ?bol( +ont "tyle? !o(e +or ?bol( +ont "tyle? !o(e +or ?re'ular +ont "tyle? !o(e +or ?italic +ont "tyle? !o(e +or ?italic +ont "tyle? !o(e +or ?bol( italic +ont "tyle? !o(e +or ?outline "tyle? !o(e +or ?"&a(ow "tyle?

5ip 2&e above table o+ co(e" ay "ee overw&el in' +ir"t ti e you are tryin' to +i'ure out &ow to write "o e &ea(er or +ooter. 9uc/ily6 t&ere i" an ea"ier way. 9et Micro"o+t I++ice Excel (o t&e wor/ +or you. For exa ple6 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'ra " own inter+ace. ;ave +ile a" test*xlsx. @ow6 ta/e t&at +ile an( rea( o++ t&e value" u"in' PHPExcel a" +ollow"$objPHPexcel'<'PHPExcel_I4=acto$-::loa((.test*xlsx.); $objGo$Hs#eet'<'$objPHPexcel !getActi"eS#eet(); "a$_(%,p($objGo$Hs#eet !getHea(e$=oote$() !get4((=oote$()); "a$_(%,p($objGo$Hs#eet !getHea(e$=oote$() !getE"en=oote$()); "a$_(%,p($objGo$Hs#eet !getHea(e$=oote$() !get4((Hea(e$()); "a$_(%,p($objGo$Hs#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" ay +in( it ea"ier to rena e test*xlsx to test*>ip6 un4ip it6 an( in"pect (irectly t&e content" o+ t&e relevant xlE2o$Hs#eetsEs#eet8*x,l to +in( t&e co(e" +or &ea(er>+ooter.

4.>.14.

ettin) printin) 'rea%s on a ro( or column

2o "et a print brea/6 u"e t&e +ollowin' co(e6 w&ic& "et" a row brea/ on row 10.
$objPHPExcel !getActi"eS#eet() !set1$eaH('.AA6.':'PHPExcel_Go$Hs#eet::1REA;_R4G');

2&e +ollowin' line o+ co(e "et" a print brea/ on colu n D$objPHPExcel !getActi"eS#eet() !set1$eaH('.9A6.':' PHPExcel_Go$Hs#eet::1REA;_C4LC0T');

4.>.1=.

"o(/"i!e )ri!lines ("en printin)

2o "&ow>&i(e 'ri(line" w&en printin'6 u"e t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !setS#o2I$i(lines(t$%e);

PHPExcel Developer Docu entation

20

4.>.1>.

ettin) ro(s/columns to repeat at top/le,t

PHPExcel can repeat "peci+ic row">cell" at top>le+t o+ a pa'e. 2&e +ollowin' co(e i" an exa ple 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((A:'K);

4.>.1..

peci,$ printin) area

2o "peci+y a wor/"&eet=" printin' area6 u"e t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !getPageSet%p() !setP$intA$ea(.AA:EK.);

2&ere can al"o be

ultiple printin' area" in a "in'le wor/"&eet-

$objPHPExcel !getActi"eS#eet() !getPageSet%p() !setP$intA$ea(.AA:EK:IO:056.);

4.>.1C.

#ormattin) cells

% cell can be +or atte( wit& +ont6 bor(er6 +ill6 Y "tyle in+or ation. For exa ple6 one can "et t&e +ore'roun( colour o+ a cell to re(6 ali'ne( to t&e ri'&t6 an( t&e bor(er to blac/ an( t&ic/ bor(er "tyle. 9et=" (o t&at on cell B2$objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !get=ont() !getColo$() !setARI1(PHPExcel_St-le_Colo$::C4L4R_RE9); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !getAlign,ent() !setHo$i>ontal(PHPExcel_St-le_Align,ent::H4RIZ4T3AL_RIIH3); $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;); !get3op() !get1otto,() !getLe&t() !getRig#t()

$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$() !setARI1(.====6666.);

;tartin' wit& PHPExcel 1.7.0 'et;tyle01 al"o accept" a cell ran'e a" a para eter. For exa ple6 you can "et a re( bac/'roun( color on a ran'e o+ cell"$objPHPExcel !getActi"eS#eet() !getSt-le(.1R:17.) !get=ill() !set=ill3-pe(PHPExcel_St-le_=ill::=ILL_S4LI9) !getSta$tColo$() !setARI1(.====6666.);

5ip 7t i" reco en(e( to "tyle any cell" at once6 u"in' e.'. 'et;tyle0H%1-M*00H16 rat&er t&an "tylin' t&e cell" in(ivi(ually in a loop. 2&i" i" uc& +a"ter co pare( to loopin' t&rou'& cell" an( "tylin' t&e in(ivi(ually.

2&ere i" al"o an alternative anner to "et "tyle". 2&e +ollowin' co(e "et" a cell=" "tyle to +ont bol(6 ali'n ent ri'&t6 top bor(er t&in an( a 'ra(ient +ill$st-leA$$a-'<'a$$a-( .&ont.'<!'a$$a-( .bol(.'<!'t$%e: ): .align,ent.'<!'a$$a-( .#o$i>ontal.'<!'PHPExcel_St-le_Align,ent::H4RIZ4T3AL_RIIH3: ):

PHPExcel Developer Docu entation

21

.bo$(e$s.'<!'a$$a-( .top.'<!'a$$a-( .st-le.'<!'PHPExcel_St-le_1o$(e$::14R9ER_3HIT: ): ): .&ill.'<!'a$$a-( .t-pe.'<!'PHPExcel_St-le_=ill::=ILL_IRA9IET3_LITEAR: .$otation.'<!'Q6: .sta$tcolo$.'<!'a$$a-( .a$gb.'<!'.==A6A6A6.: ): .en(colo$.'<!'a$$a-( .a$gb.'<!'.========.: ): ): ); $objPHPExcel !getActi"eS#eet() !getSt-le(.AR.) !appl-=$o,A$$a-($st-leA$$a-);

Ir wit& a ran'e o+ cell"$objPHPExcel !getActi"eS#eet() !getSt-le(.1R:17.) !appl-=$o,A$$a-($st-leA$$a-);

2&i" alternative et&o( u"in' array" "&oul( be +a"ter in ter " o+ execution w&enever you are "ettin' ore t&an one "tyle property. But t&e (i++erence ay barely be ea"urable unle"" you &ave any (i++erent "tyle" in your wor/boo/.

Prior to PHPExcel 1.7.0 (uplicate;tyle%rray01 wa" t&e reco en(e( et&o( +or "tylin' a cell ran'e6 but t&i" et&o( &a" now been (eprecate( "ince 'et;tyle01 &a" "tarte( to accept a cell ran'e.

4.>.1D.

Eum'er ,ormats

8ou o+ten want to +or at nu ber" in Excel. For exa ple you ay want a t&ou"an(" "eparator plu" a +ixe( nu ber o+ (eci al" a+ter t&e (eci al "eparator. Ir per&ap" you want "o e nu ber" to be 4eroApa((e(. 7n Micro"o+t I++ice Excel you ay be +a iliar wit& "electin' a nu ber +or at +ro t&e ?For at !ell"? (ialo'. Here t&ere are "o e pre(e+ine( nu ber +or at" available inclu(in' "o e +or (ate". 2&e (ialo' i" (e"i'ne( in a way "o you (onHt &ave to interact wit& t&e un(erlyin' raw nu ber +or at co(e unle"" you nee( a cu"to nu ber +or at. 7n PHPExcel6 you can al"o apply variou" pre(e+ine( nu ber +or at". Exa ple$objPHPExcel !getActi"eS#eet() !getSt-le(.AA.) !getT%,be$=o$,at() !set=o$,atCo(e(PHPExcel_St-le_T%,be$=o$,at::=4R0A3_TC01ER_C400A_SEPARA3E9A);

2&i" will +or at a nu ber e.'. 1*D7.2 "o it "&ow" up a" 16*D7.20 w&en you open t&e wor/boo/ in M; I++ice Excel. 0Depen(in' on "ettin'" +or (eci al an( t&ou"an(" "eparator" in Micro"o+t I++ice Excel it ay "&ow up a" 1.*D76201 8ou can ac&ieve exactly t&e "a e a" t&e above by u"in' t&i"$objPHPExcel !getActi"eS#eet() !getSt-le(.AA.) !getT%,be$=o$,at() !set=o$,atCo(e(.b:bb6*66.);

7n Micro"o+t I++ice Excel6 a" well a" in PHPExcel6 you will &ave to interact wit& raw nu ber +or at co(e" w&enever you nee( "o e "pecial cu"to nu ber +or at. Exa ple$objPHPExcel !getActi"eS#eet() !getSt-le(.AA.) !getT%,be$=o$,at() !set=o$,atCo(e(.c1l%edc!<R666d$b:bb6;cRe(dcD6d$b:bb6;$b:bb6.);

%not&er exa ple i" w&en you want nu ber" 4eroApa((e( wit& lea(in' 4ero" to a +ixe( len't&-

PHPExcel Developer Docu entation

22

$objPHPExcel !getActi"eS#eet() !getCell(.AA.) !setVal%e(AQ); $objPHPExcel !getActi"eS#eet() !getSt-le(.AA.) !getT%,be$=o$,at() !set=o$,atCo(e(.6666.);'EE'2ill's#o2'as'66AQ'in'Excel

5ip 2&e rule" +or co po"in' a nu ber +or at co(e in Excel can be rat&er co plicate(. ;o eti e" you /now &ow to create "o e nu ber +or at in Micro"o+t I++ice Excel6 but (onHt /now w&at t&e un(erlyin' nu ber +or at co(e loo/" li/e. How (o you +in( itB 2&e rea(er" "&ippe( wit& PHPExcel co e to t&e re"cue. 9oa( your te plate wor/boo/ u"in' e.'. Excel2007 rea(er to reveal t&e nu ber +or at co(e. Exa ple &ow rea( a nu ber +or at 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(.AA.) !getT%,be$=o$,at() !get=o$,atCo(e()); %(vance( u"er" ay +in( it +a"ter to in"pect t&e nu ber +or at co(e (irectly by rena in' te plate.xl"x to te plate.4ip6 un4ippin'6 an( loo/in' +or t&e relevant piece o+ ZM9 co(e &ol(in' t&e nu ber +or at co(e in xl$styles.xml.

4.>.2-.

+li)nment an! (rap text

9et=" "et vertical ali'n ent to t&e top +or cell" AA:9O
$objPHPExcel !getActi"eS#eet() !getSt-le(.AA:9O.) !getAlign,ent() !setVe$tical(PHPExcel_St-le_Align,ent::VER3ICAL_34P);

Here i" &ow to ac&ieve wrap text$objPHPExcel !getActi"eS#eet() !getSt-le(.AA:9O.) !getAlign,ent() !setG$ap3ext(t$%e);

4.>.21.

ettin) t"e !e,ault st$le o, a (or%'oo%

7t i" po""ible to "et t&e (e+ault "tyle o+ a wor/boo/. 9et=" "et t&e (e+ault +ont to %rial "i4e D$objPHPExcel !get9e&a%ltSt-le() !get=ont() !setTa,e(.A$ial.); $objPHPExcel !get9e&a%ltSt-le() !get=ont() !setSi>e(?);'

4.>.22.

t$lin) cell 'or!ers

7n PHPExcel it i" ea"y to apply variou" bor(er" on a rectan'ular "election. Here i" &ow to apply a t&ic/ re( bor(er outline aroun( cell" B2-^D.
$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.): ): ): ); $objGo$Hs#eet !getSt-le(.15:I?.) !appl-=$o,A$$a-($st-leA$$a-);

7n Micro"o+t I++ice Excel6 t&e above operation woul( corre"pon( to "electin' t&e cell" B2-^D6 launc&in' t&e "tyle (ialo'6 c&oo"in' a t&ic/ re( bor(er6 an( clic/in' on t&e ?Iutline? bor(er co ponent.

@ote t&at t&e bor(er outline i" applie( to t&e rectan'ular "election B2-^D a" a w&ole6 not on eac& cell in(ivi(ually.

PHPExcel Developer Docu entation

2$

8ou can ac&ieve any bor(er e++ect by u"in' Xu"t t&e * ba"ic bor(er" an( operatin' on a "in'le cell at a ti eArra- ,ele+t ri'&t top botto (ia'onal (aps to propert'et9e+t01 'et,i'&t01 'et2op01 'etBotto 01 'etDia'onal01

%((itional "&ortcut bor(er" co e in &an(y li/e in t&e exa ple above. 2&e"e are t&e "&ortcut bor(er" availableArra- ,eallbor(er" outline in"i(e vertical &ori4ontal (aps to propert'et%llBor(er"01 'etIutline01 'et7n"i(e01 'et.ertical01 'etHori4ontal01

%n overview o+ all bor(er "&ortcut" can be "een in t&e +ollowin' i a'e-

7+ you "i ultaneou"ly "et e.'. allbor(er" an( vertical6 t&en we &ave ?overlappin'? bor(er"6 an( one o+ t&e co ponent" &a" to win over t&e ot&er w&ere t&ere i" bor(er overlap. 7n PHPExcel6 +ro wea/e"t to "tron'e"t bor(er"6 t&e li"t i" a" +ollow"- allbor(er"6 outline>in"i(e6 vertical>&ori4ontal6 le+t>ri'&t>top>botto >(ia'onal. 2&i" bor(er &ierarc&y can be utili4e( to ac&ieve variou" e++ect" in an ea"y anner.

4.>.29.

Con!itional ,ormattin) a cell


ore.

% cell can be +or atte( con(itionally6 ba"e( on a "peci+ic rule. For exa ple6 one can "et t&e +ore'roun( colour o+ a cell to re( i+ it" value i" below 4ero6 an( to 'reen i+ it" value i" 4ero or Ine can "et a con(itional "tyle rule"et to a cell u"in' t&e +ollowin' co(e-

$objCon(itionalA'<'ne2'PHPExcel_St-le_Con(itional(); $objCon(itionalA !setCon(ition3-pe(PHPExcel_St-le_Con(itional::C4T9I3I4T_CELLIS); $objCon(itionalA !set4pe$ato$3-pe(PHPExcel_St-le_Con(itional::4PERA34R_LESS3HAT); $objCon(itionalA !a((Con(ition(.6.); $objCon(itionalA !getSt-le() !get=ont() !getColo$() !setARI1(PHPExcel_St-le_Colo$::C4L4R_RE9); $objCon(itionalA !getSt-le() !get=ont() !set1ol((t$%e);

PHPExcel Developer Docu entation

24

$objCon(itional5'<'ne2'PHPExcel_St-le_Con(itional(); $objCon(itional5 !setCon(ition3-pe(PHPExcel_St-le_Con(itional::C4T9I3I4T_CELLIS); $objCon(itional5 !set4pe$ato$3-pe(PHPExcel_St-le_Con(itional::4PERA34R_IREA3ER3HAT4RESCAL); $objCon(itional5 !a((Con(ition(.6.); $objCon(itional5 !getSt-le() !get=ont() !getColo$() !setARI1(PHPExcel_St-le_Colo$::C4L4R_IREET); $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(itionalA); a$$a-_p%s#($con(itionalSt-les:'$objCon(itional5); $objPHPExcel !getActi"eS#eet() !getSt-le(.15.) !setCon(itionalSt-les($con(itionalSt-les);

7+ you want to copy t&e rule"et to ot&er cell"6 you can (uplicate t&e "tyle obXect$objPHPExcel !getActi"eS#eet() !(%plicateSt-le('$objPHPExcel !getActi"eS#eet() !getSt-le(.15.):'.1R:17.');

4.>.24.
2o a(( a co E11-

+!! a comment to a cell


ent to a cell6 u"e t&e +ollowin' co(e. 2&e exa ple below a((" a co ent to cell

$objPHPExcel !getActi"eS#eet() !getCo,,ent(.EAA.) !setA%t#o$(.PHPExcel.); $objCo,,entRic#3ext'<'$objPHPExcel !getActi"eS#eet() !getCo,,ent(.EAA.) !get3ext() !c$eate3extR%n(.PHPExcel:.); $objCo,,entRic#3ext !get=ont() !set1ol((t$%e); $objPHPExcel !getActi"eS#eet() !getCo,,ent(.EAA.) !get3ext() !c$eate3extR%n(/F$Fn/); $objPHPExcel !getActi"eS#eet() !getCo,,ent(.EAA.) !get3ext() !c$eate3extR%n(.3otal' a,o%nt'on't#e'c%$$ent'in"oice:'excl%(ing'VA3*.);

4.>.2=.

+ppl$ auto,ilter to a ran)e o, cells

2o apply an auto+ilter to a ran'e o+ cell"6 u"e t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !setA%to=ilte$(.AA:CQ.);

(a,e sure that -ou al/a-s include the co&plete %ilter ran0e: Excel (oe" "upport "ettin' only t&e caption row6 but t&atH" not a be"t practice...

4.>.2>.

ettin) securit$ on a sprea!s"eet

Excel o++er" $ level" o+ JprotectionK- (ocu ent "ecurity6 "&eet "ecurity an( cell "ecurity. A Docu ent "ecurity allow" you to "et a pa""wor( on a co plete "prea("&eet6 allowin' c&an'e" to be a(e only w&en t&at pa""wor( i" entere(.

PHPExcel Developer Docu entation

2*

A A

For/"&eet "ecurity o++er" ot&er "ecurity option"- you can (i"allow in"ertin' row" on a "peci+ic "&eet6 (i"allow "ortin'6 Y !ell "ecurity o++er" t&e option to loc/>unloc/ a cell a" well a" "&ow>&i(e t&e internal +or ula

%n exa ple on "ettin' (ocu ent "ecurity$objPHPExcel !getSec%$it-() !setLocHGin(o2s(t$%e); $objPHPExcel !getSec%$it-() !setLocHSt$%ct%$e(t$%e); $objPHPExcel !getSec%$it-() !setGo$HbooHPass2o$((/PHPExcel/);

%n exa ple on "ettin' wor/"&eet "ecurity$objPHPExcel $objPHPExcel $objPHPExcel $objPHPExcel $objPHPExcel !getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getActi"eS#eet() !getP$otection() !getP$otection() !getP$otection() !getP$otection() !getP$otection() !setPass2o$((.PHPExcel.); !setS#eet(t$%e); !setSo$t(t$%e); !setInse$tRo2s(t$%e); !set=o$,atCells(t$%e);

%n exa ple on "ettin' cell "ecurity$objPHPExcel !getActi"eS#eet() !getSt-le(.1A.) !getP$otection() !setLocHe(( PHPExcel_St-le_P$otection::PR43EC3I4T_CTPR43EC3E9 );

(a,e sure -ou ena+le /or,sheet protection i% -ou need an- o% the /or,sheet protection %eatures: 2&i" can be (one u"in' t&e +ollowin' co(e- nobXPHPExcelA_'et%ctive;&eet01A_'etProtection01A_"et;&eet0true1[

4.>.2..

ettin) !ata vali!ation on a cell

Data vali(ation i" a power+ul +eature o+ Excel2007. 7t allow" to "peci+y an input +ilter on t&e (ata t&at can be in"erte( in a "peci+ic cell. 2&i" +ilter can be a ran'e 0i.e. value u"t be between 0 an( 1016 a li"t 0i.e. value u"t be pic/e( +ro a li"t16 Y 2&e +ollowin' piece o+ co(e only allow" nu ber" between 10 an( 20 to be entere( in cell B$$objVali(ation'<'$objPHPExcel !getActi"eS#eet() !getCell(.1R.) !get9ataVali(ation(); $objVali(ation !set3-pe('PHPExcel_Cell_9ataVali(ation::3UPE_GH4LE'); $objVali(ation !setE$$o$St-le('PHPExcel_Cell_9ataVali(ation::S3ULE_S34P'); $objVali(ation !setAllo21lanH(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$(.T%,be$'is'not'allo2e(B.); $objVali(ation !setP$o,pt3itle(.Allo2e('inp%t.); $objVali(ation !setP$o,pt(.4nl-'n%,be$s'bet2een'A6'an('56'a$e'allo2e(*.); $objVali(ation !set=o$,%laA(A6); $objVali(ation !set=o$,%la5(56);

2&e +ollowin' piece o+ co(e only allow" an ite

pic/e( +ro

a li"t o+ (ata to be entere( in cell B$-

$objVali(ation'<'$objPHPExcel !getActi"eS#eet() !getCell(.1K.) !get9ataVali(ation(); $objVali(ation !set3-pe('PHPExcel_Cell_9ataVali(ation::3UPE_LIS3'); $objVali(ation !setE$$o$St-le('PHPExcel_Cell_9ataVali(ation::S3ULE_IT=4R0A3I4T'); $objVali(ation !setAllo21lanH(&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(.PicH'&$o,'list.); $objVali(ation !setP$o,pt(.Please'picH'a'"al%e'&$o,'t#e'($op (o2n'list*.); $objVali(ation !set=o$,%laA(./Ite,'A:Ite,'1:Ite,'C/.);

PHPExcel Developer Docu entation

26

F&en u"in' a (ata vali(ation li"t li/e above6 a/e "ure you put t&e li"t between ? an( ? an( t&at you "plit t&e ite " wit& a co a 061. 7t i" i portant to re e ber t&at any "trin' participatin' in an Excel +or ula i" allowe( to be axi u 2** c&aracter" 0not byte"1. 2&i" "et" a li it on &ow any ite " you can &ave in t&e "trin' ?7te %67te B67te !?. 2&ere+ore it i" nor ally a better i(ea to type t&e ite value" (irectly in "o e cell ran'e6 "ay %1-%$6 an( in"tea( u"e6 "ay6 nobX.ali(ationA_"etFor ula10H;&eet\n%n1-n%n$H1[. %not&er bene+it i" t&at t&e ite value" t&e "elve" can contain t&e co a o6= c&aracter it"el+.

7+ you nee( (ata vali(ation on

ultiple cell"6 one can clone t&e rule"et-

$objPHPExcel !getActi"eS#eet() !getCell(.1?.) !set9ataVali(ation(clone' $objVali(ation);

4.>.2C.

ettin) a column8s (i!t"

% colu n=" wi(t& can be "et u"in' t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.9.) !setGi(t#(A5);

7+ you want PHPExcel to per+or an auto atic wi(t& calculation6 u"e t&e +ollowin' co(e. PHPExcel will approxi ate t&e colu n wit& to t&e wi(t& o+ t&e wi(e"t colu n value.
$objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.1.) !setA%toSi>e(t$%e);

2&e ea"ure +or colu n wi(t& in PHPExcel (oe" not corre"pon( exactly to t&e ea"ure you ay be u"e( to in Micro"o+t I++ice Excel. !olu n wi(t&" are (i++icult to (eal wit& in Excel6 an( t&ere are "everal ea"ure" +or t&e colu n wi(t&. 11 ;nner /idth in character units 0e.'. D.4$ t&i" i" probably w&at you are +a iliar wit& in Excel1 21 $ull /idth in pixels 0e.'. 64 pixel"1 $1 $ull /idth in character units 0e.'. ).14062*6 value A1 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 +ile6 &ence t&e o"t reliable ea"ure. Gn+ortunately6 (icroso%t "%%ice Excel does not present -ou /ith this &easure. 7n"tea( ea"ure" 11 an( 21 are co pute( by 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/boo/" (e+ault +ont. 2&ere+ore colu n wi(t&" ea"ure( in c&aracter unit" in two (i++erent wor/boo/" can only be co pare( i+ t&ey &ave t&e "a e (e+ault wor/boo/ +ont. 7+ you &ave "o e Excel +ile an( nee( to /now t&e colu n wi(t&" in wit& PHPExcel an( ec&o t&e retrieve( value". ea"ure $16 you can rea( t&e Excel +ile

4.>.2D.

"o(/"i!e a column

2o "et a wor/"&eet=" colu n vi"ibility6 you can u"e t&e +ollowin' co(e. 2&e +ir"t line explicitly "&ow" t&e colu n !6 t&e "econ( line &i(e" colu n 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

2o 'roup>outline a colu n6 you can u"e t&e +ollowin' co(e-

PHPExcel Developer Docu entation

27

$objPHPExcel !getActi"eS#eet() !getCol%,n9i,ension(.E.) !set4%tlineLe"el(A);

8ou can al"o collap"e t&e colu n. @ote t&at you "&oul( al"o "et t&e colu n invi"ible6 ot&erwi"e t&e collap"e will not be vi"ible 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 co plete exa ple on collap"in'. 8ou can in"truct PHPExcel to a(( a "u co(e a((" t&e "u ary to t&e le+tary to t&e ri'&t 0(e+ault16 or to t&e le+t. 2&e +ollowin'

$objPHPExcel !getActi"eS#eet() !setS#o2S%,,a$-Rig#t(&alse);

4.>.91.

ettin) a ro(8s "ei)"t

% row=" &ei'&t can be "et u"in' t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !getRo29i,ension(.A6.) !setRo2Heig#t(A66);

4.>.92.

"o(/"i!e a ro(

2o "et a wor/"&eet=" row vi"ibility6 you can u"e t&e +ollowin' co(e. 2&e +ollowin' exa ple &i(e" row nu ber 10.
$objPHPExcel !getActi"eS#eet() !getRo29i,ension(.A6.) !setVisible(&alse);

4.>.99.

Froup/outline a ro(

2o 'roup>outline a row6 you can u"e t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !getRo29i,ension(.K.) !set4%tlineLe"el(A);

8ou can al"o collap"e t&e row. @ote t&at you "&oul( al"o "et t&e row invi"ible6 ot&erwi"e t&e collap"e will not be vi"ible in Excel 2007.
$objPHPExcel !getActi"eS#eet() !getRo29i,ension(.K.) !setCollapse((t$%e); $objPHPExcel !getActi"eS#eet() !getRo29i,ension(.K.) !setVisible(&alse);

Here=" an exa ple w&ic& collap"e" row" *0 to D0&o$'($i'<'KA;'$i'D<'?6;'$iLL)') $objPHPExcel !getActi"eS#eet() $objPHPExcel !getActi"eS#eet() $objPHPExcel !getActi"eS#eet() $objPHPExcel !getActi"eS#eet() $objPHPExcel !getActi"eS#eet() !setCellVal%e(.A.'*'$i:'/=Ta,e'$i/); !setCellVal%e(.1.'*'$i:'/LTa,e'$i/); !setCellVal%e(.C.'*'$i:'/P#oneTo'$i/); !setCellVal%e(.9.'*'$i:'/=axTo'$i/); !setCellVal%e(.E.'*'$i:'t$%e);

$objPHPExcel !getActi"eS#eet() !getRo29i,ension($i) !set4%tlineLe"el(A); $objPHPExcel !getActi"eS#eet() !getRo29i,ension($i) !setVisible(&alse); + $objPHPExcel !getActi"eS#eet() !getRo29i,ension(?A) !setCollapse((t$%e);

8ou can in"truct PHPExcel to a(( a "u ary below t&e collap"ible row" 0(e+ault16 or above. 2&e +ollowin' co(e a((" t&e "u ary above$objPHPExcel !getActi"eS#eet() !setS#o2S%,,a$-1elo2(&alse);

4.>.94.

Mer)e/unmer)e cells
er'e two or ore cell"

7+ you &ave a bi' piece o+ (ata you want to (i"play in a wor/"&eet6 you can to'et&er6 to beco e one cell. 2&i" can be (one u"in' t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !,e$geCells(.AA?:E55.);

,e ovin' a

er'e can be (one u"in' t&e %n,e$geCells et&o($objPHPExcel !getActi"eS#eet() !%n,e$geCells(.AA?:E55.);

PHPExcel Developer Docu entation

2D

4.>.9=.

;nsertin) ro(s/columns

8ou can in"ert>re ove row">colu n" at a "peci+ic po"ition. 2&e +ollowin' co(e in"ert" 2 new row"6 ri'&t be+ore row 7$objPHPExcel !getActi"eS#eet() !inse$tTe2Ro21e&o$e(7:'5);

4.>.9>.

+!! a !ra(in) to a (or%s"eet

% (rawin' i" alway" repre"ente( a" a "eparate obXect6 w&ic& can be a((e( to a wor/"&eet. 2&ere+ore6 you u"t +ir"t in"tantiate a new PHPExcel_Go$Hs#eet_9$a2ing6 an( a""i'n it" propertie" a eanin'+ul value$obj9$a2ing'<'ne2'PHPExcel_Go$Hs#eet_9$a2ing(); $obj9$a2ing !setTa,e(.Logo.); $obj9$a2ing !set9esc$iption(.Logo.); $obj9$a2ing !setPat#(.*Ei,agesEo&&icelogo*jpg.); $obj9$a2ing !setHeig#t(R@);

2o a(( t&e above (rawin' to t&e wor/"&eet6 u"e t&e +ollowin' "nippet o+ co(e. PHPExcel create" t&e lin/ between t&e (rawin' an( t&e wor/"&eet$obj9$a2ing !setGo$Hs#eet($objPHPExcel !getActi"eS#eet());

8ou can "et nu erou" propertie" on a (rawin'6 &ere are "o e exa ple"$obj9$a2ing $obj9$a2ing $obj9$a2ing $obj9$a2ing $obj9$a2ing $obj9$a2ing $obj9$a2ing $obj9$a2ing !setTa,e(.Pai(.); !set9esc$iption(.Pai(.); !setPat#(.*Ei,agesEpai(*png.); !setCoo$(inates(.1AK.); !set4&&set8(AA6); !setRotation(5K); !getS#a(o2() !setVisible(t$%e); !getS#a(o2() !set9i$ection(OK);

4.>.9..

+!! ric" text to a cell

%((in' ric& text to a cell can be (one u"in' PHPExcel_Ric#3ext in"tance". Here=" an exa ple6 w&ic& create" t&e +ollowin' ric& text "trin'2&i" invoice i" payable within thirty days after the end of the month unle"" "peci+ie( ot&erwi"e 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;IREET')'); $objRic#3ext !c$eate3ext(.:'%nless'speci&ie('ot#e$2ise'on't#e'in"oice*.); $objPHPExcel !getActi"eS#eet() !getCell(.AA?.) !setVal%e($objRic#3ext);

4.>.9C.

De,ine a name! ran)e

PHPExcel "upport" t&e (e+inition o+ na e( ran'e". 2&e"e can be (e+ine( u"in' t&e +ollowin' co(eEE'A(('so,e'(ata $objPHPExcel !setActi"eS#eetIn(ex(6); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.AA.:'.=i$stna,e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.A5.:'.Lastna,e:.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.1A.:'.0aa$ten.); $objPHPExcel !getActi"eS#eet() !setCellVal%e(.15.:'.1allia%2.);

PHPExcel Developer Docu entation

2)

EE'9e&ine'na,e('$anges $objPHPExcel !a((Ta,e(Range('ne2'PHPExcel_Ta,e(Range(.Pe$son=T.:'$objPHPExcel !getActi"eS#eet():'.1A.)'); $objPHPExcel !a((Ta,e(Range('ne2'PHPExcel_Ta,e(Range(.Pe$sonLT.:'$objPHPExcel !getActi"eS#eet():'.15.)');

Iptionally6 a +ourt& para eter can be pa""e( (e+inin' t&e na e( ran'e local 0i.e. only u"able on t&e current wor/"&eet1. @a e( ran'e" are 'lobal by (e+ault.

4.>.9D.

6e!irect output to a client8s (e' 'ro(ser

;o eti e"6 one really want" to output a +ile to a client=" brow"er6 e"pecially w&en creatin' "prea("&eet" onAt&eA+ly. 2&ere are "o e ea"y "tep" t&at can be +ollowe( to (o t&i"1. !reate your PHPExcel "prea("&eet 2. Iutput H22P &ea(er" +or t&e type o+ (ocu ent you wi"& to output $. G"e t&e PHPExcel3Friter35 o+ your c&oice6 an( "ave to Jp&p->>outputK PHPExcel3Friter3Excel2007 u"e" te porary "tora'e w&en writin' to p&p->>output. By (e+ault6 te porary +ile" are "tore( in t&e "cript=" wor/in' (irectory. F&en t&ere i" no acce""6 it +all" bac/ to t&e operatin' "y"te =" te porary +ile" location.

5his &a- not +e sa%e %or unauthorized 7ie/in0: Depen(in' on t&e con+i'uration o+ your operatin' "y"te 6 te porary "tora'e can be rea( by anyone u"in' t&e "a e te porary "tora'e +ol(er. F&en con+i(entiality o+ your (ocu ent i" nee(e(6 it i" reco en(e( not to u"e p&p->>output.

H&&P "ea!ers
Exa ple o+ a "cript re(irectin' an Excel 2007 +ile to t&e clientH" brow"erDJp#p EM'He$e't#e$e'2ill'be'so,e'co(e'2#e$e'-o%'c$eate'$objPHPExcel'ME EE'$e(i$ect'o%tp%t'to'client'b$o2se$ #ea(e$(.Content 3-pe:'applicationE"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.); $objG$ite$'<'PHPExcel_I4=acto$-::c$eateG$ite$($objPHPExcel:'.Excel5667.); $objG$ite$ !sa"e(.p#p:EEo%tp%t.);' J!

Exa ple o+ a "cript re(irectin' an Excel* +ile to t&e clientH" brow"erDJp#p EM'He$e't#e$e'2ill'be'so,e'co(e'2#e$e'-o%'c$eate'$objPHPExcel'ME EE'$e(i$ect'o%tp%t'to'client'b$o2se$ #ea(e$(.Content 3-pe:'applicationE"n(*,s excel.); #ea(e$(.Content 9isposition:'attac#,ent;&ilena,e</,-&ile*xls/.); #ea(e$(.Cac#e Cont$ol:',ax age<6.); $objG$ite$'<'PHPExcel_I4=acto$-::c$eateG$ite$($objPHPExcel:'.ExcelK.); $objG$ite$ !sa"e(.p#p:EEo%tp%t.);' J!

!aution Ma/e "ure not to inclu(e any ec&o "tate ent" or output any ot&er content" t&an t&e Excel +ile. 2&ere "&oul( be no w&ite"pace be+ore t&e openin' pBp&p ta' an( at o"t one line brea/ a+ter t&e clo"in' B_ ta' 0w&ic& can al"o be o itte( to avoi( proble "1. Ma/e "ure t&at your "cript i" "ave( wit&out a BIM 0ByteAor(er ar/1. 0Becau"e t&i" count" a" ec&oin' output1

PHPExcel Developer Docu entation

$0

;a e t&in'" apply to all inclu(e( +ile"

Failin' to +ollow t&e above 'ui(eline" ay re"ult in corrupt Excel +ile" arrivin' at t&e client brow"er6 an(>or t&at &ea(er" cannot be "et by PHP 0re"ultin' in warnin' e""a'e"1.

4.>.4-.

ettin) t"e !e,ault column (i!t"

De+ault colu n wi(t& can be "et u"in' t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !get9e&a%ltCol%,n9i,ension() !setGi(t#(A5);

4.>.41.

ettin) t"e !e,ault ro( "ei)"t

De+ault row &ei'&t can be "et u"in' t&e +ollowin' co(e$objPHPExcel !getActi"eS#eet() !get9e&a%ltRo29i,ension() !setRo2Heig#t(AK);

4.>.42.

+!! a FD !ra(in) to a (or%s"eet

2&ere i'&t be a "ituation w&ere you want to 'enerate an inA e ory i a'e u"in' ^D an( a(( it to a PHPExcel wor/"&eet wit&out +ir"t &avin' to "ave t&i" +ile to a te porary location. Here=" an exa ple w&ic& 'enerate" an i a'e in e ory an( a((" it to t&e active wor/"&eetEE'Iene$ate'an'i,age $g(I,age'<'ei,agec$eatet$%ecolo$(A56:'56)'o$'(ie(.Cannot'Initiali>e'ne2'I9'i,age' st$ea,.); $textColo$'<'i,agecolo$allocate($g(I,age:'5KK:'5KK:'5KK); i,agest$ing($g(I,age:'A:'K:'K:''.C$eate('2it#'PHPExcel.:'$textColo$); EE'A(('a'($a2ing'to't#e'2o$Hs#eet $obj9$a2ing'<'ne2'PHPExcel_Go$Hs#eet_0e,o$-9$a2ing(); $obj9$a2ing !setTa,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_Go$Hs#eet_0e,o$-9$a2ing::RET9ERITI_fPEI); $obj9$a2ing !set0i,e3-pe(PHPExcel_Go$Hs#eet_0e,o$-9$a2ing::0I0E3UPE_9E=ACL3); $obj9$a2ing !setHeig#t(R@); $obj9$a2ing !setGo$Hs#eet($objPHPExcel !getActi"eS#eet());

4.>.49.

ettin) (or%s"eet 2oom level


level6 t&e +ollowin' co(e can be u"e(-

2o "et a wor/"&eet=" 4oo @ote t&at 4oo

$objPHPExcel !getActi"eS#eet() !getS#eetVie2() !setZoo,Scale(7K);

level "&oul( be in ran'e 10 q 400.

4.>.44.

"eet ta' color

;o eti e" you want to "et a color +or "&eet tab. For exa ple you can &ave a re( "&eet tab$objGo$Hs#eet !get3abColo$() !setRI1(.==6666.);

4.>.4=.

Creatin) (or%s"eets in a (or%'oo%


ore wor/"&eet" in t&e wor/boo/6 &ere i" &ow-

7+ you nee( to create

$objGo$Hs#eetA'<'$objPHPExcel !c$eateS#eet(); $objGo$Hs#eetA !set3itle(.Anot#e$'s#eet.); 2&in/ o+ create;&eet01 a" t&e ?7n"ert "&eet? button in Excel. F&en you &it t&at button a new "&eet i" appen(e( to t&e exi"tin' collection o+ wor/"&eet" in t&e wor/boo/.

4.>.4>.

Hi!!en (or%s"eets 3 "eet states4

;et a wor/"&eet to be hidden u"in' t&i" co(e-

PHPExcel Developer Docu entation

$1

$objPHPExcel !getActi"eS#eet() !setS#eetState(PHPExcel_Go$Hs#eet::SHEE3S3A3E_HI99ET);

;o eti e" you

ay even want t&e wor/"&eet to be >7er- hidden?. 2&e available "&eet "tate" are -

PHPExcel_Go$Hs#eet::SHEE3S3A3E_VISI1LE PHPExcel_Go$Hs#eet::SHEE3S3A3E_HI99ET PHPExcel_Go$Hs#eet::SHEE3S3A3E_VERUHI99ET 7n Excel t&e "&eet "tate Jvery &i((enK can only be "et pro'ra atically6 e.'. wit& .i"ual Ba"ic Macro. 7t i" not po""ible to a/e "uc& a "&eet vi"ible via t&e u"er inter+ace.

4.>.4..

6i)"tGtoGle,t (or%s"eet

For/"&eet" can be "et in(ivi(ually w&et&er colu n o%= "&oul( "tart at le+t or ri'&t "i(e. De+ault i" le+t. Here i" &ow to "et colu n" +ro ri'&tAtoAle+t.
EE'$ig#t to le&t'2o$Hs#eet $objPHPExcel !getActi"eS#eet() !setRig#t3oLe&t(t$%e);

PHPExcel Developer Docu entation

$2

=.
#.1.

Per,ormin) ,ormula calculations


Using the &'&()cel calculation engine

%" PHPExcel repre"ent" an inA e ory "prea("&eet6 it al"o o++er" +or ula calculation capabilitie". % cell can be o+ a value type 0containin' a nu ber or text16 or a +or ula type 0containin' a +or ula w&ic& can be evaluate(1. For exa ple6 t&e +or ula ?C;GM0%1-%101? evaluate" to t&e "u o+ value" in %16 %26 ...6 %10. 2o calculate a +or ula6 you can call t&e cell containin' t&e +or ula=" getCalc%late(Val%e()6 +or exa pleet&o(

$objPHPExcel !getActi"eS#eet() !getCell(.EAA.) !getCalc%late(Val%e();

7+ you write t&e +ollowin' line o+ co(e in t&e invoice (e o inclu(e( wit& PHPExcel6 it evaluate" to t&e value ?64?-

%not&er nice +eature o+ PHPExcelH" +or ula par"er6 i" t&at it can auto atically a(Xu"t a +or ula w&en in"ertin'>re ovin' row">colu n". HereH" an exa ple-

8ou "ee t&at t&e +or ula containe( in cell E11 i" ?;GM0E4-E)1?. @ow6 w&en 7 write t&e +ollowin' line o+ co(e6 two new pro(uct line" are a((e($objPHPExcel !getActi"eS#eet() !inse$tTe2Ro21e&o$e(7:'5);

PHPExcel Developer Docu entation

$$

Di( you noticeB 2&e +or ula in t&e +or er cell E11 0now E1$6 a" 7 in"erte( 2 new row"16 c&an'e( to ?;GM0E4-E111?. %l"o6 t&e in"erte( cell" (uplicate "tyle in+or ation o+ t&e previou" cell6 Xu"t li/e ExcelH" be&aviour. @ote t&at you can bot& in"ert row" an( colu n".

#.2.

/nown limitations
are (ue to t&e ean" t&at Excel

2&ere are "o e /nown li itation" to t&e PHPExcel calculation en'ine. Mo"t o+ t&e +act t&at an Excel +or ula i" converte( into PHP co(e be+ore bein' execute(. 2&i" +or ula calculation i" "ubXect to PHP=" lan'ua'e c&aracteri"tic".

=.2.1.

Operator prece!ence

7n Excel HmH win" over HEH6 Xu"t li/e H5H win" over HmH in or(inary al'ebra. 2&e +or er 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. icro"o+t.co >/b>2*1D) ,e+erence +or operator prece(ence in PHP&ttp->>www.p&p.net>operator"

=.2.2.

#ormulas involvin) num'ers an! text

For ula" involvin' nu ber" an( text ay pro(uce unexpecte( re"ult" or even unrea(able +ile content". For exa ple6 t&e +or ula HC$m?Hello ?H i" expecte( to pro(uce an error in Excel 0l.%9GE\1. Due to t&e +act t&at PHP convert" JHelloK to a nu eric value 04ero16 t&e re"ult o+ t&i" +or ula i" evaluate( a" $ in"tea( o+ evaluatin' a" an error. 2&i" al"o cau"e" t&e Excel (ocu ent bein' 'enerate( a" containin' unrea(able content. ,e+erence +or t&i" be&aviour in PHP&ttp->>be.p&p.net> anual>en>lan'ua'e.type"."trin'.p&pllan'ua'e.type"."trin'.conver"ion

PHPExcel Developer Docu entation

$4

>.

6ea!in) an! (ritin) to ,ile

%" you alrea(y /now +ro part $.4 ,ea(er" an( writer"6 rea(in' an( writin' to a per"i"te( "tora'e i" not po""ible u"in' t&e ba"e PHPExcel cla""e". For t&i" purpo"e6 PHPExcel provi(e" rea(er" an( writer"6 w&ic& are i ple entation" o+ PHPExcel3Friter37,ea(er an( PHPExcel3Friter37Friter.

..1.

&'&()cel0I1$actor

2&e PHPExcel %P7 o++er" ultiple et&o(" to create a PHPExcel3Friter37,ea(er or PHPExcel3Friter37Friter in"tance Direct creation .ia PHPExcel37IFactory %ll exa ple" un(erneat& (e on"trate t&e (irect creation PHPExcel37IFactory cla"" to (o t&i". et&o(. @ote t&at you can al"o u"e t&e

>.1.1.
2&ere are 2 explicitly.

Creatin) PHPExcel/6ea!er/;6ea!er usin) PHPExcel/;O#actor$


et&o(" +or rea(in' in a +ile into PHPExcel- u"in' auto atic +ile type re"olvin' or

%uto atic +ile type re"olvin' c&ec/" t&e (i++erent PHPExcel3,ea(er37,ea(er (i"tribute( wit& PHPExcel. 7+ one o+ t&e can loa( t&e "peci+ie( +ile na e6 t&e +ile i" loa(e( u"in' t&at PHPExcel3,ea(er37,ea(er. Explicit o(e re#uire" you to "peci+y w&ic& PHPExcel3,ea(er37,ea(er "&oul( be u"e(. 8ou can create a PHPExcel3,ea(er37,ea(er in"tance u"in' PHPExcel37IFactory in auto atic +ile type re"olvin' o(e u"in' t&e +ollowin' co(e "a ple$objPHPExcel'<'PHPExcel_I4=acto$-::loa((/6K&eat%$e(e,o*xlsx/);

% typical u"e o+ t&i" +eature i" w&en you nee( to rea( +ile" uploa(e( by your u"er"6 an( you (on=t /now w&et&er t&ey are uploa(in' xl" or xl"x +ile". 7+ you nee( to "et "o e propertie" on t&e rea(er6 0e.'. to only rea( (ata6 "ee later16 t&en you ay in"tea( want to u"e t&i" variantore about t&i"

$objRea(e$'<'PHPExcel_I4=acto$-::c$eateRea(e$=o$=ile(/6K&eat%$e(e,o*xlsx/); $objRea(e$ !setRea(9ata4nl-(t$%e); $objRea(e$ !loa((/6K&eat%$e(e,o*xlsx/);

8ou can create a PHPExcel3,ea(er37,ea(er in"tance u"in' PHPExcel37IFactory in explicit u"in' t&e +ollowin' co(e "a ple$objRea(e$'<'PHPExcel_I4=acto$-::c$eateRea(e$(/Excel5667/); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xlsx/);

o(e

>.1.2.

@ote t&at auto atic type re"olvin'

o(e i" "li'&tly "lower t&an explicit

o(e.

Creatin) PHPExcel/0riter/;0riter usin) PHPExcel/;O#actor$

8ou can create a PHPExcel3Friter37writer in"tance u"in' PHPExcel37IFactory$objG$ite$'<'PHPExcel_I4=acto$-::c$eateG$ite$($objPHPExcel:'/Excel5667/); $objG$ite$ !sa"e(/6K&eat%$e(e,o*xlsx/);

..2.

()cel 2223 4S!readsheet5L6 file format


ain +ile +or at o+ PHPExcel. 7t allow" outputtin' t&e inA e ory

Excel2007 +ile +or at i" t&e "prea("&eet to a .xl"x +ile.

PHPExcel Developer Docu entation

$*

>.2.1.

PHPExcel/6ea!er/Excel2--.

6ea!in) a sprea!s"eet
8ou can rea( an .xl"x +ile u"in' t&e +ollowin' co(e$objRea(e$'<'ne2'PHPExcel_Rea(e$_Excel5667(); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xlsx/);

6ea! !ata onl$


8ou can "et 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 Y an( Xu"t rea( cell (ata$objRea(e$'<'ne2'PHPExcel_Rea(e$_Excel5667(); $objRea(e$ !setRea(9ata4nl-(t$%e); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xlsx/);

6ea! speci,ic s"eets onl$


8ou can "et t&e option "et9oa(;&eet"Inly on t&e rea(er6 to in"truct t&e rea(er to only loa( t&e "&eet" wit& a 'iven na e$objRea(e$'<'ne2'PHPExcel_Rea(e$_Excel5667(); $objRea(e$ !setLoa(S#eets4nl-('a$$a-(/S#eet'A/:'/0-'special's#eet/)'); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xlsx/);

6ea! speci,ic cells onl$


8ou can "et t&e option "et,ea(Filter on t&e rea(er6 to in"truct t&e rea(er to only loa( t&e cell" w&ic& atc& a 'iven rule. % rea( +ilter can be any cla"" w&ic& i ple ent" PHPExcel3,ea(er37,ea(Filter. By (e+ault6 all cell" are rea( u"in' t&e PHPExcel3,ea(er3De+ault,ea(Filter. 2&e +ollowin' co(e will only rea( row 1 an( row" 20 q $0 o+ any "&eet in t&e Excel +ileclass'0-Rea(=ilte$'i,ple,ents'PHPExcel_Rea(e$_IRea(=ilte$ ) p%blic'&%nction'$ea(Cell($col%,n:'$$o2:'$2o$Hs#eetTa,e'<'..)') EE'Rea('title'$o2'an('$o2s'56' 'R6 i&'($$o2'<<'A'gg'($$o2'!<'56'aa'$$o2'D<'R6))') $et%$n't$%e; + $et%$n'&alse; + + $objRea(e$'<'ne2'PHPExcel_Rea(e$_Excel5667(); $objRea(e$ !setRea(=ilte$('ne2'0-Rea(=ilte$()'); $objPHPExcel'<'$objRea(e$ !loa((/6@la$gescale*xlsx/);

>.2.2.

PHPExcel/0riter/Excel2--.

0ritin) a sprea!s"eet
8ou can write an .xl"x +ile u"in' t&e +ollowin' co(e$objG$ite$'<'ne2'PHPExcel_G$ite$_Excel5667($objPHPExcel); $objG$ite$ !sa"e(/6K&eat%$e(e,o*xlsx/);

#ormula preGcalculation
By (e+ault6 t&i" writer preAcalculate" all +or ula" in t&e "prea("&eet. 2&i" can be "low on lar'e "prea("&eet"6 an( aybe even unwante(. 8ou can &owever (i"able +or ula preAcalculation$objG$ite$'<'ne2'PHPExcel_G$ite$_Excel5667($objPHPExcel); $objG$ite$ !setP$eCalc%late=o$,%las(&alse); $objG$ite$ !sa"e(/6K&eat%$e(e,o*xlsx/);

PHPExcel Developer Docu entation

$6

O,,ice 2--9 compati'ilit$ pac%


Becau"e o+ a bu' in t&e I++ice200$ co patibility pac/6 t&ere can be "o e " all i""ue" w&en openin' Excel2007 "prea("&eet" 0 o"tly relate( to +or ula calculation1. 8ou can enable I++ice200$ co patibility wit& t&e +ollowin' co(e$objG$ite$'<'ne2'PHPExcel_G$ite$_Excel5667($objPHPExcel); $objG$ite$ !set4&&ice566RCo,patibilit-(t$%e); $objG$ite$ !sa"e(/6K&eat%$e(e,o*xlsx/);

"%%ice200< co&pati+ilit- should onl- +e used /hen needed I++ice200$ co patibility option "&oul( only be u"e( w&en nee(e(. 2&i" option (i"able" "everal I++ice2007 +ile +or at option"6 re"ultin' in a lowerA+eature( I++ice2007 "prea("&eet w&en t&i" option i" u"e(.

..3.

()cel # 47I$$6 file format

Excel* +ile +or at i" t&e ol( Excel +ile +or at6 i ple ente( in PHPExcel to provi(e a uni+or anner to create bot& .xl"x an( .xl" +ile". 7t i" ba"ically a o(i+ie( ver"ion o+ PE%, ;prea("&eet3Excel3Friter6 an( &a" t&e "a e li itation" an( +eature" a" t&e PE%, library. Excel* +ile +or at will not be (evelope( any +urt&er6 it Xu"t provi(e" an a((itional +ile +or at +or PHPExcel.

Excel@ A1;$$) li&itations Plea"e note t&at B7FF +ile +or at &a" "o e li it" re'ar(in' to "tylin' cell" an( &an(lin' lar'e "prea("&eet" via PHP.

>.9.1.

PHPExcel/6ea!er/Excel=

6ea!in) a sprea!s"eet
8ou can rea( an .xl" +ile u"in' t&e +ollowin' co(e$objRea(e$'<'ne2'PHPExcel_Rea(e$_ExcelK(); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xls/);

6ea! !ata onl$


8ou can "et 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 Y an( Xu"t rea( cell (ata$objRea(e$'<'ne2'PHPExcel_Rea(e$_ExcelK(); $objRea(e$ !setRea(9ata4nl-(t$%e); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xls/);

6ea! speci,ic s"eets onl$


8ou can "et t&e option "et9oa(;&eet"Inly on t&e rea(er6 to in"truct t&e rea(er to only loa( t&e "&eet" wit& a 'iven na e$objRea(e$'<'ne2'PHPExcel_Rea(e$_ExcelK(); $objRea(e$ !setLoa(S#eets4nl-('a$$a-(/S#eet'A/:'/0-'special's#eet/)'); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*xls/);

6ea! speci,ic cells onl$


8ou can "et t&e option "et,ea(Filter on t&e rea(er6 to in"truct t&e rea(er to only loa( t&e cell" w&ic& atc& a 'iven rule. % rea( +ilter can be any cla"" w&ic& i ple ent" PHPExcel3,ea(er37,ea(Filter. By (e+ault6 all cell" are rea( u"in' t&e PHPExcel3,ea(er3De+ault,ea(Filter. 2&e +ollowin' co(e will only rea( row 1 an( row" 20 q $0 o+ any "&eet in t&e Excel +ileclass'0-Rea(=ilte$'i,ple,ents'PHPExcel_Rea(e$_IRea(=ilte$ ) p%blic'&%nction'$ea(Cell($col%,n:'$$o2:'$2o$Hs#eetTa,e'<'..)') EE'Rea('title'$o2'an('$o2s'56' 'R6 i&'($$o2'<<'A'gg'($$o2'!<'56'aa'$$o2'D<'R6))')

PHPExcel Developer Docu entation

$7

$et%$n't$%e; + $et%$n'&alse; + + $objRea(e$'<'ne2'PHPExcel_Rea(e$_ExcelK(); $objRea(e$ !setRea(=ilte$('ne2'0-Rea(=ilte$()'); $objPHPExcel'<'$objRea(e$ !loa((/6@la$gescale*xls/);

>.9.2.

PHPExcel/0riter/Excel=

0ritin) a sprea!s"eet
8ou can write an .xl" +ile u"in' t&e +ollowin' co(e$objG$ite$'<'ne2'PHPExcel_G$ite$_ExcelK($objPHPExcel); $objG$ite$ !sa"e(/6K&eat%$e(e,o*xls/);

..4.

()cel 2223 85L file format

Excel 200$ ZM9 +ile +or at i" a +ile +or at w&ic& can be u"e( in ol(er ver"ion" o+ Micro"o+t Excel.
Excel 200< '() li&itations Plea"e note t&at Excel 200$ ZM9 +or at &a" "o e li it" re'ar(in' to "tylin' cell" an( &an(lin' lar'e "prea("&eet" via PHP.

>.4.1.

PHPExcel/6ea!er/Excel2--9XML

6ea!in) a sprea!s"eet
8ou can rea( an .x l +ile u"in' t&e +ollowin' co(e$objRea(e$'<'ne2'PHPExcel_Rea(e$_Excel566R80L(); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*x,l/);

6ea! speci,ic cells onl$


8ou can "et t&e option "et,ea(Filter on t&e rea(er6 to in"truct t&e rea(er to only loa( t&e cell" w&ic& atc& a 'iven rule. % rea( +ilter can be any cla"" w&ic& i ple ent" PHPExcel3,ea(er37,ea(Filter. By (e+ault6 all cell" are rea( u"in' t&e PHPExcel3,ea(er3De+ault,ea(Filter. 2&e +ollowin' co(e will only rea( row 1 an( row" 20 q $0 o+ any "&eet in t&e Excel +ileclass'0-Rea(=ilte$'i,ple,ents'PHPExcel_Rea(e$_IRea(=ilte$ ) p%blic'&%nction'$ea(Cell($col%,n:'$$o2:'$2o$Hs#eetTa,e'<'..)') EE'Rea('title'$o2'an('$o2s'56' 'R6 i&'($$o2'<<'A'gg'($$o2'!<'56'aa'$$o2'D<'R6))') $et%$n't$%e; + $et%$n'&alse; + + $objRea(e$'<'ne2'PHPExcel_Rea(e$_Excel566R80L(); $objRea(e$ !setRea(=ilte$('ne2'0-Rea(=ilte$()'); $objPHPExcel'<'$objRea(e$ !loa((/6@la$gescale*x,l/);

..#.

S m,olic Lin/ 4S9L/6

;y bolic 9in/ 0;89M1 i" a Micro"o+t +ile +or at typically u"e( to exc&an'e (ata between application"6 "peci+ically "prea("&eet". ;89M +ile" conventionally &ave a ."l/ "u++ix. !o po"e( o+

PHPExcel Developer Docu entation

$D

only (i"playable %@;7 c&aracter"6 it can be ea"ily create( an( proce""e( by ot&er application"6 "uc& a" (ataba"e".

BC)D li&itations Plea"e note t&at ;89M +ile +or at &a" "o e li it" re'ar(in' to "tylin' cell" an( &an(lin' lar'e "prea("&eet" via PHP.

>.=.1.

PHPExcel/6ea!er/ 7LH

6ea!in) a sprea!s"eet
8ou can rea( an ."l/ +ile u"in' t&e +ollowin' co(e$objRea(e$'<'ne2'PHPExcel_Rea(e$_SUL;(); $objPHPExcel'<'$objRea(e$ !loa((/6K&eat%$e(e,o*slH/);

6ea! speci,ic cells onl$


8ou can "et t&e option "et,ea(Filter on t&e rea(er6 to in"truct t&e rea(er to only loa( t&e cell" w&ic& atc& a 'iven rule. % rea( +ilter can be any cla"" w&ic& i ple ent" PHPExcel3,ea(er37,ea(Filter. By (e+ault6 all cell" are rea( u"in' t&e PHPExcel3,ea(er3De+ault,ea(Filter. 2&e +ollowin' co(e will only rea( row 1 an( row" 20 q $0 o+ any "&eet in t&e ;89M +ileclass'0-Rea(=ilte$'i,ple,ents'PHPExcel_Rea(e$_IRea(=ilte$ ) p%blic'&%nction'$ea(Cell($col%,n:'$$o2:'$2o$Hs#eetTa,e'<'..)') EE'Rea('title'$o2'an('$o2s'56' 'R6 i&'($$o2'<<'A'gg'($$o2'!<'56'aa'$$o2'D<'R6))') $et%$n't$%e; + $et%$n'&alse; + + $objRea(e$'<'ne2'PHPExcel_Rea(e$_SUL;(); $objRea(e$ !setRea(=ilte$('ne2'0-Rea(=ilte$()'); $objPHPExcel'<'$objRea(e$ !loa((/6@la$gescale*slH/);

....

*S: 4*omma Se!arated :alues6

!;. 0!o a ;eparate( .alue"1 are o+ten u"e( a" an i port>export +ile +or at wit& ot&er "y"te ". PHPExcel allow" rea(in' an( writin' to !;. +ile".

#BV li&itations Plea"e note t&at !;. +ile +or at &a" "o e li it" re'ar(in' to "tylin' cell"6 nu ber +or attin'6 Y

>.>.1.

PHPExcel/6ea!er/C I

6ea!in) a C I ,ile
8ou 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+ten6 !;. +ile" are not really Jco a "eparate(K6 or u"e "e icolon 0[1 a" a "eparator. 8ou can in"truct PHPExcel3,ea(er3!;. "o e option" be+ore rea(in' a !;. +ile. @ote t&at PHPExcel3,ea(er3!;. by (e+ault a""u e" t&at t&e loa(e( !;. +ile i" G2FAD enco(e(. 7+ you are rea(in' !;. +ile" t&at were create( in Micro"o+t I++ice Excel t&e correct input enco(in' ay rat&er be Fin(ow"A12*2 0!P12*21. %lway" a/e "ure t&at t&e input enco(in' i" "et appropriately.

PHPExcel Developer Docu entation

$)

$objRea(e$'<'ne2'PHPExcel_Rea(e$_CSV(); $objRea(e$ !setInp%tEnco(ing(.CPA5K5.); $objRea(e$ !set9eli,ite$(.;.); $objRea(e$ !setEnclos%$e(..); $objRea(e$ !setLineEn(ing(/F$Fn/); $objRea(e$ !setS#eetIn(ex(6); $objPHPExcel'<'$objRea(e$ !loa((/sa,ple*cs"/);

6ea! a speci,ic (or%s"eet


!;. +ile" can only contain one wor/"&eet. 2&ere+ore6 you can "peci+y w&ic& "&eet to rea( +ro
$objRea(e$ !setS#eetIn(ex(6);

!;.-

6ea! into existin) sprea!s"eet


F&en wor/in' wit& !;. +ile"6 it i'&t occur t&at you want to i port !;. (ata into an exi"tin' PHPExcel obXect. 2&e +ollowin' co(e loa(" a !;. +ile into an exi"tin' nobXPHPExcel containin' "o e "&eet"6 an( i port" onto t&e 6t& "&eet$objRea(e$'<'ne2'PHPExcel_Rea(e$_CSV(); $objRea(e$ !set9eli,ite$(.;.); $objRea(e$ !setEnclos%$e(..); $objRea(e$ !setLineEn(ing(/F$Fn/); $objRea(e$ !setS#eetIn(ex(K);' $objRea(e$ !loa(IntoExisting(/6K&eat%$e(e,o*cs"/:'$objPHPExcel);

>.>.2.

PHPExcel/0riter/C I

0ritin) a C I ,ile
8ou can write a .c"v +ile u"in' t&e +ollowin' co(e$objG$ite$'<'ne2'PHPExcel_G$ite$_CSV($objPHPExcel); $objG$ite$ !sa"e(/6K&eat%$e(e,o*cs"/);

ettin) C I options
I+ten6 !;. +ile" are not really Jco a "eparate(K6 or u"e "e icolon 0[1 a" a "eparator. 8ou can in"truct PHPExcel3Friter3!;. "o e option" be+ore writin' a !;. +ile$objG$ite$'<'ne2'PHPExcel_G$ite$_CSV($objPHPExcel); $objG$ite$ !set9eli,ite$(.;.); $objG$ite$ !setEnclos%$e(..); $objG$ite$ !setLineEn(ing(/F$Fn/); $objG$ite$ !setS#eetIn(ex(6); $objG$ite$ !sa"e(/6K&eat%$e(e,o*cs"/);

0rite a speci,ic (or%s"eet


!;. +ile" can only contain one wor/"&eet. 2&ere+ore6 you can "peci+y w&ic& "&eet to write to !;.$objG$ite$ !setS#eetIn(ex(6);

#ormula preGcalculation
By (e+ault6 t&i" writer preAcalculate" all +or ula" in t&e "prea("&eet. 2&i" can be "low on lar'e "prea("&eet"6 an( aybe even unwante(. 8ou can &owever (i"able +or ula preAcalculation$objG$ite$'<'ne2'PHPExcel_G$ite$_CSV($objPHPExcel); $objG$ite$ !setP$eCalc%late=o$,%las(&alse); $objG$ite$ !sa"e(/6K&eat%$e(e,o*cs"/);

0ritin) ?&#GC C I ,iles


% !;. +ile can be +ollowin' co(ear/e( a" G2FAD by writin' a BIM +ile &ea(er. 2&i" can be enable( by u"in' t&e

$objG$ite$'<'ne2'PHPExcel_G$ite$_CSV($objPHPExcel); $objG$ite$ !setCse140(t$%e);

PHPExcel Developer Docu entation

40

$objG$ite$ !sa"e(/6K&eat%$e(e,o*cs"/);

Decimal an! t"ousan!s separators


7+ t&e wor/"&eet you are exportin' contain" nu ber" wit& (eci al or t&ou"an(" "eparator" t&en you "&oul( t&in/ about w&at c&aracter" you want to u"e +or t&o"e be+ore (oin' t&e export. By (e+ault PHPExcel loo/" up in t&e "erver=" locale "ettin'" to (eci(e w&at c&aracter" to u"e. But to avoi( proble " it i" reco en(e( to "et t&e c&aracter" explicitly a" "&own below. En'li"& u"er" will want to u"e t&i" be+ore (oin' t&e export$eN%i$e_once'.PHPExcelES#a$e(ESt$ing*p#p. PHPExcel_S#a$e(_St$ing::set9eci,alSepa$ato$(.*.); PHPExcel_S#a$e(_St$ing::set3#o%san(sSepa$ato$(.:.);

^er an u"er" will want to u"e t&e oppo"ite value".


$eN%i$e_once'.PHPExcelES#a$e(ESt$ing*p#p. PHPExcel_S#a$e(_St$ing::set9eci,alSepa$ato$(.:.); PHPExcel_S#a$e(_St$ing::set3#o%san(sSepa$ato$(.*.);

@ote t&at t&e above co(e "et" (eci al an( t&ou"an( "eparator" a" 'lobal option". 2&i" al"o a++ect" &ow H2M9 an( PDF i" exporte(.

..3.

'%5L

PHPExcel allow" you to write a "prea("&eet into H2M9 +or at6 +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!.

H5() li&itations Plea"e note t&at H2M9 +ile +or at &a" "o e li it" re'ar(in' to "tylin' cell"6 nu ber +or attin'6 Y

>...1.

PHPExcel/0riter/H&ML
Plea"e note t&at PHPExcel3Friter3H2M9 only output" t&e +ir"t wor/"&eet by (e+ault.

0ritin) a sprea!s"eet
8ou can write a .&t +ile u"in' t&e +ollowin' co(e$objG$ite$'<'ne2'PHPExcel_G$ite$_H30L($objPHPExcel); $objG$ite$ !sa"e(/6K&eat%$e(e,o*#t,/);

0rite all (or%s"eets


H2M9 +ile" can contain one or +ile6 u"e t&e +ollowin' co(eore wor/"&eet". 7+ you want to write all "&eet" into a "in'le H2M9

$objG$ite$ !2$iteAllS#eets();

0rite a speci,ic (or%s"eet


H2M9 +ile" can contain one or H2M9ore wor/"&eet". 2&ere+ore6 you can "peci+y w&ic& "&eet to write to

$objG$ite$ !setS#eetIn(ex(6);

ettin) t"e ima)es root o, t"e H&ML ,ile


2&ere i'&t be "ituation" w&ere you want to explicitly "et t&e inclu(e( i a'e" root. For exa ple6 one i'&t want to "ee Di,g''st-le</position:'$elati"e;'le&t:'6px;'top:'6px;'2i(t#:' AO6px;'#eig#t:'7?px;/'s$c</http://www.domain.com/i,agesElogo*jpg/'bo$(e$</6/! in"tea( o+ Di,g''st-le</position:'$elati"e;'le&t:'6px;'top:'6px;'2i(t#:'AO6px;'#eig#t:' 7?px;/'s$c</*Ei,agesElogo*jpg/'bo$(e$</6/!.

PHPExcel Developer Docu entation

41

8ou can u"e t&e +ollowin' co(e to ac&ieve t&i" re"ult$objG$ite$ !setI,agesRoot(.#ttp:EE222*exa,ple*co,.);

#ormula preGcalculation
By (e+ault6 t&i" writer preAcalculate" all +or ula" in t&e "prea("&eet. 2&i" can be "low on lar'e "prea("&eet"6 an( aybe even unwante(. 8ou can &owever (i"able +or ula preAcalculation$objG$ite$'<'ne2'PHPExcel_G$ite$_H30L($objPHPExcel); $objG$ite$ !setP$eCalc%late=o$,%las(&alse); $objG$ite$ !sa"e(/6K&eat%$e(e,o*#t,/);

Em'e!!in) )enerate! H&ML in a (e' pa)e


2&ere i'&t be a "ituation w&ere you want to e be( t&e 'enerate( H2M9 in an exi"tin' web"ite. PHPExcel3Friter3H2M9 provi(e" "upport to 'enerate only "peci+ic part" o+ t&e H2M9 co(e6 w&ic& allow" you to u"e t&e"e part" in your web"ite. ;upporte( et&o(" 'enerateH2M9Hea(er01 'enerate;tyle"01 'enerate;&eetData01 'enerateH2M9Footer01 Here=" an exa ple w&ic& retrieve" all part" in(epen(ently an( pa'eDJp#p $objG$ite$'<'ne2'PHPExcel_G$ite$_H30L($objPHPExcel); ec#o'$objG$ite$ !gene$ateH30LHea(e$(); J! Dst-le! DB #t,l') ''&ont &a,il-:'3i,es'Te2'Ro,an; ''&ont si>e:'Qpt; ''bacHg$o%n( colo$:'2#ite; + DJp#p ec#o'$objG$ite$ !gene$ateSt-les(&alse);'EE'(o'not'2$ite'Dst-le!'an('DEst-le! J! ! DEst-le! DJp#p ec#o'$objG$ite$ !gene$ateS#eet9ata(); ec#o'$objG$ite$ !gene$ateH30L=oote$(); J!

er'e" t&e

into a re"ultin' H2M9

0ritin) ?&#GC H&ML ,iles


% H2M9 +ile can be +ollowin' co(ear/e( a" G2FAD by writin' a BIM +ile &ea(er. 2&i" can be enable( by u"in' t&e

$objG$ite$'<'ne2'PHPExcel_G$ite$_H30L($objPHPExcel); $objG$ite$ !setCse140(t$%e); $objG$ite$ !sa"e(/6K&eat%$e(e,o*#t,/);

Decimal an! t"ousan!s separators


;ee "ection PHPExcel3Friter3!;. &ow to control t&e appearance o+ t&e"e.

PHPExcel Developer Docu entation

42

..;.

&<$

PHPExcel allow" you to write a "prea("&eet into PDF +or at6 +or +a"t (i"tribution o+ repre"ente( (ata.

PD$ li&itations Plea"e note t&at PDF +ile +or at &a" "o e li it" re'ar(in' to "tylin' cell"6 nu ber +or attin'6 Y

>.C.1.

PHPExcel/0riter/PD#
Plea"e note t&at PHPExcel3Friter3PDF only output" t&e +ir"t wor/"&eet by (e+ault.

0ritin) a sprea!s"eet
8ou can write a .p(+ +ile u"in' t&e +ollowin' co(e$objG$ite$'<'ne2'PHPExcel_G$ite$_P9=($objPHPExcel); $objG$ite$ !sa"e(/6K&eat%$e(e,o*p(&/);

0rite all (or%s"eets


PDF +ile" can contain one or u"e t&e +ollowin' co(eore wor/"&eet". 7+ you want to write all "&eet" into a "in'le PDF +ile6

$objG$ite$ !2$iteAllS#eets();

0rite a speci,ic (or%s"eet


PDF +ile" can contain one or PDFore wor/"&eet". 2&ere+ore6 you can "peci+y w&ic& "&eet to write to

$objG$ite$ !setS#eetIn(ex(6);

#ormula preGcalculation
By (e+ault6 t&i" writer preAcalculate" all +or ula" in t&e "prea("&eet. 2&i" can be "low on lar'e "prea("&eet"6 an( aybe even unwante(. 8ou can &owever (i"able +or ula preAcalculation$objG$ite$'<'ne2'PHPExcel_G$ite$_P9=($objPHPExcel); $objG$ite$ !setP$eCalc%late=o$,%las(&alse); $objG$ite$ !sa"e(/6K&eat%$e(e,o*p(&/);

Decimal an! t"ousan!s separators


;ee "ection PHPExcel3Friter3!;. &ow to control t&e appearance o+ t&e"e.

..=.

Generating ()cel files from tem!lates 4read> modif > write6

,ea(er" an( writer" are t&e tool" t&at allow you to 'enerate Excel +ile" +ro te plate". 2&i" re#uire" le"" co(in' e++ort t&an 'eneratin' t&e Excel +ile +ro "cratc&6 e"pecially i+ your te plate &a" any "tyle"6 pa'e "etup propertie"6 &ea(er" etc. Here i" an exa ple &ow to open a te plate +ile6 +ill in a couple o+ +iel(" an( "ave it a'ain$objPHPexcel'<'PHPExcel_I4=acto$-::loa((.te,plate*xlsx.); $objGo$Hs#eet'<'$objPHPexcel !getActi"eS#eet(); $objGo$Hs#eet !getCell(.AA.) !setVal%e(.fo#n.); $objGo$Hs#eet !getCell(.A5.) !setVal%e(.S,it#.); $objG$ite$'<'PHPExcel_I4=acto$-::c$eateG$ite$($objPHPexcel:'.ExcelK.); $objG$ite$ !sa"e(.2$ite*xls.);

@otice t&at it i" o/ to loa( an xl"x +ile an( 'enerate an xl" +ile.

PHPExcel Developer Docu entation

4$

..

Cre!its

Plea"e re+er to t&e internet pa'e &ttp->>www.co(eplex.co >PHPExcel>Fi/i>.iew.a"pxB titleC!re(it"Ere+errin'2itleCHo e +or upAtoA(ate cre(it".

PHPExcel Developer Docu entation

44

+ppen!ix +1 Iali! arra$ %e$s ,or st$le appl$#rom+rra$34


2&e +ollowin' table li"t" t&e vali( array /ey" +or PHPExcel3;tyle applyFro %rray01 cla""e". 7+ t&e JMap" to propertyK colu n ap" a /ey to a "etter6 t&e value provi(e( +or t&at /ey will be applie( (irectly. 7+ t&e JMap" to propertyK colu n ap" a /ey to a 'etter6 t&e value provi(e( +or t&at /ey will be applie( a" anot&er "tyle array. PHPExcel_Bt-le %rray /ey+ill +ont bor(er" ali'n ent nu ber+or at protection PHPExcel_Bt-le_$ill %rray /eytype rotation "tartcolor en(color color PHPExcel_Bt-le_$ont %rray /eyna e bol( italic un(erline "tri/e color "i4e "uper;cript "ub;cript PHPExcel_Bt-le_1orders %rray /eyallbor(er" le+t ri'&t top botto (ia'onal vertical &ori4ontal (ia'onal(irection outline PHPExcel_Bt-le_1order %rray /ey"tyle color PHPExcel_Bt-le_Ali0n&ent %rray /ey&ori4ontal vertical Map" to property'etFill01 'etFont01 'etBor(er"01 'et%li'n ent01 'et@u berFor at01 'etProtection01 Map" to property"etFill2ype01 "et,otation01 'et;tart!olor01 'etEn(!olor01 'et;tart!olor01 Map" to property"et@a e01 "etBol(01 "et7talic01 "etGn(erline01 "et;tri/et&rou'&01 'et!olor01 "et;i4e01 "et;uper;cript01 "et;ub;cript01 Map" to property'et9e+t01[ 'et,i'&t01[ 'et2op01[ 'etBotto 01 'et9e+t01 'et,i'&t01 'et2op01 'etBotto 01 'etDia'onal01 'et.ertical01 'etHori4ontal01 "etDia'onalDirection01 "etIutline01 Map" to property"etBor(er;tyle01 'et!olor01 Map" to property"etHori4ontal01 "et.ertical01

PHPExcel Developer Docu entation

4*

rotation wrap "&rin/2oFit in(ent PHPExcel_Bt-le_8u&+er$or&at %rray /eyco(e PHPExcel_Bt-le_Protection %rray /eyloc/e( &i((en

"et2ext,otation01 "etFrap2ext01 "et;&rin/2oFit01 "et7n(ent01 Map" to property"etFor at!o(e01 Map" to property"et9oc/e(01 "etHi((en01

PHPExcel Developer Docu entation

46

You might also like