44a1fc04edf403e1ba1f617974e8bb24552c21cf.svn-base 232 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader_Excel5
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. // Original file header of ParseXL (used as the base for this class):
  28. // --------------------------------------------------------------------------------
  29. // Adapted from Excel_Spreadsheet_Reader developed by users bizon153,
  30. // trex005, and mmp11 (SourceForge.net)
  31. // http://sourceforge.net/projects/phpexcelreader/
  32. // Primary changes made by canyoncasa (dvc) for ParseXL 1.00 ...
  33. // Modelled moreso after Perl Excel Parse/Write modules
  34. // Added Parse_Excel_Spreadsheet object
  35. // Reads a whole worksheet or tab as row,column array or as
  36. // associated hash of indexed rows and named column fields
  37. // Added variables for worksheet (tab) indexes and names
  38. // Added an object call for loading individual woorksheets
  39. // Changed default indexing defaults to 0 based arrays
  40. // Fixed date/time and percent formats
  41. // Includes patches found at SourceForge...
  42. // unicode patch by nobody
  43. // unpack("d") machine depedency patch by matchy
  44. // boundsheet utf16 patch by bjaenichen
  45. // Renamed functions for shorter names
  46. // General code cleanup and rigor, including <80 column width
  47. // Included a testcase Excel file and PHP example calls
  48. // Code works for PHP 5.x
  49. // Primary changes made by canyoncasa (dvc) for ParseXL 1.10 ...
  50. // http://sourceforge.net/tracker/index.php?func=detail&aid=1466964&group_id=99160&atid=623334
  51. // Decoding of formula conditions, results, and tokens.
  52. // Support for user-defined named cells added as an array "namedcells"
  53. // Patch code for user-defined named cells supports single cells only.
  54. // NOTE: this patch only works for BIFF8 as BIFF5-7 use a different
  55. // external sheet reference structure
  56. /** PHPExcel root directory */
  57. if (!defined('PHPEXCEL_ROOT')) {
  58. /**
  59. * @ignore
  60. */
  61. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  62. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  63. }
  64. /**
  65. * PHPExcel_Reader_Excel5
  66. *
  67. * This class uses {@link http://sourceforge.net/projects/phpexcelreader/parseXL}
  68. *
  69. * @category PHPExcel
  70. * @package PHPExcel_Reader_Excel5
  71. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  72. */
  73. class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  74. {
  75. // ParseXL definitions
  76. const XLS_BIFF8 = 0x0600;
  77. const XLS_BIFF7 = 0x0500;
  78. const XLS_WorkbookGlobals = 0x0005;
  79. const XLS_Worksheet = 0x0010;
  80. // record identifiers
  81. const XLS_Type_FORMULA = 0x0006;
  82. const XLS_Type_EOF = 0x000a;
  83. const XLS_Type_PROTECT = 0x0012;
  84. const XLS_Type_OBJECTPROTECT = 0x0063;
  85. const XLS_Type_SCENPROTECT = 0x00dd;
  86. const XLS_Type_PASSWORD = 0x0013;
  87. const XLS_Type_HEADER = 0x0014;
  88. const XLS_Type_FOOTER = 0x0015;
  89. const XLS_Type_EXTERNSHEET = 0x0017;
  90. const XLS_Type_DEFINEDNAME = 0x0018;
  91. const XLS_Type_VERTICALPAGEBREAKS = 0x001a;
  92. const XLS_Type_HORIZONTALPAGEBREAKS = 0x001b;
  93. const XLS_Type_NOTE = 0x001c;
  94. const XLS_Type_SELECTION = 0x001d;
  95. const XLS_Type_DATEMODE = 0x0022;
  96. const XLS_Type_EXTERNNAME = 0x0023;
  97. const XLS_Type_LEFTMARGIN = 0x0026;
  98. const XLS_Type_RIGHTMARGIN = 0x0027;
  99. const XLS_Type_TOPMARGIN = 0x0028;
  100. const XLS_Type_BOTTOMMARGIN = 0x0029;
  101. const XLS_Type_PRINTGRIDLINES = 0x002b;
  102. const XLS_Type_FILEPASS = 0x002f;
  103. const XLS_Type_FONT = 0x0031;
  104. const XLS_Type_CONTINUE = 0x003c;
  105. const XLS_Type_PANE = 0x0041;
  106. const XLS_Type_CODEPAGE = 0x0042;
  107. const XLS_Type_DEFCOLWIDTH = 0x0055;
  108. const XLS_Type_OBJ = 0x005d;
  109. const XLS_Type_COLINFO = 0x007d;
  110. const XLS_Type_IMDATA = 0x007f;
  111. const XLS_Type_SHEETPR = 0x0081;
  112. const XLS_Type_HCENTER = 0x0083;
  113. const XLS_Type_VCENTER = 0x0084;
  114. const XLS_Type_SHEET = 0x0085;
  115. const XLS_Type_PALETTE = 0x0092;
  116. const XLS_Type_SCL = 0x00a0;
  117. const XLS_Type_PAGESETUP = 0x00a1;
  118. const XLS_Type_MULRK = 0x00bd;
  119. const XLS_Type_MULBLANK = 0x00be;
  120. const XLS_Type_DBCELL = 0x00d7;
  121. const XLS_Type_XF = 0x00e0;
  122. const XLS_Type_MERGEDCELLS = 0x00e5;
  123. const XLS_Type_MSODRAWINGGROUP = 0x00eb;
  124. const XLS_Type_MSODRAWING = 0x00ec;
  125. const XLS_Type_SST = 0x00fc;
  126. const XLS_Type_LABELSST = 0x00fd;
  127. const XLS_Type_EXTSST = 0x00ff;
  128. const XLS_Type_EXTERNALBOOK = 0x01ae;
  129. const XLS_Type_DATAVALIDATIONS = 0x01b2;
  130. const XLS_Type_TXO = 0x01b6;
  131. const XLS_Type_HYPERLINK = 0x01b8;
  132. const XLS_Type_DATAVALIDATION = 0x01be;
  133. const XLS_Type_DIMENSION = 0x0200;
  134. const XLS_Type_BLANK = 0x0201;
  135. const XLS_Type_NUMBER = 0x0203;
  136. const XLS_Type_LABEL = 0x0204;
  137. const XLS_Type_BOOLERR = 0x0205;
  138. const XLS_Type_STRING = 0x0207;
  139. const XLS_Type_ROW = 0x0208;
  140. const XLS_Type_INDEX = 0x020b;
  141. const XLS_Type_ARRAY = 0x0221;
  142. const XLS_Type_DEFAULTROWHEIGHT = 0x0225;
  143. const XLS_Type_WINDOW2 = 0x023e;
  144. const XLS_Type_RK = 0x027e;
  145. const XLS_Type_STYLE = 0x0293;
  146. const XLS_Type_FORMAT = 0x041e;
  147. const XLS_Type_SHAREDFMLA = 0x04bc;
  148. const XLS_Type_BOF = 0x0809;
  149. const XLS_Type_SHEETPROTECTION = 0x0867;
  150. const XLS_Type_RANGEPROTECTION = 0x0868;
  151. const XLS_Type_SHEETLAYOUT = 0x0862;
  152. const XLS_Type_XFEXT = 0x087d;
  153. const XLS_Type_PAGELAYOUTVIEW = 0x088b;
  154. const XLS_Type_UNKNOWN = 0xffff;
  155. // Encryption type
  156. const MS_BIFF_CRYPTO_NONE = 0;
  157. const MS_BIFF_CRYPTO_XOR = 1;
  158. const MS_BIFF_CRYPTO_RC4 = 2;
  159. // Size of stream blocks when using RC4 encryption
  160. const REKEY_BLOCK = 0x400;
  161. /**
  162. * Summary Information stream data.
  163. *
  164. * @var string
  165. */
  166. private $_summaryInformation;
  167. /**
  168. * Extended Summary Information stream data.
  169. *
  170. * @var string
  171. */
  172. private $_documentSummaryInformation;
  173. /**
  174. * User-Defined Properties stream data.
  175. *
  176. * @var string
  177. */
  178. private $_userDefinedProperties;
  179. /**
  180. * Workbook stream data. (Includes workbook globals substream as well as sheet substreams)
  181. *
  182. * @var string
  183. */
  184. private $_data;
  185. /**
  186. * Size in bytes of $this->_data
  187. *
  188. * @var int
  189. */
  190. private $_dataSize;
  191. /**
  192. * Current position in stream
  193. *
  194. * @var integer
  195. */
  196. private $_pos;
  197. /**
  198. * Workbook to be returned by the reader.
  199. *
  200. * @var PHPExcel
  201. */
  202. private $_phpExcel;
  203. /**
  204. * Worksheet that is currently being built by the reader.
  205. *
  206. * @var PHPExcel_Worksheet
  207. */
  208. private $_phpSheet;
  209. /**
  210. * BIFF version
  211. *
  212. * @var int
  213. */
  214. private $_version;
  215. /**
  216. * Codepage set in the Excel file being read. Only important for BIFF5 (Excel 5.0 - Excel 95)
  217. * For BIFF8 (Excel 97 - Excel 2003) this will always have the value 'UTF-16LE'
  218. *
  219. * @var string
  220. */
  221. private $_codepage;
  222. /**
  223. * Shared formats
  224. *
  225. * @var array
  226. */
  227. private $_formats;
  228. /**
  229. * Shared fonts
  230. *
  231. * @var array
  232. */
  233. private $_objFonts;
  234. /**
  235. * Color palette
  236. *
  237. * @var array
  238. */
  239. private $_palette;
  240. /**
  241. * Worksheets
  242. *
  243. * @var array
  244. */
  245. private $_sheets;
  246. /**
  247. * External books
  248. *
  249. * @var array
  250. */
  251. private $_externalBooks;
  252. /**
  253. * REF structures. Only applies to BIFF8.
  254. *
  255. * @var array
  256. */
  257. private $_ref;
  258. /**
  259. * External names
  260. *
  261. * @var array
  262. */
  263. private $_externalNames;
  264. /**
  265. * Defined names
  266. *
  267. * @var array
  268. */
  269. private $_definedname;
  270. /**
  271. * Shared strings. Only applies to BIFF8.
  272. *
  273. * @var array
  274. */
  275. private $_sst;
  276. /**
  277. * Panes are frozen? (in sheet currently being read). See WINDOW2 record.
  278. *
  279. * @var boolean
  280. */
  281. private $_frozen;
  282. /**
  283. * Fit printout to number of pages? (in sheet currently being read). See SHEETPR record.
  284. *
  285. * @var boolean
  286. */
  287. private $_isFitToPages;
  288. /**
  289. * Objects. One OBJ record contributes with one entry.
  290. *
  291. * @var array
  292. */
  293. private $_objs;
  294. /**
  295. * Text Objects. One TXO record corresponds with one entry.
  296. *
  297. * @var array
  298. */
  299. private $_textObjects;
  300. /**
  301. * Cell Annotations (BIFF8)
  302. *
  303. * @var array
  304. */
  305. private $_cellNotes;
  306. /**
  307. * The combined MSODRAWINGGROUP data
  308. *
  309. * @var string
  310. */
  311. private $_drawingGroupData;
  312. /**
  313. * The combined MSODRAWING data (per sheet)
  314. *
  315. * @var string
  316. */
  317. private $_drawingData;
  318. /**
  319. * Keep track of XF index
  320. *
  321. * @var int
  322. */
  323. private $_xfIndex;
  324. /**
  325. * Mapping of XF index (that is a cell XF) to final index in cellXf collection
  326. *
  327. * @var array
  328. */
  329. private $_mapCellXfIndex;
  330. /**
  331. * Mapping of XF index (that is a style XF) to final index in cellStyleXf collection
  332. *
  333. * @var array
  334. */
  335. private $_mapCellStyleXfIndex;
  336. /**
  337. * The shared formulas in a sheet. One SHAREDFMLA record contributes with one value.
  338. *
  339. * @var array
  340. */
  341. private $_sharedFormulas;
  342. /**
  343. * The shared formula parts in a sheet. One FORMULA record contributes with one value if it
  344. * refers to a shared formula.
  345. *
  346. * @var array
  347. */
  348. private $_sharedFormulaParts;
  349. /**
  350. * The type of encryption in use
  351. *
  352. * @var int
  353. */
  354. private $_encryption = 0;
  355. /**
  356. * The position in the stream after which contents are encrypted
  357. *
  358. * @var int
  359. */
  360. private $_encryptionStartPos = false;
  361. /**
  362. * The current RC4 decryption object
  363. *
  364. * @var PHPExcel_Reader_Excel5_RC4
  365. */
  366. private $_rc4Key = null;
  367. /**
  368. * The position in the stream that the RC4 decryption object was left at
  369. *
  370. * @var int
  371. */
  372. private $_rc4Pos = 0;
  373. /**
  374. * The current MD5 context state
  375. *
  376. * @var string
  377. */
  378. private $_md5Ctxt = null;
  379. /**
  380. * Create a new PHPExcel_Reader_Excel5 instance
  381. */
  382. public function __construct() {
  383. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  384. }
  385. /**
  386. * Can the current PHPExcel_Reader_IReader read the file?
  387. *
  388. * @param string $pFilename
  389. * @return boolean
  390. * @throws PHPExcel_Reader_Exception
  391. */
  392. public function canRead($pFilename)
  393. {
  394. // Check if file exists
  395. if (!file_exists($pFilename)) {
  396. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  397. }
  398. try {
  399. // Use ParseXL for the hard work.
  400. $ole = new PHPExcel_Shared_OLERead();
  401. // get excel data
  402. $res = $ole->read($pFilename);
  403. return true;
  404. } catch (PHPExcel_Exception $e) {
  405. return false;
  406. }
  407. }
  408. /**
  409. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  410. *
  411. * @param string $pFilename
  412. * @throws PHPExcel_Reader_Exception
  413. */
  414. public function listWorksheetNames($pFilename)
  415. {
  416. // Check if file exists
  417. if (!file_exists($pFilename)) {
  418. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  419. }
  420. $worksheetNames = array();
  421. // Read the OLE file
  422. $this->_loadOLE($pFilename);
  423. // total byte size of Excel data (workbook global substream + sheet substreams)
  424. $this->_dataSize = strlen($this->_data);
  425. $this->_pos = 0;
  426. $this->_sheets = array();
  427. // Parse Workbook Global Substream
  428. while ($this->_pos < $this->_dataSize) {
  429. $code = self::_GetInt2d($this->_data, $this->_pos);
  430. switch ($code) {
  431. case self::XLS_Type_BOF: $this->_readBof(); break;
  432. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  433. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  434. default: $this->_readDefault(); break;
  435. }
  436. }
  437. foreach ($this->_sheets as $sheet) {
  438. if ($sheet['sheetType'] != 0x00) {
  439. // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
  440. continue;
  441. }
  442. $worksheetNames[] = $sheet['name'];
  443. }
  444. return $worksheetNames;
  445. }
  446. /**
  447. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  448. *
  449. * @param string $pFilename
  450. * @throws PHPExcel_Reader_Exception
  451. */
  452. public function listWorksheetInfo($pFilename)
  453. {
  454. // Check if file exists
  455. if (!file_exists($pFilename)) {
  456. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  457. }
  458. $worksheetInfo = array();
  459. // Read the OLE file
  460. $this->_loadOLE($pFilename);
  461. // total byte size of Excel data (workbook global substream + sheet substreams)
  462. $this->_dataSize = strlen($this->_data);
  463. // initialize
  464. $this->_pos = 0;
  465. $this->_sheets = array();
  466. // Parse Workbook Global Substream
  467. while ($this->_pos < $this->_dataSize) {
  468. $code = self::_GetInt2d($this->_data, $this->_pos);
  469. switch ($code) {
  470. case self::XLS_Type_BOF: $this->_readBof(); break;
  471. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  472. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  473. default: $this->_readDefault(); break;
  474. }
  475. }
  476. // Parse the individual sheets
  477. foreach ($this->_sheets as $sheet) {
  478. if ($sheet['sheetType'] != 0x00) {
  479. // 0x00: Worksheet
  480. // 0x02: Chart
  481. // 0x06: Visual Basic module
  482. continue;
  483. }
  484. $tmpInfo = array();
  485. $tmpInfo['worksheetName'] = $sheet['name'];
  486. $tmpInfo['lastColumnLetter'] = 'A';
  487. $tmpInfo['lastColumnIndex'] = 0;
  488. $tmpInfo['totalRows'] = 0;
  489. $tmpInfo['totalColumns'] = 0;
  490. $this->_pos = $sheet['offset'];
  491. while ($this->_pos <= $this->_dataSize - 4) {
  492. $code = self::_GetInt2d($this->_data, $this->_pos);
  493. switch ($code) {
  494. case self::XLS_Type_RK:
  495. case self::XLS_Type_LABELSST:
  496. case self::XLS_Type_NUMBER:
  497. case self::XLS_Type_FORMULA:
  498. case self::XLS_Type_BOOLERR:
  499. case self::XLS_Type_LABEL:
  500. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  501. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  502. // move stream pointer to next record
  503. $this->_pos += 4 + $length;
  504. $rowIndex = self::_GetInt2d($recordData, 0) + 1;
  505. $columnIndex = self::_GetInt2d($recordData, 2);
  506. $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
  507. $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
  508. break;
  509. case self::XLS_Type_BOF: $this->_readBof(); break;
  510. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  511. default: $this->_readDefault(); break;
  512. }
  513. }
  514. $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  515. $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
  516. $worksheetInfo[] = $tmpInfo;
  517. }
  518. return $worksheetInfo;
  519. }
  520. /**
  521. * Loads PHPExcel from file
  522. *
  523. * @param string $pFilename
  524. * @return PHPExcel
  525. * @throws PHPExcel_Reader_Exception
  526. */
  527. public function load($pFilename)
  528. {
  529. // Read the OLE file
  530. $this->_loadOLE($pFilename);
  531. // Initialisations
  532. $this->_phpExcel = new PHPExcel;
  533. $this->_phpExcel->removeSheetByIndex(0); // remove 1st sheet
  534. if (!$this->_readDataOnly) {
  535. $this->_phpExcel->removeCellStyleXfByIndex(0); // remove the default style
  536. $this->_phpExcel->removeCellXfByIndex(0); // remove the default style
  537. }
  538. // Read the summary information stream (containing meta data)
  539. $this->_readSummaryInformation();
  540. // Read the Additional document summary information stream (containing application-specific meta data)
  541. $this->_readDocumentSummaryInformation();
  542. // total byte size of Excel data (workbook global substream + sheet substreams)
  543. $this->_dataSize = strlen($this->_data);
  544. // initialize
  545. $this->_pos = 0;
  546. $this->_codepage = 'CP1252';
  547. $this->_formats = array();
  548. $this->_objFonts = array();
  549. $this->_palette = array();
  550. $this->_sheets = array();
  551. $this->_externalBooks = array();
  552. $this->_ref = array();
  553. $this->_definedname = array();
  554. $this->_sst = array();
  555. $this->_drawingGroupData = '';
  556. $this->_xfIndex = '';
  557. $this->_mapCellXfIndex = array();
  558. $this->_mapCellStyleXfIndex = array();
  559. // Parse Workbook Global Substream
  560. while ($this->_pos < $this->_dataSize) {
  561. $code = self::_GetInt2d($this->_data, $this->_pos);
  562. switch ($code) {
  563. case self::XLS_Type_BOF: $this->_readBof(); break;
  564. case self::XLS_Type_FILEPASS: $this->_readFilepass(); break;
  565. case self::XLS_Type_CODEPAGE: $this->_readCodepage(); break;
  566. case self::XLS_Type_DATEMODE: $this->_readDateMode(); break;
  567. case self::XLS_Type_FONT: $this->_readFont(); break;
  568. case self::XLS_Type_FORMAT: $this->_readFormat(); break;
  569. case self::XLS_Type_XF: $this->_readXf(); break;
  570. case self::XLS_Type_XFEXT: $this->_readXfExt(); break;
  571. case self::XLS_Type_STYLE: $this->_readStyle(); break;
  572. case self::XLS_Type_PALETTE: $this->_readPalette(); break;
  573. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  574. case self::XLS_Type_EXTERNALBOOK: $this->_readExternalBook(); break;
  575. case self::XLS_Type_EXTERNNAME: $this->_readExternName(); break;
  576. case self::XLS_Type_EXTERNSHEET: $this->_readExternSheet(); break;
  577. case self::XLS_Type_DEFINEDNAME: $this->_readDefinedName(); break;
  578. case self::XLS_Type_MSODRAWINGGROUP: $this->_readMsoDrawingGroup(); break;
  579. case self::XLS_Type_SST: $this->_readSst(); break;
  580. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  581. default: $this->_readDefault(); break;
  582. }
  583. }
  584. // Resolve indexed colors for font, fill, and border colors
  585. // Cannot be resolved already in XF record, because PALETTE record comes afterwards
  586. if (!$this->_readDataOnly) {
  587. foreach ($this->_objFonts as $objFont) {
  588. if (isset($objFont->colorIndex)) {
  589. $color = self::_readColor($objFont->colorIndex,$this->_palette,$this->_version);
  590. $objFont->getColor()->setRGB($color['rgb']);
  591. }
  592. }
  593. foreach ($this->_phpExcel->getCellXfCollection() as $objStyle) {
  594. // fill start and end color
  595. $fill = $objStyle->getFill();
  596. if (isset($fill->startcolorIndex)) {
  597. $startColor = self::_readColor($fill->startcolorIndex,$this->_palette,$this->_version);
  598. $fill->getStartColor()->setRGB($startColor['rgb']);
  599. }
  600. if (isset($fill->endcolorIndex)) {
  601. $endColor = self::_readColor($fill->endcolorIndex,$this->_palette,$this->_version);
  602. $fill->getEndColor()->setRGB($endColor['rgb']);
  603. }
  604. // border colors
  605. $top = $objStyle->getBorders()->getTop();
  606. $right = $objStyle->getBorders()->getRight();
  607. $bottom = $objStyle->getBorders()->getBottom();
  608. $left = $objStyle->getBorders()->getLeft();
  609. $diagonal = $objStyle->getBorders()->getDiagonal();
  610. if (isset($top->colorIndex)) {
  611. $borderTopColor = self::_readColor($top->colorIndex,$this->_palette,$this->_version);
  612. $top->getColor()->setRGB($borderTopColor['rgb']);
  613. }
  614. if (isset($right->colorIndex)) {
  615. $borderRightColor = self::_readColor($right->colorIndex,$this->_palette,$this->_version);
  616. $right->getColor()->setRGB($borderRightColor['rgb']);
  617. }
  618. if (isset($bottom->colorIndex)) {
  619. $borderBottomColor = self::_readColor($bottom->colorIndex,$this->_palette,$this->_version);
  620. $bottom->getColor()->setRGB($borderBottomColor['rgb']);
  621. }
  622. if (isset($left->colorIndex)) {
  623. $borderLeftColor = self::_readColor($left->colorIndex,$this->_palette,$this->_version);
  624. $left->getColor()->setRGB($borderLeftColor['rgb']);
  625. }
  626. if (isset($diagonal->colorIndex)) {
  627. $borderDiagonalColor = self::_readColor($diagonal->colorIndex,$this->_palette,$this->_version);
  628. $diagonal->getColor()->setRGB($borderDiagonalColor['rgb']);
  629. }
  630. }
  631. }
  632. // treat MSODRAWINGGROUP records, workbook-level Escher
  633. if (!$this->_readDataOnly && $this->_drawingGroupData) {
  634. $escherWorkbook = new PHPExcel_Shared_Escher();
  635. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorkbook);
  636. $escherWorkbook = $reader->load($this->_drawingGroupData);
  637. // debug Escher stream
  638. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  639. //$debug->load($this->_drawingGroupData);
  640. }
  641. // Parse the individual sheets
  642. foreach ($this->_sheets as $sheet) {
  643. if ($sheet['sheetType'] != 0x00) {
  644. // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
  645. continue;
  646. }
  647. // check if sheet should be skipped
  648. if (isset($this->_loadSheetsOnly) && !in_array($sheet['name'], $this->_loadSheetsOnly)) {
  649. continue;
  650. }
  651. // add sheet to PHPExcel object
  652. $this->_phpSheet = $this->_phpExcel->createSheet();
  653. // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
  654. // cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
  655. // name in line with the formula, not the reverse
  656. $this->_phpSheet->setTitle($sheet['name'],false);
  657. $this->_phpSheet->setSheetState($sheet['sheetState']);
  658. $this->_pos = $sheet['offset'];
  659. // Initialize isFitToPages. May change after reading SHEETPR record.
  660. $this->_isFitToPages = false;
  661. // Initialize drawingData
  662. $this->_drawingData = '';
  663. // Initialize objs
  664. $this->_objs = array();
  665. // Initialize shared formula parts
  666. $this->_sharedFormulaParts = array();
  667. // Initialize shared formulas
  668. $this->_sharedFormulas = array();
  669. // Initialize text objs
  670. $this->_textObjects = array();
  671. // Initialize cell annotations
  672. $this->_cellNotes = array();
  673. $this->textObjRef = -1;
  674. while ($this->_pos <= $this->_dataSize - 4) {
  675. $code = self::_GetInt2d($this->_data, $this->_pos);
  676. switch ($code) {
  677. case self::XLS_Type_BOF: $this->_readBof(); break;
  678. case self::XLS_Type_PRINTGRIDLINES: $this->_readPrintGridlines(); break;
  679. case self::XLS_Type_DEFAULTROWHEIGHT: $this->_readDefaultRowHeight(); break;
  680. case self::XLS_Type_SHEETPR: $this->_readSheetPr(); break;
  681. case self::XLS_Type_HORIZONTALPAGEBREAKS: $this->_readHorizontalPageBreaks(); break;
  682. case self::XLS_Type_VERTICALPAGEBREAKS: $this->_readVerticalPageBreaks(); break;
  683. case self::XLS_Type_HEADER: $this->_readHeader(); break;
  684. case self::XLS_Type_FOOTER: $this->_readFooter(); break;
  685. case self::XLS_Type_HCENTER: $this->_readHcenter(); break;
  686. case self::XLS_Type_VCENTER: $this->_readVcenter(); break;
  687. case self::XLS_Type_LEFTMARGIN: $this->_readLeftMargin(); break;
  688. case self::XLS_Type_RIGHTMARGIN: $this->_readRightMargin(); break;
  689. case self::XLS_Type_TOPMARGIN: $this->_readTopMargin(); break;
  690. case self::XLS_Type_BOTTOMMARGIN: $this->_readBottomMargin(); break;
  691. case self::XLS_Type_PAGESETUP: $this->_readPageSetup(); break;
  692. case self::XLS_Type_PROTECT: $this->_readProtect(); break;
  693. case self::XLS_Type_SCENPROTECT: $this->_readScenProtect(); break;
  694. case self::XLS_Type_OBJECTPROTECT: $this->_readObjectProtect(); break;
  695. case self::XLS_Type_PASSWORD: $this->_readPassword(); break;
  696. case self::XLS_Type_DEFCOLWIDTH: $this->_readDefColWidth(); break;
  697. case self::XLS_Type_COLINFO: $this->_readColInfo(); break;
  698. case self::XLS_Type_DIMENSION: $this->_readDefault(); break;
  699. case self::XLS_Type_ROW: $this->_readRow(); break;
  700. case self::XLS_Type_DBCELL: $this->_readDefault(); break;
  701. case self::XLS_Type_RK: $this->_readRk(); break;
  702. case self::XLS_Type_LABELSST: $this->_readLabelSst(); break;
  703. case self::XLS_Type_MULRK: $this->_readMulRk(); break;
  704. case self::XLS_Type_NUMBER: $this->_readNumber(); break;
  705. case self::XLS_Type_FORMULA: $this->_readFormula(); break;
  706. case self::XLS_Type_SHAREDFMLA: $this->_readSharedFmla(); break;
  707. case self::XLS_Type_BOOLERR: $this->_readBoolErr(); break;
  708. case self::XLS_Type_MULBLANK: $this->_readMulBlank(); break;
  709. case self::XLS_Type_LABEL: $this->_readLabel(); break;
  710. case self::XLS_Type_BLANK: $this->_readBlank(); break;
  711. case self::XLS_Type_MSODRAWING: $this->_readMsoDrawing(); break;
  712. case self::XLS_Type_OBJ: $this->_readObj(); break;
  713. case self::XLS_Type_WINDOW2: $this->_readWindow2(); break;
  714. case self::XLS_Type_PAGELAYOUTVIEW: $this->_readPageLayoutView(); break;
  715. case self::XLS_Type_SCL: $this->_readScl(); break;
  716. case self::XLS_Type_PANE: $this->_readPane(); break;
  717. case self::XLS_Type_SELECTION: $this->_readSelection(); break;
  718. case self::XLS_Type_MERGEDCELLS: $this->_readMergedCells(); break;
  719. case self::XLS_Type_HYPERLINK: $this->_readHyperLink(); break;
  720. case self::XLS_Type_DATAVALIDATIONS: $this->_readDataValidations(); break;
  721. case self::XLS_Type_DATAVALIDATION: $this->_readDataValidation(); break;
  722. case self::XLS_Type_SHEETLAYOUT: $this->_readSheetLayout(); break;
  723. case self::XLS_Type_SHEETPROTECTION: $this->_readSheetProtection(); break;
  724. case self::XLS_Type_RANGEPROTECTION: $this->_readRangeProtection(); break;
  725. case self::XLS_Type_NOTE: $this->_readNote(); break;
  726. //case self::XLS_Type_IMDATA: $this->_readImData(); break;
  727. case self::XLS_Type_TXO: $this->_readTextObject(); break;
  728. case self::XLS_Type_CONTINUE: $this->_readContinue(); break;
  729. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  730. default: $this->_readDefault(); break;
  731. }
  732. }
  733. // treat MSODRAWING records, sheet-level Escher
  734. if (!$this->_readDataOnly && $this->_drawingData) {
  735. $escherWorksheet = new PHPExcel_Shared_Escher();
  736. $reader = new PHPExcel_Reader_Excel5_Escher($escherWorksheet);
  737. $escherWorksheet = $reader->load($this->_drawingData);
  738. // debug Escher stream
  739. //$debug = new Debug_Escher(new PHPExcel_Shared_Escher());
  740. //$debug->load($this->_drawingData);
  741. // get all spContainers in one long array, so they can be mapped to OBJ records
  742. $allSpContainers = $escherWorksheet->getDgContainer()->getSpgrContainer()->getAllSpContainers();
  743. }
  744. // treat OBJ records
  745. foreach ($this->_objs as $n => $obj) {
  746. // echo '<hr /><b>Object</b> reference is ',$n,'<br />';
  747. // var_dump($obj);
  748. // echo '<br />';
  749. // the first shape container never has a corresponding OBJ record, hence $n + 1
  750. if (isset($allSpContainers[$n + 1]) && is_object($allSpContainers[$n + 1])) {
  751. $spContainer = $allSpContainers[$n + 1];
  752. // we skip all spContainers that are a part of a group shape since we cannot yet handle those
  753. if ($spContainer->getNestingLevel() > 1) {
  754. continue;
  755. }
  756. // calculate the width and height of the shape
  757. list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($spContainer->getStartCoordinates());
  758. list($endColumn, $endRow) = PHPExcel_Cell::coordinateFromString($spContainer->getEndCoordinates());
  759. $startOffsetX = $spContainer->getStartOffsetX();
  760. $startOffsetY = $spContainer->getStartOffsetY();
  761. $endOffsetX = $spContainer->getEndOffsetX();
  762. $endOffsetY = $spContainer->getEndOffsetY();
  763. $width = PHPExcel_Shared_Excel5::getDistanceX($this->_phpSheet, $startColumn, $startOffsetX, $endColumn, $endOffsetX);
  764. $height = PHPExcel_Shared_Excel5::getDistanceY($this->_phpSheet, $startRow, $startOffsetY, $endRow, $endOffsetY);
  765. // calculate offsetX and offsetY of the shape
  766. $offsetX = $startOffsetX * PHPExcel_Shared_Excel5::sizeCol($this->_phpSheet, $startColumn) / 1024;
  767. $offsetY = $startOffsetY * PHPExcel_Shared_Excel5::sizeRow($this->_phpSheet, $startRow) / 256;
  768. switch ($obj['otObjType']) {
  769. case 0x19:
  770. // Note
  771. // echo 'Cell Annotation Object<br />';
  772. // echo 'Object ID is ',$obj['idObjID'],'<br />';
  773. //
  774. if (isset($this->_cellNotes[$obj['idObjID']])) {
  775. $cellNote = $this->_cellNotes[$obj['idObjID']];
  776. if (isset($this->_textObjects[$obj['idObjID']])) {
  777. $textObject = $this->_textObjects[$obj['idObjID']];
  778. $this->_cellNotes[$obj['idObjID']]['objTextData'] = $textObject;
  779. }
  780. }
  781. break;
  782. case 0x08:
  783. // echo 'Picture Object<br />';
  784. // picture
  785. // get index to BSE entry (1-based)
  786. $BSEindex = $spContainer->getOPT(0x0104);
  787. $BSECollection = $escherWorkbook->getDggContainer()->getBstoreContainer()->getBSECollection();
  788. $BSE = $BSECollection[$BSEindex - 1];
  789. $blipType = $BSE->getBlipType();
  790. // need check because some blip types are not supported by Escher reader such as EMF
  791. if ($blip = $BSE->getBlip()) {
  792. $ih = imagecreatefromstring($blip->getData());
  793. $drawing = new PHPExcel_Worksheet_MemoryDrawing();
  794. $drawing->setImageResource($ih);
  795. // width, height, offsetX, offsetY
  796. $drawing->setResizeProportional(false);
  797. $drawing->setWidth($width);
  798. $drawing->setHeight($height);
  799. $drawing->setOffsetX($offsetX);
  800. $drawing->setOffsetY($offsetY);
  801. switch ($blipType) {
  802. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_JPEG:
  803. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
  804. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_JPEG);
  805. break;
  806. case PHPExcel_Shared_Escher_DggContainer_BstoreContainer_BSE::BLIPTYPE_PNG:
  807. $drawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
  808. $drawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_PNG);
  809. break;
  810. }
  811. $drawing->setWorksheet($this->_phpSheet);
  812. $drawing->setCoordinates($spContainer->getStartCoordinates());
  813. }
  814. break;
  815. default:
  816. // other object type
  817. break;
  818. }
  819. }
  820. }
  821. // treat SHAREDFMLA records
  822. if ($this->_version == self::XLS_BIFF8) {
  823. foreach ($this->_sharedFormulaParts as $cell => $baseCell) {
  824. list($column, $row) = PHPExcel_Cell::coordinateFromString($cell);
  825. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle()) ) {
  826. $formula = $this->_getFormulaFromStructure($this->_sharedFormulas[$baseCell], $cell);
  827. $this->_phpSheet->getCell($cell)->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  828. }
  829. }
  830. }
  831. if (!empty($this->_cellNotes)) {
  832. foreach($this->_cellNotes as $note => $noteDetails) {
  833. if (!isset($noteDetails['objTextData'])) {
  834. if (isset($this->_textObjects[$note])) {
  835. $textObject = $this->_textObjects[$note];
  836. $noteDetails['objTextData'] = $textObject;
  837. } else {
  838. $noteDetails['objTextData']['text'] = '';
  839. }
  840. }
  841. // echo '<b>Cell annotation ',$note,'</b><br />';
  842. // var_dump($noteDetails);
  843. // echo '<br />';
  844. $cellAddress = str_replace('$','',$noteDetails['cellRef']);
  845. $this->_phpSheet->getComment( $cellAddress )
  846. ->setAuthor( $noteDetails['author'] )
  847. ->setText($this->_parseRichText($noteDetails['objTextData']['text']) );
  848. }
  849. }
  850. }
  851. // add the named ranges (defined names)
  852. foreach ($this->_definedname as $definedName) {
  853. if ($definedName['isBuiltInName']) {
  854. switch ($definedName['name']) {
  855. case pack('C', 0x06):
  856. // print area
  857. // in general, formula looks like this: Foo!$C$7:$J$66,Bar!$A$1:$IV$2
  858. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  859. $extractedRanges = array();
  860. foreach ($ranges as $range) {
  861. // $range should look like one of these
  862. // Foo!$C$7:$J$66
  863. // Bar!$A$1:$IV$2
  864. $explodes = explode('!', $range); // FIXME: what if sheetname contains exclamation mark?
  865. $sheetName = trim($explodes[0], "'");
  866. if (count($explodes) == 2) {
  867. if (strpos($explodes[1], ':') === FALSE) {
  868. $explodes[1] = $explodes[1] . ':' . $explodes[1];
  869. }
  870. $extractedRanges[] = str_replace('$', '', $explodes[1]); // C7:J66
  871. }
  872. }
  873. if ($docSheet = $this->_phpExcel->getSheetByName($sheetName)) {
  874. $docSheet->getPageSetup()->setPrintArea(implode(',', $extractedRanges)); // C7:J66,A1:IV2
  875. }
  876. break;
  877. case pack('C', 0x07):
  878. // print titles (repeating rows)
  879. // Assuming BIFF8, there are 3 cases
  880. // 1. repeating rows
  881. // formula looks like this: Sheet!$A$1:$IV$2
  882. // rows 1-2 repeat
  883. // 2. repeating columns
  884. // formula looks like this: Sheet!$A$1:$B$65536
  885. // columns A-B repeat
  886. // 3. both repeating rows and repeating columns
  887. // formula looks like this: Sheet!$A$1:$B$65536,Sheet!$A$1:$IV$2
  888. $ranges = explode(',', $definedName['formula']); // FIXME: what if sheetname contains comma?
  889. foreach ($ranges as $range) {
  890. // $range should look like this one of these
  891. // Sheet!$A$1:$B$65536
  892. // Sheet!$A$1:$IV$2
  893. $explodes = explode('!', $range);
  894. if (count($explodes) == 2) {
  895. if ($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) {
  896. $extractedRange = $explodes[1];
  897. $extractedRange = str_replace('$', '', $extractedRange);
  898. $coordinateStrings = explode(':', $extractedRange);
  899. if (count($coordinateStrings) == 2) {
  900. list($firstColumn, $firstRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[0]);
  901. list($lastColumn, $lastRow) = PHPExcel_Cell::coordinateFromString($coordinateStrings[1]);
  902. if ($firstColumn == 'A' and $lastColumn == 'IV') {
  903. // then we have repeating rows
  904. $docSheet->getPageSetup()->setRowsToRepeatAtTop(array($firstRow, $lastRow));
  905. } elseif ($firstRow == 1 and $lastRow == 65536) {
  906. // then we have repeating columns
  907. $docSheet->getPageSetup()->setColumnsToRepeatAtLeft(array($firstColumn, $lastColumn));
  908. }
  909. }
  910. }
  911. }
  912. }
  913. break;
  914. }
  915. } else {
  916. // Extract range
  917. $explodes = explode('!', $definedName['formula']);
  918. if (count($explodes) == 2) {
  919. if (($docSheet = $this->_phpExcel->getSheetByName($explodes[0])) ||
  920. ($docSheet = $this->_phpExcel->getSheetByName(trim($explodes[0],"'")))) {
  921. $extractedRange = $explodes[1];
  922. $extractedRange = str_replace('$', '', $extractedRange);
  923. $localOnly = ($definedName['scope'] == 0) ? false : true;
  924. $scope = ($definedName['scope'] == 0) ?
  925. null : $this->_phpExcel->getSheetByName($this->_sheets[$definedName['scope'] - 1]['name']);
  926. $this->_phpExcel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $docSheet, $extractedRange, $localOnly, $scope) );
  927. }
  928. } else {
  929. // Named Value
  930. // TODO Provide support for named values
  931. }
  932. }
  933. }
  934. $this->_data = null;
  935. return $this->_phpExcel;
  936. }
  937. /**
  938. * Read record data from stream, decrypting as required
  939. *
  940. * @param string $data Data stream to read from
  941. * @param int $pos Position to start reading from
  942. * @param int $length Record data length
  943. *
  944. * @return string Record data
  945. */
  946. private function _readRecordData($data, $pos, $len)
  947. {
  948. $data = substr($data, $pos, $len);
  949. // File not encrypted, or record before encryption start point
  950. if ($this->_encryption == self::MS_BIFF_CRYPTO_NONE || $pos < $this->_encryptionStartPos) {
  951. return $data;
  952. }
  953. $recordData = '';
  954. if ($this->_encryption == self::MS_BIFF_CRYPTO_RC4) {
  955. $oldBlock = floor($this->_rc4Pos / self::REKEY_BLOCK);
  956. $block = floor($pos / self::REKEY_BLOCK);
  957. $endBlock = floor(($pos + $len) / self::REKEY_BLOCK);
  958. // Spin an RC4 decryptor to the right spot. If we have a decryptor sitting
  959. // at a point earlier in the current block, re-use it as we can save some time.
  960. if ($block != $oldBlock || $pos < $this->_rc4Pos || !$this->_rc4Key) {
  961. $this->_rc4Key = $this->_makeKey($block, $this->_md5Ctxt);
  962. $step = $pos % self::REKEY_BLOCK;
  963. } else {
  964. $step = $pos - $this->_rc4Pos;
  965. }
  966. $this->_rc4Key->RC4(str_repeat("\0", $step));
  967. // Decrypt record data (re-keying at the end of every block)
  968. while ($block != $endBlock) {
  969. $step = self::REKEY_BLOCK - ($pos % self::REKEY_BLOCK);
  970. $recordData .= $this->_rc4Key->RC4(substr($data, 0, $step));
  971. $data = substr($data, $step);
  972. $pos += $step;
  973. $len -= $step;
  974. $block++;
  975. $this->_rc4Key = $this->_makeKey($block, $this->_md5Ctxt);
  976. }
  977. $recordData .= $this->_rc4Key->RC4(substr($data, 0, $len));
  978. // Keep track of the position of this decryptor.
  979. // We'll try and re-use it later if we can to speed things up
  980. $this->_rc4Pos = $pos + $len;
  981. } elseif ($this->_encryption == self::MS_BIFF_CRYPTO_XOR) {
  982. throw new PHPExcel_Reader_Exception('XOr encryption not supported');
  983. }
  984. return $recordData;
  985. }
  986. /**
  987. * Use OLE reader to extract the relevant data streams from the OLE file
  988. *
  989. * @param string $pFilename
  990. */
  991. private function _loadOLE($pFilename)
  992. {
  993. // OLE reader
  994. $ole = new PHPExcel_Shared_OLERead();
  995. // get excel data,
  996. $res = $ole->read($pFilename);
  997. // Get workbook data: workbook stream + sheet streams
  998. $this->_data = $ole->getStream($ole->wrkbook);
  999. // Get summary information data
  1000. $this->_summaryInformation = $ole->getStream($ole->summaryInformation);
  1001. // Get additional document summary information data
  1002. $this->_documentSummaryInformation = $ole->getStream($ole->documentSummaryInformation);
  1003. // Get user-defined property data
  1004. // $this->_userDefinedProperties = $ole->getUserDefinedProperties();
  1005. }
  1006. /**
  1007. * Read summary information
  1008. */
  1009. private function _readSummaryInformation()
  1010. {
  1011. if (!isset($this->_summaryInformation)) {
  1012. return;
  1013. }
  1014. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  1015. // offset: 2; size: 2;
  1016. // offset: 4; size: 2; OS version
  1017. // offset: 6; size: 2; OS indicator
  1018. // offset: 8; size: 16
  1019. // offset: 24; size: 4; section count
  1020. $secCount = self::_GetInt4d($this->_summaryInformation, 24);
  1021. // offset: 28; size: 16; first section's class id: e0 85 9f f2 f9 4f 68 10 ab 91 08 00 2b 27 b3 d9
  1022. // offset: 44; size: 4
  1023. $secOffset = self::_GetInt4d($this->_summaryInformation, 44);
  1024. // section header
  1025. // offset: $secOffset; size: 4; section length
  1026. $secLength = self::_GetInt4d($this->_summaryInformation, $secOffset);
  1027. // offset: $secOffset+4; size: 4; property count
  1028. $countProperties = self::_GetInt4d($this->_summaryInformation, $secOffset+4);
  1029. // initialize code page (used to resolve string values)
  1030. $codePage = 'CP1252';
  1031. // offset: ($secOffset+8); size: var
  1032. // loop through property decarations and properties
  1033. for ($i = 0; $i < $countProperties; ++$i) {
  1034. // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
  1035. $id = self::_GetInt4d($this->_summaryInformation, ($secOffset+8) + (8 * $i));
  1036. // Use value of property id as appropriate
  1037. // offset: ($secOffset+12) + (8 * $i); size: 4; offset from beginning of section (48)
  1038. $offset = self::_GetInt4d($this->_summaryInformation, ($secOffset+12) + (8 * $i));
  1039. $type = self::_GetInt4d($this->_summaryInformation, $secOffset + $offset);
  1040. // initialize property value
  1041. $value = null;
  1042. // extract property value based on property type
  1043. switch ($type) {
  1044. case 0x02: // 2 byte signed integer
  1045. $value = self::_GetInt2d($this->_summaryInformation, $secOffset + 4 + $offset);
  1046. break;
  1047. case 0x03: // 4 byte signed integer
  1048. $value = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
  1049. break;
  1050. case 0x13: // 4 byte unsigned integer
  1051. // not needed yet, fix later if necessary
  1052. break;
  1053. case 0x1E: // null-terminated string prepended by dword string length
  1054. $byteLength = self::_GetInt4d($this->_summaryInformation, $secOffset + 4 + $offset);
  1055. $value = substr($this->_summaryInformation, $secOffset + 8 + $offset, $byteLength);
  1056. $value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
  1057. $value = rtrim($value);
  1058. break;
  1059. case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  1060. // PHP-time
  1061. $value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_summaryInformation, $secOffset + 4 + $offset, 8));
  1062. break;
  1063. case 0x47: // Clipboard format
  1064. // not needed yet, fix later if necessary
  1065. break;
  1066. }
  1067. switch ($id) {
  1068. case 0x01: // Code Page
  1069. $codePage = PHPExcel_Shared_CodePage::NumberToName($value);
  1070. break;
  1071. case 0x02: // Title
  1072. $this->_phpExcel->getProperties()->setTitle($value);
  1073. break;
  1074. case 0x03: // Subject
  1075. $this->_phpExcel->getProperties()->setSubject($value);
  1076. break;
  1077. case 0x04: // Author (Creator)
  1078. $this->_phpExcel->getProperties()->setCreator($value);
  1079. break;
  1080. case 0x05: // Keywords
  1081. $this->_phpExcel->getProperties()->setKeywords($value);
  1082. break;
  1083. case 0x06: // Comments (Description)
  1084. $this->_phpExcel->getProperties()->setDescription($value);
  1085. break;
  1086. case 0x07: // Template
  1087. // Not supported by PHPExcel
  1088. break;
  1089. case 0x08: // Last Saved By (LastModifiedBy)
  1090. $this->_phpExcel->getProperties()->setLastModifiedBy($value);
  1091. break;
  1092. case 0x09: // Revision
  1093. // Not supported by PHPExcel
  1094. break;
  1095. case 0x0A: // Total Editing Time
  1096. // Not supported by PHPExcel
  1097. break;
  1098. case 0x0B: // Last Printed
  1099. // Not supported by PHPExcel
  1100. break;
  1101. case 0x0C: // Created Date/Time
  1102. $this->_phpExcel->getProperties()->setCreated($value);
  1103. break;
  1104. case 0x0D: // Modified Date/Time
  1105. $this->_phpExcel->getProperties()->setModified($value);
  1106. break;
  1107. case 0x0E: // Number of Pages
  1108. // Not supported by PHPExcel
  1109. break;
  1110. case 0x0F: // Number of Words
  1111. // Not supported by PHPExcel
  1112. break;
  1113. case 0x10: // Number of Characters
  1114. // Not supported by PHPExcel
  1115. break;
  1116. case 0x11: // Thumbnail
  1117. // Not supported by PHPExcel
  1118. break;
  1119. case 0x12: // Name of creating application
  1120. // Not supported by PHPExcel
  1121. break;
  1122. case 0x13: // Security
  1123. // Not supported by PHPExcel
  1124. break;
  1125. }
  1126. }
  1127. }
  1128. /**
  1129. * Read additional document summary information
  1130. */
  1131. private function _readDocumentSummaryInformation()
  1132. {
  1133. if (!isset($this->_documentSummaryInformation)) {
  1134. return;
  1135. }
  1136. // offset: 0; size: 2; must be 0xFE 0xFF (UTF-16 LE byte order mark)
  1137. // offset: 2; size: 2;
  1138. // offset: 4; size: 2; OS version
  1139. // offset: 6; size: 2; OS indicator
  1140. // offset: 8; size: 16
  1141. // offset: 24; size: 4; section count
  1142. $secCount = self::_GetInt4d($this->_documentSummaryInformation, 24);
  1143. // echo '$secCount = ',$secCount,'<br />';
  1144. // offset: 28; size: 16; first section's class id: 02 d5 cd d5 9c 2e 1b 10 93 97 08 00 2b 2c f9 ae
  1145. // offset: 44; size: 4; first section offset
  1146. $secOffset = self::_GetInt4d($this->_documentSummaryInformation, 44);
  1147. // echo '$secOffset = ',$secOffset,'<br />';
  1148. // section header
  1149. // offset: $secOffset; size: 4; section length
  1150. $secLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset);
  1151. // echo '$secLength = ',$secLength,'<br />';
  1152. // offset: $secOffset+4; size: 4; property count
  1153. $countProperties = self::_GetInt4d($this->_documentSummaryInformation, $secOffset+4);
  1154. // echo '$countProperties = ',$countProperties,'<br />';
  1155. // initialize code page (used to resolve string values)
  1156. $codePage = 'CP1252';
  1157. // offset: ($secOffset+8); size: var
  1158. // loop through property decarations and properties
  1159. for ($i = 0; $i < $countProperties; ++$i) {
  1160. // echo 'Property ',$i,'<br />';
  1161. // offset: ($secOffset+8) + (8 * $i); size: 4; property ID
  1162. $id = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+8) + (8 * $i));
  1163. // echo 'ID is ',$id,'<br />';
  1164. // Use value of property id as appropriate
  1165. // offset: 60 + 8 * $i; size: 4; offset from beginning of section (48)
  1166. $offset = self::_GetInt4d($this->_documentSummaryInformation, ($secOffset+12) + (8 * $i));
  1167. $type = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + $offset);
  1168. // echo 'Type is ',$type,', ';
  1169. // initialize property value
  1170. $value = null;
  1171. // extract property value based on property type
  1172. switch ($type) {
  1173. case 0x02: // 2 byte signed integer
  1174. $value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1175. break;
  1176. case 0x03: // 4 byte signed integer
  1177. $value = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1178. break;
  1179. case 0x0B: // Boolean
  1180. $value = self::_GetInt2d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1181. $value = ($value == 0 ? false : true);
  1182. break;
  1183. case 0x13: // 4 byte unsigned integer
  1184. // not needed yet, fix later if necessary
  1185. break;
  1186. case 0x1E: // null-terminated string prepended by dword string length
  1187. $byteLength = self::_GetInt4d($this->_documentSummaryInformation, $secOffset + 4 + $offset);
  1188. $value = substr($this->_documentSummaryInformation, $secOffset + 8 + $offset, $byteLength);
  1189. $value = PHPExcel_Shared_String::ConvertEncoding($value, 'UTF-8', $codePage);
  1190. $value = rtrim($value);
  1191. break;
  1192. case 0x40: // Filetime (64-bit value representing the number of 100-nanosecond intervals since January 1, 1601)
  1193. // PHP-Time
  1194. $value = PHPExcel_Shared_OLE::OLE2LocalDate(substr($this->_documentSummaryInformation, $secOffset + 4 + $offset, 8));
  1195. break;
  1196. case 0x47: // Clipboard format
  1197. // not needed yet, fix later if necessary
  1198. break;
  1199. }
  1200. switch ($id) {
  1201. case 0x01: // Code Page
  1202. $codePage = PHPExcel_Shared_CodePage::NumberToName($value);
  1203. break;
  1204. case 0x02: // Category
  1205. $this->_phpExcel->getProperties()->setCategory($value);
  1206. break;
  1207. case 0x03: // Presentation Target
  1208. // Not supported by PHPExcel
  1209. break;
  1210. case 0x04: // Bytes
  1211. // Not supported by PHPExcel
  1212. break;
  1213. case 0x05: // Lines
  1214. // Not supported by PHPExcel
  1215. break;
  1216. case 0x06: // Paragraphs
  1217. // Not supported by PHPExcel
  1218. break;
  1219. case 0x07: // Slides
  1220. // Not supported by PHPExcel
  1221. break;
  1222. case 0x08: // Notes
  1223. // Not supported by PHPExcel
  1224. break;
  1225. case 0x09: // Hidden Slides
  1226. // Not supported by PHPExcel
  1227. break;
  1228. case 0x0A: // MM Clips
  1229. // Not supported by PHPExcel
  1230. break;
  1231. case 0x0B: // Scale Crop
  1232. // Not supported by PHPExcel
  1233. break;
  1234. case 0x0C: // Heading Pairs
  1235. // Not supported by PHPExcel
  1236. break;
  1237. case 0x0D: // Titles of Parts
  1238. // Not supported by PHPExcel
  1239. break;
  1240. case 0x0E: // Manager
  1241. $this->_phpExcel->getProperties()->setManager($value);
  1242. break;
  1243. case 0x0F: // Company
  1244. $this->_phpExcel->getProperties()->setCompany($value);
  1245. break;
  1246. case 0x10: // Links up-to-date
  1247. // Not supported by PHPExcel
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. /**
  1253. * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
  1254. */
  1255. private function _readDefault()
  1256. {
  1257. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1258. // $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1259. // move stream pointer to next record
  1260. $this->_pos += 4 + $length;
  1261. }
  1262. /**
  1263. * The NOTE record specifies a comment associated with a particular cell. In Excel 95 (BIFF7) and earlier versions,
  1264. * this record stores a note (cell note). This feature was significantly enhanced in Excel 97.
  1265. */
  1266. private function _readNote()
  1267. {
  1268. // echo '<b>Read Cell Annotation</b><br />';
  1269. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1270. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1271. // move stream pointer to next record
  1272. $this->_pos += 4 + $length;
  1273. if ($this->_readDataOnly) {
  1274. return;
  1275. }
  1276. $cellAddress = $this->_readBIFF8CellAddress(substr($recordData, 0, 4));
  1277. if ($this->_version == self::XLS_BIFF8) {
  1278. $noteObjID = self::_GetInt2d($recordData, 6);
  1279. $noteAuthor = self::_readUnicodeStringLong(substr($recordData, 8));
  1280. $noteAuthor = $noteAuthor['value'];
  1281. // echo 'Note Address=',$cellAddress,'<br />';
  1282. // echo 'Note Object ID=',$noteObjID,'<br />';
  1283. // echo 'Note Author=',$noteAuthor,'<hr />';
  1284. //
  1285. $this->_cellNotes[$noteObjID] = array('cellRef' => $cellAddress,
  1286. 'objectID' => $noteObjID,
  1287. 'author' => $noteAuthor
  1288. );
  1289. } else {
  1290. $extension = false;
  1291. if ($cellAddress == '$B$65536') {
  1292. // If the address row is -1 and the column is 0, (which translates as $B$65536) then this is a continuation
  1293. // note from the previous cell annotation. We're not yet handling this, so annotations longer than the
  1294. // max 2048 bytes will probably throw a wobbly.
  1295. $row = self::_GetInt2d($recordData, 0);
  1296. $extension = true;
  1297. $cellAddress = array_pop(array_keys($this->_phpSheet->getComments()));
  1298. }
  1299. // echo 'Note Address=',$cellAddress,'<br />';
  1300. $cellAddress = str_replace('$','',$cellAddress);
  1301. $noteLength = self::_GetInt2d($recordData, 4);
  1302. $noteText = trim(substr($recordData, 6));
  1303. // echo 'Note Length=',$noteLength,'<br />';
  1304. // echo 'Note Text=',$noteText,'<br />';
  1305. if ($extension) {
  1306. // Concatenate this extension with the currently set comment for the cell
  1307. $comment = $this->_phpSheet->getComment( $cellAddress );
  1308. $commentText = $comment->getText()->getPlainText();
  1309. $comment->setText($this->_parseRichText($commentText.$noteText) );
  1310. } else {
  1311. // Set comment for the cell
  1312. $this->_phpSheet->getComment( $cellAddress )
  1313. // ->setAuthor( $author )
  1314. ->setText($this->_parseRichText($noteText) );
  1315. }
  1316. }
  1317. }
  1318. /**
  1319. * The TEXT Object record contains the text associated with a cell annotation.
  1320. */
  1321. private function _readTextObject()
  1322. {
  1323. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1324. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1325. // move stream pointer to next record
  1326. $this->_pos += 4 + $length;
  1327. if ($this->_readDataOnly) {
  1328. return;
  1329. }
  1330. // recordData consists of an array of subrecords looking like this:
  1331. // grbit: 2 bytes; Option Flags
  1332. // rot: 2 bytes; rotation
  1333. // cchText: 2 bytes; length of the text (in the first continue record)
  1334. // cbRuns: 2 bytes; length of the formatting (in the second continue record)
  1335. // followed by the continuation records containing the actual text and formatting
  1336. $grbitOpts = self::_GetInt2d($recordData, 0);
  1337. $rot = self::_GetInt2d($recordData, 2);
  1338. $cchText = self::_GetInt2d($recordData, 10);
  1339. $cbRuns = self::_GetInt2d($recordData, 12);
  1340. $text = $this->_getSplicedRecordData();
  1341. $this->_textObjects[$this->textObjRef] = array(
  1342. 'text' => substr($text["recordData"],$text["spliceOffsets"][0]+1,$cchText),
  1343. 'format' => substr($text["recordData"],$text["spliceOffsets"][1],$cbRuns),
  1344. 'alignment' => $grbitOpts,
  1345. 'rotation' => $rot
  1346. );
  1347. // echo '<b>_readTextObject()</b><br />';
  1348. // var_dump($this->_textObjects[$this->textObjRef]);
  1349. // echo '<br />';
  1350. }
  1351. /**
  1352. * Read BOF
  1353. */
  1354. private function _readBof()
  1355. {
  1356. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1357. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1358. // move stream pointer to next record
  1359. $this->_pos += 4 + $length;
  1360. // offset: 2; size: 2; type of the following data
  1361. $substreamType = self::_GetInt2d($recordData, 2);
  1362. switch ($substreamType) {
  1363. case self::XLS_WorkbookGlobals:
  1364. $version = self::_GetInt2d($recordData, 0);
  1365. if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
  1366. throw new PHPExcel_Reader_Exception('Cannot read this Excel file. Version is too old.');
  1367. }
  1368. $this->_version = $version;
  1369. break;
  1370. case self::XLS_Worksheet:
  1371. // do not use this version information for anything
  1372. // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
  1373. break;
  1374. default:
  1375. // substream, e.g. chart
  1376. // just skip the entire substream
  1377. do {
  1378. $code = self::_GetInt2d($this->_data, $this->_pos);
  1379. $this->_readDefault();
  1380. } while ($code != self::XLS_Type_EOF && $this->_pos < $this->_dataSize);
  1381. break;
  1382. }
  1383. }
  1384. /**
  1385. * FILEPASS
  1386. *
  1387. * This record is part of the File Protection Block. It
  1388. * contains information about the read/write password of the
  1389. * file. All record contents following this record will be
  1390. * encrypted.
  1391. *
  1392. * -- "OpenOffice.org's Documentation of the Microsoft
  1393. * Excel File Format"
  1394. *
  1395. * The decryption functions and objects used from here on in
  1396. * are based on the source of Spreadsheet-ParseExcel:
  1397. * http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel/
  1398. */
  1399. private function _readFilepass()
  1400. {
  1401. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1402. if ($length != 54) {
  1403. throw new PHPExcel_Reader_Exception('Unexpected file pass record length');
  1404. }
  1405. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1406. // move stream pointer to next record
  1407. $this->_pos += 4 + $length;
  1408. if (!$this->_verifyPassword(
  1409. 'VelvetSweatshop',
  1410. substr($recordData, 6, 16),
  1411. substr($recordData, 22, 16),
  1412. substr($recordData, 38, 16),
  1413. $this->_md5Ctxt
  1414. )) {
  1415. throw new PHPExcel_Reader_Exception('Decryption password incorrect');
  1416. }
  1417. $this->_encryption = self::MS_BIFF_CRYPTO_RC4;
  1418. // Decryption required from the record after next onwards
  1419. $this->_encryptionStartPos = $this->_pos + self::_GetInt2d($this->_data, $this->_pos + 2);
  1420. }
  1421. /**
  1422. * Make an RC4 decryptor for the given block
  1423. *
  1424. * @var int $block Block for which to create decrypto
  1425. * @var string $valContext MD5 context state
  1426. *
  1427. * @return PHPExcel_Reader_Excel5_RC4
  1428. */
  1429. private function _makeKey($block, $valContext)
  1430. {
  1431. $pwarray = str_repeat("\0", 64);
  1432. for ($i = 0; $i < 5; $i++) {
  1433. $pwarray[$i] = $valContext[$i];
  1434. }
  1435. $pwarray[5] = chr($block & 0xff);
  1436. $pwarray[6] = chr(($block >> 8) & 0xff);
  1437. $pwarray[7] = chr(($block >> 16) & 0xff);
  1438. $pwarray[8] = chr(($block >> 24) & 0xff);
  1439. $pwarray[9] = "\x80";
  1440. $pwarray[56] = "\x48";
  1441. $md5 = new PHPExcel_Reader_Excel5_MD5();
  1442. $md5->add($pwarray);
  1443. $s = $md5->getContext();
  1444. return new PHPExcel_Reader_Excel5_RC4($s);
  1445. }
  1446. /**
  1447. * Verify RC4 file password
  1448. *
  1449. * @var string $password Password to check
  1450. * @var string $docid Document id
  1451. * @var string $salt_data Salt data
  1452. * @var string $hashedsalt_data Hashed salt data
  1453. * @var string &$valContext Set to the MD5 context of the value
  1454. *
  1455. * @return bool Success
  1456. */
  1457. private function _verifyPassword($password, $docid, $salt_data, $hashedsalt_data, &$valContext)
  1458. {
  1459. $pwarray = str_repeat("\0", 64);
  1460. for ($i = 0; $i < strlen($password); $i++) {
  1461. $o = ord(substr($password, $i, 1));
  1462. $pwarray[2 * $i] = chr($o & 0xff);
  1463. $pwarray[2 * $i + 1] = chr(($o >> 8) & 0xff);
  1464. }
  1465. $pwarray[2 * $i] = chr(0x80);
  1466. $pwarray[56] = chr(($i << 4) & 0xff);
  1467. $md5 = new PHPExcel_Reader_Excel5_MD5();
  1468. $md5->add($pwarray);
  1469. $mdContext1 = $md5->getContext();
  1470. $offset = 0;
  1471. $keyoffset = 0;
  1472. $tocopy = 5;
  1473. $md5->reset();
  1474. while ($offset != 16) {
  1475. if ((64 - $offset) < 5) {
  1476. $tocopy = 64 - $offset;
  1477. }
  1478. for ($i = 0; $i <= $tocopy; $i++) {
  1479. $pwarray[$offset + $i] = $mdContext1[$keyoffset + $i];
  1480. }
  1481. $offset += $tocopy;
  1482. if ($offset == 64) {
  1483. $md5->add($pwarray);
  1484. $keyoffset = $tocopy;
  1485. $tocopy = 5 - $tocopy;
  1486. $offset = 0;
  1487. continue;
  1488. }
  1489. $keyoffset = 0;
  1490. $tocopy = 5;
  1491. for ($i = 0; $i < 16; $i++) {
  1492. $pwarray[$offset + $i] = $docid[$i];
  1493. }
  1494. $offset += 16;
  1495. }
  1496. $pwarray[16] = "\x80";
  1497. for ($i = 0; $i < 47; $i++) {
  1498. $pwarray[17 + $i] = "\0";
  1499. }
  1500. $pwarray[56] = "\x80";
  1501. $pwarray[57] = "\x0a";
  1502. $md5->add($pwarray);
  1503. $valContext = $md5->getContext();
  1504. $key = $this->_makeKey(0, $valContext);
  1505. $salt = $key->RC4($salt_data);
  1506. $hashedsalt = $key->RC4($hashedsalt_data);
  1507. $salt .= "\x80" . str_repeat("\0", 47);
  1508. $salt[56] = "\x80";
  1509. $md5->reset();
  1510. $md5->add($salt);
  1511. $mdContext2 = $md5->getContext();
  1512. return $mdContext2 == $hashedsalt;
  1513. }
  1514. /**
  1515. * CODEPAGE
  1516. *
  1517. * This record stores the text encoding used to write byte
  1518. * strings, stored as MS Windows code page identifier.
  1519. *
  1520. * -- "OpenOffice.org's Documentation of the Microsoft
  1521. * Excel File Format"
  1522. */
  1523. private function _readCodepage()
  1524. {
  1525. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1526. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1527. // move stream pointer to next record
  1528. $this->_pos += 4 + $length;
  1529. // offset: 0; size: 2; code page identifier
  1530. $codepage = self::_GetInt2d($recordData, 0);
  1531. $this->_codepage = PHPExcel_Shared_CodePage::NumberToName($codepage);
  1532. }
  1533. /**
  1534. * DATEMODE
  1535. *
  1536. * This record specifies the base date for displaying date
  1537. * values. All dates are stored as count of days past this
  1538. * base date. In BIFF2-BIFF4 this record is part of the
  1539. * Calculation Settings Block. In BIFF5-BIFF8 it is
  1540. * stored in the Workbook Globals Substream.
  1541. *
  1542. * -- "OpenOffice.org's Documentation of the Microsoft
  1543. * Excel File Format"
  1544. */
  1545. private function _readDateMode()
  1546. {
  1547. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1548. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1549. // move stream pointer to next record
  1550. $this->_pos += 4 + $length;
  1551. // offset: 0; size: 2; 0 = base 1900, 1 = base 1904
  1552. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_WINDOWS_1900);
  1553. if (ord($recordData{0}) == 1) {
  1554. PHPExcel_Shared_Date::setExcelCalendar(PHPExcel_Shared_Date::CALENDAR_MAC_1904);
  1555. }
  1556. }
  1557. /**
  1558. * Read a FONT record
  1559. */
  1560. private function _readFont()
  1561. {
  1562. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1563. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1564. // move stream pointer to next record
  1565. $this->_pos += 4 + $length;
  1566. if (!$this->_readDataOnly) {
  1567. $objFont = new PHPExcel_Style_Font();
  1568. // offset: 0; size: 2; height of the font (in twips = 1/20 of a point)
  1569. $size = self::_GetInt2d($recordData, 0);
  1570. $objFont->setSize($size / 20);
  1571. // offset: 2; size: 2; option flags
  1572. // bit: 0; mask 0x0001; bold (redundant in BIFF5-BIFF8)
  1573. // bit: 1; mask 0x0002; italic
  1574. $isItalic = (0x0002 & self::_GetInt2d($recordData, 2)) >> 1;
  1575. if ($isItalic) $objFont->setItalic(true);
  1576. // bit: 2; mask 0x0004; underlined (redundant in BIFF5-BIFF8)
  1577. // bit: 3; mask 0x0008; strike
  1578. $isStrike = (0x0008 & self::_GetInt2d($recordData, 2)) >> 3;
  1579. if ($isStrike) $objFont->setStrikethrough(true);
  1580. // offset: 4; size: 2; colour index
  1581. $colorIndex = self::_GetInt2d($recordData, 4);
  1582. $objFont->colorIndex = $colorIndex;
  1583. // offset: 6; size: 2; font weight
  1584. $weight = self::_GetInt2d($recordData, 6);
  1585. switch ($weight) {
  1586. case 0x02BC:
  1587. $objFont->setBold(true);
  1588. break;
  1589. }
  1590. // offset: 8; size: 2; escapement type
  1591. $escapement = self::_GetInt2d($recordData, 8);
  1592. switch ($escapement) {
  1593. case 0x0001:
  1594. $objFont->setSuperScript(true);
  1595. break;
  1596. case 0x0002:
  1597. $objFont->setSubScript(true);
  1598. break;
  1599. }
  1600. // offset: 10; size: 1; underline type
  1601. $underlineType = ord($recordData{10});
  1602. switch ($underlineType) {
  1603. case 0x00:
  1604. break; // no underline
  1605. case 0x01:
  1606. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  1607. break;
  1608. case 0x02:
  1609. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLE);
  1610. break;
  1611. case 0x21:
  1612. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING);
  1613. break;
  1614. case 0x22:
  1615. $objFont->setUnderline(PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING);
  1616. break;
  1617. }
  1618. // offset: 11; size: 1; font family
  1619. // offset: 12; size: 1; character set
  1620. // offset: 13; size: 1; not used
  1621. // offset: 14; size: var; font name
  1622. if ($this->_version == self::XLS_BIFF8) {
  1623. $string = self::_readUnicodeStringShort(substr($recordData, 14));
  1624. } else {
  1625. $string = $this->_readByteStringShort(substr($recordData, 14));
  1626. }
  1627. $objFont->setName($string['value']);
  1628. $this->_objFonts[] = $objFont;
  1629. }
  1630. }
  1631. /**
  1632. * FORMAT
  1633. *
  1634. * This record contains information about a number format.
  1635. * All FORMAT records occur together in a sequential list.
  1636. *
  1637. * In BIFF2-BIFF4 other records referencing a FORMAT record
  1638. * contain a zero-based index into this list. From BIFF5 on
  1639. * the FORMAT record contains the index itself that will be
  1640. * used by other records.
  1641. *
  1642. * -- "OpenOffice.org's Documentation of the Microsoft
  1643. * Excel File Format"
  1644. */
  1645. private function _readFormat()
  1646. {
  1647. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1648. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1649. // move stream pointer to next record
  1650. $this->_pos += 4 + $length;
  1651. if (!$this->_readDataOnly) {
  1652. $indexCode = self::_GetInt2d($recordData, 0);
  1653. if ($this->_version == self::XLS_BIFF8) {
  1654. $string = self::_readUnicodeStringLong(substr($recordData, 2));
  1655. } else {
  1656. // BIFF7
  1657. $string = $this->_readByteStringShort(substr($recordData, 2));
  1658. }
  1659. $formatString = $string['value'];
  1660. $this->_formats[$indexCode] = $formatString;
  1661. }
  1662. }
  1663. /**
  1664. * XF - Extended Format
  1665. *
  1666. * This record contains formatting information for cells, rows, columns or styles.
  1667. * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF
  1668. * and 1 cell XF.
  1669. * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF
  1670. * and XF record 15 is a cell XF
  1671. * We only read the first cell style XF and skip the remaining cell style XF records
  1672. * We read all cell XF records.
  1673. *
  1674. * -- "OpenOffice.org's Documentation of the Microsoft
  1675. * Excel File Format"
  1676. */
  1677. private function _readXf()
  1678. {
  1679. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1680. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1681. // move stream pointer to next record
  1682. $this->_pos += 4 + $length;
  1683. $objStyle = new PHPExcel_Style();
  1684. if (!$this->_readDataOnly) {
  1685. // offset: 0; size: 2; Index to FONT record
  1686. if (self::_GetInt2d($recordData, 0) < 4) {
  1687. $fontIndex = self::_GetInt2d($recordData, 0);
  1688. } else {
  1689. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  1690. // check the OpenOffice documentation of the FONT record
  1691. $fontIndex = self::_GetInt2d($recordData, 0) - 1;
  1692. }
  1693. $objStyle->setFont($this->_objFonts[$fontIndex]);
  1694. // offset: 2; size: 2; Index to FORMAT record
  1695. $numberFormatIndex = self::_GetInt2d($recordData, 2);
  1696. if (isset($this->_formats[$numberFormatIndex])) {
  1697. // then we have user-defined format code
  1698. $numberformat = array('code' => $this->_formats[$numberFormatIndex]);
  1699. } elseif (($code = PHPExcel_Style_NumberFormat::builtInFormatCode($numberFormatIndex)) !== '') {
  1700. // then we have built-in format code
  1701. $numberformat = array('code' => $code);
  1702. } else {
  1703. // we set the general format code
  1704. $numberformat = array('code' => 'General');
  1705. }
  1706. $objStyle->getNumberFormat()->setFormatCode($numberformat['code']);
  1707. // offset: 4; size: 2; XF type, cell protection, and parent style XF
  1708. // bit 2-0; mask 0x0007; XF_TYPE_PROT
  1709. $xfTypeProt = self::_GetInt2d($recordData, 4);
  1710. // bit 0; mask 0x01; 1 = cell is locked
  1711. $isLocked = (0x01 & $xfTypeProt) >> 0;
  1712. $objStyle->getProtection()->setLocked($isLocked ?
  1713. PHPExcel_Style_Protection::PROTECTION_INHERIT : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1714. // bit 1; mask 0x02; 1 = Formula is hidden
  1715. $isHidden = (0x02 & $xfTypeProt) >> 1;
  1716. $objStyle->getProtection()->setHidden($isHidden ?
  1717. PHPExcel_Style_Protection::PROTECTION_PROTECTED : PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
  1718. // bit 2; mask 0x04; 0 = Cell XF, 1 = Cell Style XF
  1719. $isCellStyleXf = (0x04 & $xfTypeProt) >> 2;
  1720. // offset: 6; size: 1; Alignment and text break
  1721. // bit 2-0, mask 0x07; horizontal alignment
  1722. $horAlign = (0x07 & ord($recordData{6})) >> 0;
  1723. switch ($horAlign) {
  1724. case 0:
  1725. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_GENERAL);
  1726. break;
  1727. case 1:
  1728. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1729. break;
  1730. case 2:
  1731. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1732. break;
  1733. case 3:
  1734. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  1735. break;
  1736. case 4:
  1737. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_FILL);
  1738. break;
  1739. case 5:
  1740. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  1741. break;
  1742. case 6:
  1743. $objStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS);
  1744. break;
  1745. }
  1746. // bit 3, mask 0x08; wrap text
  1747. $wrapText = (0x08 & ord($recordData{6})) >> 3;
  1748. switch ($wrapText) {
  1749. case 0:
  1750. $objStyle->getAlignment()->setWrapText(false);
  1751. break;
  1752. case 1:
  1753. $objStyle->getAlignment()->setWrapText(true);
  1754. break;
  1755. }
  1756. // bit 6-4, mask 0x70; vertical alignment
  1757. $vertAlign = (0x70 & ord($recordData{6})) >> 4;
  1758. switch ($vertAlign) {
  1759. case 0:
  1760. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
  1761. break;
  1762. case 1:
  1763. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1764. break;
  1765. case 2:
  1766. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_BOTTOM);
  1767. break;
  1768. case 3:
  1769. $objStyle->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_JUSTIFY);
  1770. break;
  1771. }
  1772. if ($this->_version == self::XLS_BIFF8) {
  1773. // offset: 7; size: 1; XF_ROTATION: Text rotation angle
  1774. $angle = ord($recordData{7});
  1775. $rotation = 0;
  1776. if ($angle <= 90) {
  1777. $rotation = $angle;
  1778. } else if ($angle <= 180) {
  1779. $rotation = 90 - $angle;
  1780. } else if ($angle == 255) {
  1781. $rotation = -165;
  1782. }
  1783. $objStyle->getAlignment()->setTextRotation($rotation);
  1784. // offset: 8; size: 1; Indentation, shrink to cell size, and text direction
  1785. // bit: 3-0; mask: 0x0F; indent level
  1786. $indent = (0x0F & ord($recordData{8})) >> 0;
  1787. $objStyle->getAlignment()->setIndent($indent);
  1788. // bit: 4; mask: 0x10; 1 = shrink content to fit into cell
  1789. $shrinkToFit = (0x10 & ord($recordData{8})) >> 4;
  1790. switch ($shrinkToFit) {
  1791. case 0:
  1792. $objStyle->getAlignment()->setShrinkToFit(false);
  1793. break;
  1794. case 1:
  1795. $objStyle->getAlignment()->setShrinkToFit(true);
  1796. break;
  1797. }
  1798. // offset: 9; size: 1; Flags used for attribute groups
  1799. // offset: 10; size: 4; Cell border lines and background area
  1800. // bit: 3-0; mask: 0x0000000F; left style
  1801. if ($bordersLeftStyle = self::_mapBorderStyle((0x0000000F & self::_GetInt4d($recordData, 10)) >> 0)) {
  1802. $objStyle->getBorders()->getLeft()->setBorderStyle($bordersLeftStyle);
  1803. }
  1804. // bit: 7-4; mask: 0x000000F0; right style
  1805. if ($bordersRightStyle = self::_mapBorderStyle((0x000000F0 & self::_GetInt4d($recordData, 10)) >> 4)) {
  1806. $objStyle->getBorders()->getRight()->setBorderStyle($bordersRightStyle);
  1807. }
  1808. // bit: 11-8; mask: 0x00000F00; top style
  1809. if ($bordersTopStyle = self::_mapBorderStyle((0x00000F00 & self::_GetInt4d($recordData, 10)) >> 8)) {
  1810. $objStyle->getBorders()->getTop()->setBorderStyle($bordersTopStyle);
  1811. }
  1812. // bit: 15-12; mask: 0x0000F000; bottom style
  1813. if ($bordersBottomStyle = self::_mapBorderStyle((0x0000F000 & self::_GetInt4d($recordData, 10)) >> 12)) {
  1814. $objStyle->getBorders()->getBottom()->setBorderStyle($bordersBottomStyle);
  1815. }
  1816. // bit: 22-16; mask: 0x007F0000; left color
  1817. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & self::_GetInt4d($recordData, 10)) >> 16;
  1818. // bit: 29-23; mask: 0x3F800000; right color
  1819. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & self::_GetInt4d($recordData, 10)) >> 23;
  1820. // bit: 30; mask: 0x40000000; 1 = diagonal line from top left to right bottom
  1821. $diagonalDown = (0x40000000 & self::_GetInt4d($recordData, 10)) >> 30 ?
  1822. true : false;
  1823. // bit: 31; mask: 0x80000000; 1 = diagonal line from bottom left to top right
  1824. $diagonalUp = (0x80000000 & self::_GetInt4d($recordData, 10)) >> 31 ?
  1825. true : false;
  1826. if ($diagonalUp == false && $diagonalDown == false) {
  1827. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_NONE);
  1828. } elseif ($diagonalUp == true && $diagonalDown == false) {
  1829. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_UP);
  1830. } elseif ($diagonalUp == false && $diagonalDown == true) {
  1831. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_DOWN);
  1832. } elseif ($diagonalUp == true && $diagonalDown == true) {
  1833. $objStyle->getBorders()->setDiagonalDirection(PHPExcel_Style_Borders::DIAGONAL_BOTH);
  1834. }
  1835. // offset: 14; size: 4;
  1836. // bit: 6-0; mask: 0x0000007F; top color
  1837. $objStyle->getBorders()->getTop()->colorIndex = (0x0000007F & self::_GetInt4d($recordData, 14)) >> 0;
  1838. // bit: 13-7; mask: 0x00003F80; bottom color
  1839. $objStyle->getBorders()->getBottom()->colorIndex = (0x00003F80 & self::_GetInt4d($recordData, 14)) >> 7;
  1840. // bit: 20-14; mask: 0x001FC000; diagonal color
  1841. $objStyle->getBorders()->getDiagonal()->colorIndex = (0x001FC000 & self::_GetInt4d($recordData, 14)) >> 14;
  1842. // bit: 24-21; mask: 0x01E00000; diagonal style
  1843. if ($bordersDiagonalStyle = self::_mapBorderStyle((0x01E00000 & self::_GetInt4d($recordData, 14)) >> 21)) {
  1844. $objStyle->getBorders()->getDiagonal()->setBorderStyle($bordersDiagonalStyle);
  1845. }
  1846. // bit: 31-26; mask: 0xFC000000 fill pattern
  1847. if ($fillType = self::_mapFillPattern((0xFC000000 & self::_GetInt4d($recordData, 14)) >> 26)) {
  1848. $objStyle->getFill()->setFillType($fillType);
  1849. }
  1850. // offset: 18; size: 2; pattern and background colour
  1851. // bit: 6-0; mask: 0x007F; color index for pattern color
  1852. $objStyle->getFill()->startcolorIndex = (0x007F & self::_GetInt2d($recordData, 18)) >> 0;
  1853. // bit: 13-7; mask: 0x3F80; color index for pattern background
  1854. $objStyle->getFill()->endcolorIndex = (0x3F80 & self::_GetInt2d($recordData, 18)) >> 7;
  1855. } else {
  1856. // BIFF5
  1857. // offset: 7; size: 1; Text orientation and flags
  1858. $orientationAndFlags = ord($recordData{7});
  1859. // bit: 1-0; mask: 0x03; XF_ORIENTATION: Text orientation
  1860. $xfOrientation = (0x03 & $orientationAndFlags) >> 0;
  1861. switch ($xfOrientation) {
  1862. case 0:
  1863. $objStyle->getAlignment()->setTextRotation(0);
  1864. break;
  1865. case 1:
  1866. $objStyle->getAlignment()->setTextRotation(-165);
  1867. break;
  1868. case 2:
  1869. $objStyle->getAlignment()->setTextRotation(90);
  1870. break;
  1871. case 3:
  1872. $objStyle->getAlignment()->setTextRotation(-90);
  1873. break;
  1874. }
  1875. // offset: 8; size: 4; cell border lines and background area
  1876. $borderAndBackground = self::_GetInt4d($recordData, 8);
  1877. // bit: 6-0; mask: 0x0000007F; color index for pattern color
  1878. $objStyle->getFill()->startcolorIndex = (0x0000007F & $borderAndBackground) >> 0;
  1879. // bit: 13-7; mask: 0x00003F80; color index for pattern background
  1880. $objStyle->getFill()->endcolorIndex = (0x00003F80 & $borderAndBackground) >> 7;
  1881. // bit: 21-16; mask: 0x003F0000; fill pattern
  1882. $objStyle->getFill()->setFillType(self::_mapFillPattern((0x003F0000 & $borderAndBackground) >> 16));
  1883. // bit: 24-22; mask: 0x01C00000; bottom line style
  1884. $objStyle->getBorders()->getBottom()->setBorderStyle(self::_mapBorderStyle((0x01C00000 & $borderAndBackground) >> 22));
  1885. // bit: 31-25; mask: 0xFE000000; bottom line color
  1886. $objStyle->getBorders()->getBottom()->colorIndex = (0xFE000000 & $borderAndBackground) >> 25;
  1887. // offset: 12; size: 4; cell border lines
  1888. $borderLines = self::_GetInt4d($recordData, 12);
  1889. // bit: 2-0; mask: 0x00000007; top line style
  1890. $objStyle->getBorders()->getTop()->setBorderStyle(self::_mapBorderStyle((0x00000007 & $borderLines) >> 0));
  1891. // bit: 5-3; mask: 0x00000038; left line style
  1892. $objStyle->getBorders()->getLeft()->setBorderStyle(self::_mapBorderStyle((0x00000038 & $borderLines) >> 3));
  1893. // bit: 8-6; mask: 0x000001C0; right line style
  1894. $objStyle->getBorders()->getRight()->setBorderStyle(self::_mapBorderStyle((0x000001C0 & $borderLines) >> 6));
  1895. // bit: 15-9; mask: 0x0000FE00; top line color index
  1896. $objStyle->getBorders()->getTop()->colorIndex = (0x0000FE00 & $borderLines) >> 9;
  1897. // bit: 22-16; mask: 0x007F0000; left line color index
  1898. $objStyle->getBorders()->getLeft()->colorIndex = (0x007F0000 & $borderLines) >> 16;
  1899. // bit: 29-23; mask: 0x3F800000; right line color index
  1900. $objStyle->getBorders()->getRight()->colorIndex = (0x3F800000 & $borderLines) >> 23;
  1901. }
  1902. // add cellStyleXf or cellXf and update mapping
  1903. if ($isCellStyleXf) {
  1904. // we only read one style XF record which is always the first
  1905. if ($this->_xfIndex == 0) {
  1906. $this->_phpExcel->addCellStyleXf($objStyle);
  1907. $this->_mapCellStyleXfIndex[$this->_xfIndex] = 0;
  1908. }
  1909. } else {
  1910. // we read all cell XF records
  1911. $this->_phpExcel->addCellXf($objStyle);
  1912. $this->_mapCellXfIndex[$this->_xfIndex] = count($this->_phpExcel->getCellXfCollection()) - 1;
  1913. }
  1914. // update XF index for when we read next record
  1915. ++$this->_xfIndex;
  1916. }
  1917. }
  1918. /**
  1919. *
  1920. */
  1921. private function _readXfExt()
  1922. {
  1923. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1924. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  1925. // move stream pointer to next record
  1926. $this->_pos += 4 + $length;
  1927. if (!$this->_readDataOnly) {
  1928. // offset: 0; size: 2; 0x087D = repeated header
  1929. // offset: 2; size: 2
  1930. // offset: 4; size: 8; not used
  1931. // offset: 12; size: 2; record version
  1932. // offset: 14; size: 2; index to XF record which this record modifies
  1933. $ixfe = self::_GetInt2d($recordData, 14);
  1934. // offset: 16; size: 2; not used
  1935. // offset: 18; size: 2; number of extension properties that follow
  1936. $cexts = self::_GetInt2d($recordData, 18);
  1937. // start reading the actual extension data
  1938. $offset = 20;
  1939. while ($offset < $length) {
  1940. // extension type
  1941. $extType = self::_GetInt2d($recordData, $offset);
  1942. // extension length
  1943. $cb = self::_GetInt2d($recordData, $offset + 2);
  1944. // extension data
  1945. $extData = substr($recordData, $offset + 4, $cb);
  1946. switch ($extType) {
  1947. case 4: // fill start color
  1948. $xclfType = self::_GetInt2d($extData, 0); // color type
  1949. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1950. if ($xclfType == 2) {
  1951. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1952. // modify the relevant style property
  1953. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1954. $fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  1955. $fill->getStartColor()->setRGB($rgb);
  1956. unset($fill->startcolorIndex); // normal color index does not apply, discard
  1957. }
  1958. }
  1959. break;
  1960. case 5: // fill end color
  1961. $xclfType = self::_GetInt2d($extData, 0); // color type
  1962. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1963. if ($xclfType == 2) {
  1964. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1965. // modify the relevant style property
  1966. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1967. $fill = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFill();
  1968. $fill->getEndColor()->setRGB($rgb);
  1969. unset($fill->endcolorIndex); // normal color index does not apply, discard
  1970. }
  1971. }
  1972. break;
  1973. case 7: // border color top
  1974. $xclfType = self::_GetInt2d($extData, 0); // color type
  1975. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1976. if ($xclfType == 2) {
  1977. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1978. // modify the relevant style property
  1979. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1980. $top = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getTop();
  1981. $top->getColor()->setRGB($rgb);
  1982. unset($top->colorIndex); // normal color index does not apply, discard
  1983. }
  1984. }
  1985. break;
  1986. case 8: // border color bottom
  1987. $xclfType = self::_GetInt2d($extData, 0); // color type
  1988. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  1989. if ($xclfType == 2) {
  1990. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  1991. // modify the relevant style property
  1992. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  1993. $bottom = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getBottom();
  1994. $bottom->getColor()->setRGB($rgb);
  1995. unset($bottom->colorIndex); // normal color index does not apply, discard
  1996. }
  1997. }
  1998. break;
  1999. case 9: // border color left
  2000. $xclfType = self::_GetInt2d($extData, 0); // color type
  2001. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2002. if ($xclfType == 2) {
  2003. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2004. // modify the relevant style property
  2005. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  2006. $left = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getLeft();
  2007. $left->getColor()->setRGB($rgb);
  2008. unset($left->colorIndex); // normal color index does not apply, discard
  2009. }
  2010. }
  2011. break;
  2012. case 10: // border color right
  2013. $xclfType = self::_GetInt2d($extData, 0); // color type
  2014. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2015. if ($xclfType == 2) {
  2016. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2017. // modify the relevant style property
  2018. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  2019. $right = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getRight();
  2020. $right->getColor()->setRGB($rgb);
  2021. unset($right->colorIndex); // normal color index does not apply, discard
  2022. }
  2023. }
  2024. break;
  2025. case 11: // border color diagonal
  2026. $xclfType = self::_GetInt2d($extData, 0); // color type
  2027. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2028. if ($xclfType == 2) {
  2029. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2030. // modify the relevant style property
  2031. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  2032. $diagonal = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getBorders()->getDiagonal();
  2033. $diagonal->getColor()->setRGB($rgb);
  2034. unset($diagonal->colorIndex); // normal color index does not apply, discard
  2035. }
  2036. }
  2037. break;
  2038. case 13: // font color
  2039. $xclfType = self::_GetInt2d($extData, 0); // color type
  2040. $xclrValue = substr($extData, 4, 4); // color value (value based on color type)
  2041. if ($xclfType == 2) {
  2042. $rgb = sprintf('%02X%02X%02X', ord($xclrValue{0}), ord($xclrValue{1}), ord($xclrValue{2}));
  2043. // modify the relevant style property
  2044. if ( isset($this->_mapCellXfIndex[$ixfe]) ) {
  2045. $font = $this->_phpExcel->getCellXfByIndex($this->_mapCellXfIndex[$ixfe])->getFont();
  2046. $font->getColor()->setRGB($rgb);
  2047. unset($font->colorIndex); // normal color index does not apply, discard
  2048. }
  2049. }
  2050. break;
  2051. }
  2052. $offset += $cb;
  2053. }
  2054. }
  2055. }
  2056. /**
  2057. * Read STYLE record
  2058. */
  2059. private function _readStyle()
  2060. {
  2061. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2062. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2063. // move stream pointer to next record
  2064. $this->_pos += 4 + $length;
  2065. if (!$this->_readDataOnly) {
  2066. // offset: 0; size: 2; index to XF record and flag for built-in style
  2067. $ixfe = self::_GetInt2d($recordData, 0);
  2068. // bit: 11-0; mask 0x0FFF; index to XF record
  2069. $xfIndex = (0x0FFF & $ixfe) >> 0;
  2070. // bit: 15; mask 0x8000; 0 = user-defined style, 1 = built-in style
  2071. $isBuiltIn = (bool) ((0x8000 & $ixfe) >> 15);
  2072. if ($isBuiltIn) {
  2073. // offset: 2; size: 1; identifier for built-in style
  2074. $builtInId = ord($recordData{2});
  2075. switch ($builtInId) {
  2076. case 0x00:
  2077. // currently, we are not using this for anything
  2078. break;
  2079. default:
  2080. break;
  2081. }
  2082. } else {
  2083. // user-defined; not supported by PHPExcel
  2084. }
  2085. }
  2086. }
  2087. /**
  2088. * Read PALETTE record
  2089. */
  2090. private function _readPalette()
  2091. {
  2092. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2093. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2094. // move stream pointer to next record
  2095. $this->_pos += 4 + $length;
  2096. if (!$this->_readDataOnly) {
  2097. // offset: 0; size: 2; number of following colors
  2098. $nm = self::_GetInt2d($recordData, 0);
  2099. // list of RGB colors
  2100. for ($i = 0; $i < $nm; ++$i) {
  2101. $rgb = substr($recordData, 2 + 4 * $i, 4);
  2102. $this->_palette[] = self::_readRGB($rgb);
  2103. }
  2104. }
  2105. }
  2106. /**
  2107. * SHEET
  2108. *
  2109. * This record is located in the Workbook Globals
  2110. * Substream and represents a sheet inside the workbook.
  2111. * One SHEET record is written for each sheet. It stores the
  2112. * sheet name and a stream offset to the BOF record of the
  2113. * respective Sheet Substream within the Workbook Stream.
  2114. *
  2115. * -- "OpenOffice.org's Documentation of the Microsoft
  2116. * Excel File Format"
  2117. */
  2118. private function _readSheet()
  2119. {
  2120. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2121. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2122. // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
  2123. // NOTE: not encrypted
  2124. $rec_offset = self::_GetInt4d($this->_data, $this->_pos + 4);
  2125. // move stream pointer to next record
  2126. $this->_pos += 4 + $length;
  2127. // offset: 4; size: 1; sheet state
  2128. switch (ord($recordData{4})) {
  2129. case 0x00: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE; break;
  2130. case 0x01: $sheetState = PHPExcel_Worksheet::SHEETSTATE_HIDDEN; break;
  2131. case 0x02: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VERYHIDDEN; break;
  2132. default: $sheetState = PHPExcel_Worksheet::SHEETSTATE_VISIBLE; break;
  2133. }
  2134. // offset: 5; size: 1; sheet type
  2135. $sheetType = ord($recordData{5});
  2136. // offset: 6; size: var; sheet name
  2137. if ($this->_version == self::XLS_BIFF8) {
  2138. $string = self::_readUnicodeStringShort(substr($recordData, 6));
  2139. $rec_name = $string['value'];
  2140. } elseif ($this->_version == self::XLS_BIFF7) {
  2141. $string = $this->_readByteStringShort(substr($recordData, 6));
  2142. $rec_name = $string['value'];
  2143. }
  2144. $this->_sheets[] = array(
  2145. 'name' => $rec_name,
  2146. 'offset' => $rec_offset,
  2147. 'sheetState' => $sheetState,
  2148. 'sheetType' => $sheetType,
  2149. );
  2150. }
  2151. /**
  2152. * Read EXTERNALBOOK record
  2153. */
  2154. private function _readExternalBook()
  2155. {
  2156. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2157. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2158. // move stream pointer to next record
  2159. $this->_pos += 4 + $length;
  2160. // offset within record data
  2161. $offset = 0;
  2162. // there are 4 types of records
  2163. if (strlen($recordData) > 4) {
  2164. // external reference
  2165. // offset: 0; size: 2; number of sheet names ($nm)
  2166. $nm = self::_GetInt2d($recordData, 0);
  2167. $offset += 2;
  2168. // offset: 2; size: var; encoded URL without sheet name (Unicode string, 16-bit length)
  2169. $encodedUrlString = self::_readUnicodeStringLong(substr($recordData, 2));
  2170. $offset += $encodedUrlString['size'];
  2171. // offset: var; size: var; list of $nm sheet names (Unicode strings, 16-bit length)
  2172. $externalSheetNames = array();
  2173. for ($i = 0; $i < $nm; ++$i) {
  2174. $externalSheetNameString = self::_readUnicodeStringLong(substr($recordData, $offset));
  2175. $externalSheetNames[] = $externalSheetNameString['value'];
  2176. $offset += $externalSheetNameString['size'];
  2177. }
  2178. // store the record data
  2179. $this->_externalBooks[] = array(
  2180. 'type' => 'external',
  2181. 'encodedUrl' => $encodedUrlString['value'],
  2182. 'externalSheetNames' => $externalSheetNames,
  2183. );
  2184. } elseif (substr($recordData, 2, 2) == pack('CC', 0x01, 0x04)) {
  2185. // internal reference
  2186. // offset: 0; size: 2; number of sheet in this document
  2187. // offset: 2; size: 2; 0x01 0x04
  2188. $this->_externalBooks[] = array(
  2189. 'type' => 'internal',
  2190. );
  2191. } elseif (substr($recordData, 0, 4) == pack('vCC', 0x0001, 0x01, 0x3A)) {
  2192. // add-in function
  2193. // offset: 0; size: 2; 0x0001
  2194. $this->_externalBooks[] = array(
  2195. 'type' => 'addInFunction',
  2196. );
  2197. } elseif (substr($recordData, 0, 2) == pack('v', 0x0000)) {
  2198. // DDE links, OLE links
  2199. // offset: 0; size: 2; 0x0000
  2200. // offset: 2; size: var; encoded source document name
  2201. $this->_externalBooks[] = array(
  2202. 'type' => 'DDEorOLE',
  2203. );
  2204. }
  2205. }
  2206. /**
  2207. * Read EXTERNNAME record.
  2208. */
  2209. private function _readExternName()
  2210. {
  2211. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2212. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2213. // move stream pointer to next record
  2214. $this->_pos += 4 + $length;
  2215. // external sheet references provided for named cells
  2216. if ($this->_version == self::XLS_BIFF8) {
  2217. // offset: 0; size: 2; options
  2218. $options = self::_GetInt2d($recordData, 0);
  2219. // offset: 2; size: 2;
  2220. // offset: 4; size: 2; not used
  2221. // offset: 6; size: var
  2222. $nameString = self::_readUnicodeStringShort(substr($recordData, 6));
  2223. // offset: var; size: var; formula data
  2224. $offset = 6 + $nameString['size'];
  2225. $formula = $this->_getFormulaFromStructure(substr($recordData, $offset));
  2226. $this->_externalNames[] = array(
  2227. 'name' => $nameString['value'],
  2228. 'formula' => $formula,
  2229. );
  2230. }
  2231. }
  2232. /**
  2233. * Read EXTERNSHEET record
  2234. */
  2235. private function _readExternSheet()
  2236. {
  2237. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2238. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2239. // move stream pointer to next record
  2240. $this->_pos += 4 + $length;
  2241. // external sheet references provided for named cells
  2242. if ($this->_version == self::XLS_BIFF8) {
  2243. // offset: 0; size: 2; number of following ref structures
  2244. $nm = self::_GetInt2d($recordData, 0);
  2245. for ($i = 0; $i < $nm; ++$i) {
  2246. $this->_ref[] = array(
  2247. // offset: 2 + 6 * $i; index to EXTERNALBOOK record
  2248. 'externalBookIndex' => self::_GetInt2d($recordData, 2 + 6 * $i),
  2249. // offset: 4 + 6 * $i; index to first sheet in EXTERNALBOOK record
  2250. 'firstSheetIndex' => self::_GetInt2d($recordData, 4 + 6 * $i),
  2251. // offset: 6 + 6 * $i; index to last sheet in EXTERNALBOOK record
  2252. 'lastSheetIndex' => self::_GetInt2d($recordData, 6 + 6 * $i),
  2253. );
  2254. }
  2255. }
  2256. }
  2257. /**
  2258. * DEFINEDNAME
  2259. *
  2260. * This record is part of a Link Table. It contains the name
  2261. * and the token array of an internal defined name. Token
  2262. * arrays of defined names contain tokens with aberrant
  2263. * token classes.
  2264. *
  2265. * -- "OpenOffice.org's Documentation of the Microsoft
  2266. * Excel File Format"
  2267. */
  2268. private function _readDefinedName()
  2269. {
  2270. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2271. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2272. // move stream pointer to next record
  2273. $this->_pos += 4 + $length;
  2274. if ($this->_version == self::XLS_BIFF8) {
  2275. // retrieves named cells
  2276. // offset: 0; size: 2; option flags
  2277. $opts = self::_GetInt2d($recordData, 0);
  2278. // bit: 5; mask: 0x0020; 0 = user-defined name, 1 = built-in-name
  2279. $isBuiltInName = (0x0020 & $opts) >> 5;
  2280. // offset: 2; size: 1; keyboard shortcut
  2281. // offset: 3; size: 1; length of the name (character count)
  2282. $nlen = ord($recordData{3});
  2283. // offset: 4; size: 2; size of the formula data (it can happen that this is zero)
  2284. // note: there can also be additional data, this is not included in $flen
  2285. $flen = self::_GetInt2d($recordData, 4);
  2286. // offset: 8; size: 2; 0=Global name, otherwise index to sheet (1-based)
  2287. $scope = self::_GetInt2d($recordData, 8);
  2288. // offset: 14; size: var; Name (Unicode string without length field)
  2289. $string = self::_readUnicodeString(substr($recordData, 14), $nlen);
  2290. // offset: var; size: $flen; formula data
  2291. $offset = 14 + $string['size'];
  2292. $formulaStructure = pack('v', $flen) . substr($recordData, $offset);
  2293. try {
  2294. $formula = $this->_getFormulaFromStructure($formulaStructure);
  2295. } catch (PHPExcel_Exception $e) {
  2296. $formula = '';
  2297. }
  2298. $this->_definedname[] = array(
  2299. 'isBuiltInName' => $isBuiltInName,
  2300. 'name' => $string['value'],
  2301. 'formula' => $formula,
  2302. 'scope' => $scope,
  2303. );
  2304. }
  2305. }
  2306. /**
  2307. * Read MSODRAWINGGROUP record
  2308. */
  2309. private function _readMsoDrawingGroup()
  2310. {
  2311. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2312. // get spliced record data
  2313. $splicedRecordData = $this->_getSplicedRecordData();
  2314. $recordData = $splicedRecordData['recordData'];
  2315. $this->_drawingGroupData .= $recordData;
  2316. }
  2317. /**
  2318. * SST - Shared String Table
  2319. *
  2320. * This record contains a list of all strings used anywhere
  2321. * in the workbook. Each string occurs only once. The
  2322. * workbook uses indexes into the list to reference the
  2323. * strings.
  2324. *
  2325. * -- "OpenOffice.org's Documentation of the Microsoft
  2326. * Excel File Format"
  2327. **/
  2328. private function _readSst()
  2329. {
  2330. // offset within (spliced) record data
  2331. $pos = 0;
  2332. // get spliced record data
  2333. $splicedRecordData = $this->_getSplicedRecordData();
  2334. $recordData = $splicedRecordData['recordData'];
  2335. $spliceOffsets = $splicedRecordData['spliceOffsets'];
  2336. // offset: 0; size: 4; total number of strings in the workbook
  2337. $pos += 4;
  2338. // offset: 4; size: 4; number of following strings ($nm)
  2339. $nm = self::_GetInt4d($recordData, 4);
  2340. $pos += 4;
  2341. // loop through the Unicode strings (16-bit length)
  2342. for ($i = 0; $i < $nm; ++$i) {
  2343. // number of characters in the Unicode string
  2344. $numChars = self::_GetInt2d($recordData, $pos);
  2345. $pos += 2;
  2346. // option flags
  2347. $optionFlags = ord($recordData{$pos});
  2348. ++$pos;
  2349. // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  2350. $isCompressed = (($optionFlags & 0x01) == 0) ;
  2351. // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  2352. $hasAsian = (($optionFlags & 0x04) != 0);
  2353. // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  2354. $hasRichText = (($optionFlags & 0x08) != 0);
  2355. if ($hasRichText) {
  2356. // number of Rich-Text formatting runs
  2357. $formattingRuns = self::_GetInt2d($recordData, $pos);
  2358. $pos += 2;
  2359. }
  2360. if ($hasAsian) {
  2361. // size of Asian phonetic setting
  2362. $extendedRunLength = self::_GetInt4d($recordData, $pos);
  2363. $pos += 4;
  2364. }
  2365. // expected byte length of character array if not split
  2366. $len = ($isCompressed) ? $numChars : $numChars * 2;
  2367. // look up limit position
  2368. foreach ($spliceOffsets as $spliceOffset) {
  2369. // it can happen that the string is empty, therefore we need
  2370. // <= and not just <
  2371. if ($pos <= $spliceOffset) {
  2372. $limitpos = $spliceOffset;
  2373. break;
  2374. }
  2375. }
  2376. if ($pos + $len <= $limitpos) {
  2377. // character array is not split between records
  2378. $retstr = substr($recordData, $pos, $len);
  2379. $pos += $len;
  2380. } else {
  2381. // character array is split between records
  2382. // first part of character array
  2383. $retstr = substr($recordData, $pos, $limitpos - $pos);
  2384. $bytesRead = $limitpos - $pos;
  2385. // remaining characters in Unicode string
  2386. $charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2));
  2387. $pos = $limitpos;
  2388. // keep reading the characters
  2389. while ($charsLeft > 0) {
  2390. // look up next limit position, in case the string span more than one continue record
  2391. foreach ($spliceOffsets as $spliceOffset) {
  2392. if ($pos < $spliceOffset) {
  2393. $limitpos = $spliceOffset;
  2394. break;
  2395. }
  2396. }
  2397. // repeated option flags
  2398. // OpenOffice.org documentation 5.21
  2399. $option = ord($recordData{$pos});
  2400. ++$pos;
  2401. if ($isCompressed && ($option == 0)) {
  2402. // 1st fragment compressed
  2403. // this fragment compressed
  2404. $len = min($charsLeft, $limitpos - $pos);
  2405. $retstr .= substr($recordData, $pos, $len);
  2406. $charsLeft -= $len;
  2407. $isCompressed = true;
  2408. } elseif (!$isCompressed && ($option != 0)) {
  2409. // 1st fragment uncompressed
  2410. // this fragment uncompressed
  2411. $len = min($charsLeft * 2, $limitpos - $pos);
  2412. $retstr .= substr($recordData, $pos, $len);
  2413. $charsLeft -= $len / 2;
  2414. $isCompressed = false;
  2415. } elseif (!$isCompressed && ($option == 0)) {
  2416. // 1st fragment uncompressed
  2417. // this fragment compressed
  2418. $len = min($charsLeft, $limitpos - $pos);
  2419. for ($j = 0; $j < $len; ++$j) {
  2420. $retstr .= $recordData{$pos + $j} . chr(0);
  2421. }
  2422. $charsLeft -= $len;
  2423. $isCompressed = false;
  2424. } else {
  2425. // 1st fragment compressed
  2426. // this fragment uncompressed
  2427. $newstr = '';
  2428. for ($j = 0; $j < strlen($retstr); ++$j) {
  2429. $newstr .= $retstr[$j] . chr(0);
  2430. }
  2431. $retstr = $newstr;
  2432. $len = min($charsLeft * 2, $limitpos - $pos);
  2433. $retstr .= substr($recordData, $pos, $len);
  2434. $charsLeft -= $len / 2;
  2435. $isCompressed = false;
  2436. }
  2437. $pos += $len;
  2438. }
  2439. }
  2440. // convert to UTF-8
  2441. $retstr = self::_encodeUTF16($retstr, $isCompressed);
  2442. // read additional Rich-Text information, if any
  2443. $fmtRuns = array();
  2444. if ($hasRichText) {
  2445. // list of formatting runs
  2446. for ($j = 0; $j < $formattingRuns; ++$j) {
  2447. // first formatted character; zero-based
  2448. $charPos = self::_GetInt2d($recordData, $pos + $j * 4);
  2449. // index to font record
  2450. $fontIndex = self::_GetInt2d($recordData, $pos + 2 + $j * 4);
  2451. $fmtRuns[] = array(
  2452. 'charPos' => $charPos,
  2453. 'fontIndex' => $fontIndex,
  2454. );
  2455. }
  2456. $pos += 4 * $formattingRuns;
  2457. }
  2458. // read additional Asian phonetics information, if any
  2459. if ($hasAsian) {
  2460. // For Asian phonetic settings, we skip the extended string data
  2461. $pos += $extendedRunLength;
  2462. }
  2463. // store the shared sting
  2464. $this->_sst[] = array(
  2465. 'value' => $retstr,
  2466. 'fmtRuns' => $fmtRuns,
  2467. );
  2468. }
  2469. // _getSplicedRecordData() takes care of moving current position in data stream
  2470. }
  2471. /**
  2472. * Read PRINTGRIDLINES record
  2473. */
  2474. private function _readPrintGridlines()
  2475. {
  2476. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2477. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2478. // move stream pointer to next record
  2479. $this->_pos += 4 + $length;
  2480. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2481. // offset: 0; size: 2; 0 = do not print sheet grid lines; 1 = print sheet gridlines
  2482. $printGridlines = (bool) self::_GetInt2d($recordData, 0);
  2483. $this->_phpSheet->setPrintGridlines($printGridlines);
  2484. }
  2485. }
  2486. /**
  2487. * Read DEFAULTROWHEIGHT record
  2488. */
  2489. private function _readDefaultRowHeight()
  2490. {
  2491. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2492. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2493. // move stream pointer to next record
  2494. $this->_pos += 4 + $length;
  2495. // offset: 0; size: 2; option flags
  2496. // offset: 2; size: 2; default height for unused rows, (twips 1/20 point)
  2497. $height = self::_GetInt2d($recordData, 2);
  2498. $this->_phpSheet->getDefaultRowDimension()->setRowHeight($height / 20);
  2499. }
  2500. /**
  2501. * Read SHEETPR record
  2502. */
  2503. private function _readSheetPr()
  2504. {
  2505. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2506. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2507. // move stream pointer to next record
  2508. $this->_pos += 4 + $length;
  2509. // offset: 0; size: 2
  2510. // bit: 6; mask: 0x0040; 0 = outline buttons above outline group
  2511. $isSummaryBelow = (0x0040 & self::_GetInt2d($recordData, 0)) >> 6;
  2512. $this->_phpSheet->setShowSummaryBelow($isSummaryBelow);
  2513. // bit: 7; mask: 0x0080; 0 = outline buttons left of outline group
  2514. $isSummaryRight = (0x0080 & self::_GetInt2d($recordData, 0)) >> 7;
  2515. $this->_phpSheet->setShowSummaryRight($isSummaryRight);
  2516. // bit: 8; mask: 0x100; 0 = scale printout in percent, 1 = fit printout to number of pages
  2517. // this corresponds to radio button setting in page setup dialog in Excel
  2518. $this->_isFitToPages = (bool) ((0x0100 & self::_GetInt2d($recordData, 0)) >> 8);
  2519. }
  2520. /**
  2521. * Read HORIZONTALPAGEBREAKS record
  2522. */
  2523. private function _readHorizontalPageBreaks()
  2524. {
  2525. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2526. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2527. // move stream pointer to next record
  2528. $this->_pos += 4 + $length;
  2529. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2530. // offset: 0; size: 2; number of the following row index structures
  2531. $nm = self::_GetInt2d($recordData, 0);
  2532. // offset: 2; size: 6 * $nm; list of $nm row index structures
  2533. for ($i = 0; $i < $nm; ++$i) {
  2534. $r = self::_GetInt2d($recordData, 2 + 6 * $i);
  2535. $cf = self::_GetInt2d($recordData, 2 + 6 * $i + 2);
  2536. $cl = self::_GetInt2d($recordData, 2 + 6 * $i + 4);
  2537. // not sure why two column indexes are necessary?
  2538. $this->_phpSheet->setBreakByColumnAndRow($cf, $r, PHPExcel_Worksheet::BREAK_ROW);
  2539. }
  2540. }
  2541. }
  2542. /**
  2543. * Read VERTICALPAGEBREAKS record
  2544. */
  2545. private function _readVerticalPageBreaks()
  2546. {
  2547. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2548. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2549. // move stream pointer to next record
  2550. $this->_pos += 4 + $length;
  2551. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  2552. // offset: 0; size: 2; number of the following column index structures
  2553. $nm = self::_GetInt2d($recordData, 0);
  2554. // offset: 2; size: 6 * $nm; list of $nm row index structures
  2555. for ($i = 0; $i < $nm; ++$i) {
  2556. $c = self::_GetInt2d($recordData, 2 + 6 * $i);
  2557. $rf = self::_GetInt2d($recordData, 2 + 6 * $i + 2);
  2558. $rl = self::_GetInt2d($recordData, 2 + 6 * $i + 4);
  2559. // not sure why two row indexes are necessary?
  2560. $this->_phpSheet->setBreakByColumnAndRow($c, $rf, PHPExcel_Worksheet::BREAK_COLUMN);
  2561. }
  2562. }
  2563. }
  2564. /**
  2565. * Read HEADER record
  2566. */
  2567. private function _readHeader()
  2568. {
  2569. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2570. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2571. // move stream pointer to next record
  2572. $this->_pos += 4 + $length;
  2573. if (!$this->_readDataOnly) {
  2574. // offset: 0; size: var
  2575. // realized that $recordData can be empty even when record exists
  2576. if ($recordData) {
  2577. if ($this->_version == self::XLS_BIFF8) {
  2578. $string = self::_readUnicodeStringLong($recordData);
  2579. } else {
  2580. $string = $this->_readByteStringShort($recordData);
  2581. }
  2582. $this->_phpSheet->getHeaderFooter()->setOddHeader($string['value']);
  2583. $this->_phpSheet->getHeaderFooter()->setEvenHeader($string['value']);
  2584. }
  2585. }
  2586. }
  2587. /**
  2588. * Read FOOTER record
  2589. */
  2590. private function _readFooter()
  2591. {
  2592. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2593. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2594. // move stream pointer to next record
  2595. $this->_pos += 4 + $length;
  2596. if (!$this->_readDataOnly) {
  2597. // offset: 0; size: var
  2598. // realized that $recordData can be empty even when record exists
  2599. if ($recordData) {
  2600. if ($this->_version == self::XLS_BIFF8) {
  2601. $string = self::_readUnicodeStringLong($recordData);
  2602. } else {
  2603. $string = $this->_readByteStringShort($recordData);
  2604. }
  2605. $this->_phpSheet->getHeaderFooter()->setOddFooter($string['value']);
  2606. $this->_phpSheet->getHeaderFooter()->setEvenFooter($string['value']);
  2607. }
  2608. }
  2609. }
  2610. /**
  2611. * Read HCENTER record
  2612. */
  2613. private function _readHcenter()
  2614. {
  2615. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2616. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2617. // move stream pointer to next record
  2618. $this->_pos += 4 + $length;
  2619. if (!$this->_readDataOnly) {
  2620. // offset: 0; size: 2; 0 = print sheet left aligned, 1 = print sheet centered horizontally
  2621. $isHorizontalCentered = (bool) self::_GetInt2d($recordData, 0);
  2622. $this->_phpSheet->getPageSetup()->setHorizontalCentered($isHorizontalCentered);
  2623. }
  2624. }
  2625. /**
  2626. * Read VCENTER record
  2627. */
  2628. private function _readVcenter()
  2629. {
  2630. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2631. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2632. // move stream pointer to next record
  2633. $this->_pos += 4 + $length;
  2634. if (!$this->_readDataOnly) {
  2635. // offset: 0; size: 2; 0 = print sheet aligned at top page border, 1 = print sheet vertically centered
  2636. $isVerticalCentered = (bool) self::_GetInt2d($recordData, 0);
  2637. $this->_phpSheet->getPageSetup()->setVerticalCentered($isVerticalCentered);
  2638. }
  2639. }
  2640. /**
  2641. * Read LEFTMARGIN record
  2642. */
  2643. private function _readLeftMargin()
  2644. {
  2645. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2646. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2647. // move stream pointer to next record
  2648. $this->_pos += 4 + $length;
  2649. if (!$this->_readDataOnly) {
  2650. // offset: 0; size: 8
  2651. $this->_phpSheet->getPageMargins()->setLeft(self::_extractNumber($recordData));
  2652. }
  2653. }
  2654. /**
  2655. * Read RIGHTMARGIN record
  2656. */
  2657. private function _readRightMargin()
  2658. {
  2659. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2660. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2661. // move stream pointer to next record
  2662. $this->_pos += 4 + $length;
  2663. if (!$this->_readDataOnly) {
  2664. // offset: 0; size: 8
  2665. $this->_phpSheet->getPageMargins()->setRight(self::_extractNumber($recordData));
  2666. }
  2667. }
  2668. /**
  2669. * Read TOPMARGIN record
  2670. */
  2671. private function _readTopMargin()
  2672. {
  2673. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2674. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2675. // move stream pointer to next record
  2676. $this->_pos += 4 + $length;
  2677. if (!$this->_readDataOnly) {
  2678. // offset: 0; size: 8
  2679. $this->_phpSheet->getPageMargins()->setTop(self::_extractNumber($recordData));
  2680. }
  2681. }
  2682. /**
  2683. * Read BOTTOMMARGIN record
  2684. */
  2685. private function _readBottomMargin()
  2686. {
  2687. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2688. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2689. // move stream pointer to next record
  2690. $this->_pos += 4 + $length;
  2691. if (!$this->_readDataOnly) {
  2692. // offset: 0; size: 8
  2693. $this->_phpSheet->getPageMargins()->setBottom(self::_extractNumber($recordData));
  2694. }
  2695. }
  2696. /**
  2697. * Read PAGESETUP record
  2698. */
  2699. private function _readPageSetup()
  2700. {
  2701. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2702. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2703. // move stream pointer to next record
  2704. $this->_pos += 4 + $length;
  2705. if (!$this->_readDataOnly) {
  2706. // offset: 0; size: 2; paper size
  2707. $paperSize = self::_GetInt2d($recordData, 0);
  2708. // offset: 2; size: 2; scaling factor
  2709. $scale = self::_GetInt2d($recordData, 2);
  2710. // offset: 6; size: 2; fit worksheet width to this number of pages, 0 = use as many as needed
  2711. $fitToWidth = self::_GetInt2d($recordData, 6);
  2712. // offset: 8; size: 2; fit worksheet height to this number of pages, 0 = use as many as needed
  2713. $fitToHeight = self::_GetInt2d($recordData, 8);
  2714. // offset: 10; size: 2; option flags
  2715. // bit: 1; mask: 0x0002; 0=landscape, 1=portrait
  2716. $isPortrait = (0x0002 & self::_GetInt2d($recordData, 10)) >> 1;
  2717. // bit: 2; mask: 0x0004; 1= paper size, scaling factor, paper orient. not init
  2718. // when this bit is set, do not use flags for those properties
  2719. $isNotInit = (0x0004 & self::_GetInt2d($recordData, 10)) >> 2;
  2720. if (!$isNotInit) {
  2721. $this->_phpSheet->getPageSetup()->setPaperSize($paperSize);
  2722. switch ($isPortrait) {
  2723. case 0: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); break;
  2724. case 1: $this->_phpSheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); break;
  2725. }
  2726. $this->_phpSheet->getPageSetup()->setScale($scale, false);
  2727. $this->_phpSheet->getPageSetup()->setFitToPage((bool) $this->_isFitToPages);
  2728. $this->_phpSheet->getPageSetup()->setFitToWidth($fitToWidth, false);
  2729. $this->_phpSheet->getPageSetup()->setFitToHeight($fitToHeight, false);
  2730. }
  2731. // offset: 16; size: 8; header margin (IEEE 754 floating-point value)
  2732. $marginHeader = self::_extractNumber(substr($recordData, 16, 8));
  2733. $this->_phpSheet->getPageMargins()->setHeader($marginHeader);
  2734. // offset: 24; size: 8; footer margin (IEEE 754 floating-point value)
  2735. $marginFooter = self::_extractNumber(substr($recordData, 24, 8));
  2736. $this->_phpSheet->getPageMargins()->setFooter($marginFooter);
  2737. }
  2738. }
  2739. /**
  2740. * PROTECT - Sheet protection (BIFF2 through BIFF8)
  2741. * if this record is omitted, then it also means no sheet protection
  2742. */
  2743. private function _readProtect()
  2744. {
  2745. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2746. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2747. // move stream pointer to next record
  2748. $this->_pos += 4 + $length;
  2749. if ($this->_readDataOnly) {
  2750. return;
  2751. }
  2752. // offset: 0; size: 2;
  2753. // bit 0, mask 0x01; 1 = sheet is protected
  2754. $bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
  2755. $this->_phpSheet->getProtection()->setSheet((bool)$bool);
  2756. }
  2757. /**
  2758. * SCENPROTECT
  2759. */
  2760. private function _readScenProtect()
  2761. {
  2762. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2763. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2764. // move stream pointer to next record
  2765. $this->_pos += 4 + $length;
  2766. if ($this->_readDataOnly) {
  2767. return;
  2768. }
  2769. // offset: 0; size: 2;
  2770. // bit: 0, mask 0x01; 1 = scenarios are protected
  2771. $bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
  2772. $this->_phpSheet->getProtection()->setScenarios((bool)$bool);
  2773. }
  2774. /**
  2775. * OBJECTPROTECT
  2776. */
  2777. private function _readObjectProtect()
  2778. {
  2779. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2780. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2781. // move stream pointer to next record
  2782. $this->_pos += 4 + $length;
  2783. if ($this->_readDataOnly) {
  2784. return;
  2785. }
  2786. // offset: 0; size: 2;
  2787. // bit: 0, mask 0x01; 1 = objects are protected
  2788. $bool = (0x01 & self::_GetInt2d($recordData, 0)) >> 0;
  2789. $this->_phpSheet->getProtection()->setObjects((bool)$bool);
  2790. }
  2791. /**
  2792. * PASSWORD - Sheet protection (hashed) password (BIFF2 through BIFF8)
  2793. */
  2794. private function _readPassword()
  2795. {
  2796. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2797. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2798. // move stream pointer to next record
  2799. $this->_pos += 4 + $length;
  2800. if (!$this->_readDataOnly) {
  2801. // offset: 0; size: 2; 16-bit hash value of password
  2802. $password = strtoupper(dechex(self::_GetInt2d($recordData, 0))); // the hashed password
  2803. $this->_phpSheet->getProtection()->setPassword($password, true);
  2804. }
  2805. }
  2806. /**
  2807. * Read DEFCOLWIDTH record
  2808. */
  2809. private function _readDefColWidth()
  2810. {
  2811. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2812. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2813. // move stream pointer to next record
  2814. $this->_pos += 4 + $length;
  2815. // offset: 0; size: 2; default column width
  2816. $width = self::_GetInt2d($recordData, 0);
  2817. if ($width != 8) {
  2818. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width);
  2819. }
  2820. }
  2821. /**
  2822. * Read COLINFO record
  2823. */
  2824. private function _readColInfo()
  2825. {
  2826. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2827. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2828. // move stream pointer to next record
  2829. $this->_pos += 4 + $length;
  2830. if (!$this->_readDataOnly) {
  2831. // offset: 0; size: 2; index to first column in range
  2832. $fc = self::_GetInt2d($recordData, 0); // first column index
  2833. // offset: 2; size: 2; index to last column in range
  2834. $lc = self::_GetInt2d($recordData, 2); // first column index
  2835. // offset: 4; size: 2; width of the column in 1/256 of the width of the zero character
  2836. $width = self::_GetInt2d($recordData, 4);
  2837. // offset: 6; size: 2; index to XF record for default column formatting
  2838. $xfIndex = self::_GetInt2d($recordData, 6);
  2839. // offset: 8; size: 2; option flags
  2840. // bit: 0; mask: 0x0001; 1= columns are hidden
  2841. $isHidden = (0x0001 & self::_GetInt2d($recordData, 8)) >> 0;
  2842. // bit: 10-8; mask: 0x0700; outline level of the columns (0 = no outline)
  2843. $level = (0x0700 & self::_GetInt2d($recordData, 8)) >> 8;
  2844. // bit: 12; mask: 0x1000; 1 = collapsed
  2845. $isCollapsed = (0x1000 & self::_GetInt2d($recordData, 8)) >> 12;
  2846. // offset: 10; size: 2; not used
  2847. for ($i = $fc; $i <= $lc; ++$i) {
  2848. if ($lc == 255 || $lc == 256) {
  2849. $this->_phpSheet->getDefaultColumnDimension()->setWidth($width / 256);
  2850. break;
  2851. }
  2852. $this->_phpSheet->getColumnDimensionByColumn($i)->setWidth($width / 256);
  2853. $this->_phpSheet->getColumnDimensionByColumn($i)->setVisible(!$isHidden);
  2854. $this->_phpSheet->getColumnDimensionByColumn($i)->setOutlineLevel($level);
  2855. $this->_phpSheet->getColumnDimensionByColumn($i)->setCollapsed($isCollapsed);
  2856. $this->_phpSheet->getColumnDimensionByColumn($i)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2857. }
  2858. }
  2859. }
  2860. /**
  2861. * ROW
  2862. *
  2863. * This record contains the properties of a single row in a
  2864. * sheet. Rows and cells in a sheet are divided into blocks
  2865. * of 32 rows.
  2866. *
  2867. * -- "OpenOffice.org's Documentation of the Microsoft
  2868. * Excel File Format"
  2869. */
  2870. private function _readRow()
  2871. {
  2872. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2873. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2874. // move stream pointer to next record
  2875. $this->_pos += 4 + $length;
  2876. if (!$this->_readDataOnly) {
  2877. // offset: 0; size: 2; index of this row
  2878. $r = self::_GetInt2d($recordData, 0);
  2879. // offset: 2; size: 2; index to column of the first cell which is described by a cell record
  2880. // offset: 4; size: 2; index to column of the last cell which is described by a cell record, increased by 1
  2881. // offset: 6; size: 2;
  2882. // bit: 14-0; mask: 0x7FFF; height of the row, in twips = 1/20 of a point
  2883. $height = (0x7FFF & self::_GetInt2d($recordData, 6)) >> 0;
  2884. // bit: 15: mask: 0x8000; 0 = row has custom height; 1= row has default height
  2885. $useDefaultHeight = (0x8000 & self::_GetInt2d($recordData, 6)) >> 15;
  2886. if (!$useDefaultHeight) {
  2887. $this->_phpSheet->getRowDimension($r + 1)->setRowHeight($height / 20);
  2888. }
  2889. // offset: 8; size: 2; not used
  2890. // offset: 10; size: 2; not used in BIFF5-BIFF8
  2891. // offset: 12; size: 4; option flags and default row formatting
  2892. // bit: 2-0: mask: 0x00000007; outline level of the row
  2893. $level = (0x00000007 & self::_GetInt4d($recordData, 12)) >> 0;
  2894. $this->_phpSheet->getRowDimension($r + 1)->setOutlineLevel($level);
  2895. // bit: 4; mask: 0x00000010; 1 = outline group start or ends here... and is collapsed
  2896. $isCollapsed = (0x00000010 & self::_GetInt4d($recordData, 12)) >> 4;
  2897. $this->_phpSheet->getRowDimension($r + 1)->setCollapsed($isCollapsed);
  2898. // bit: 5; mask: 0x00000020; 1 = row is hidden
  2899. $isHidden = (0x00000020 & self::_GetInt4d($recordData, 12)) >> 5;
  2900. $this->_phpSheet->getRowDimension($r + 1)->setVisible(!$isHidden);
  2901. // bit: 7; mask: 0x00000080; 1 = row has explicit format
  2902. $hasExplicitFormat = (0x00000080 & self::_GetInt4d($recordData, 12)) >> 7;
  2903. // bit: 27-16; mask: 0x0FFF0000; only applies when hasExplicitFormat = 1; index to XF record
  2904. $xfIndex = (0x0FFF0000 & self::_GetInt4d($recordData, 12)) >> 16;
  2905. if ($hasExplicitFormat) {
  2906. $this->_phpSheet->getRowDimension($r + 1)->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2907. }
  2908. }
  2909. }
  2910. /**
  2911. * Read RK record
  2912. * This record represents a cell that contains an RK value
  2913. * (encoded integer or floating-point value). If a
  2914. * floating-point value cannot be encoded to an RK value,
  2915. * a NUMBER record will be written. This record replaces the
  2916. * record INTEGER written in BIFF2.
  2917. *
  2918. * -- "OpenOffice.org's Documentation of the Microsoft
  2919. * Excel File Format"
  2920. */
  2921. private function _readRk()
  2922. {
  2923. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2924. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2925. // move stream pointer to next record
  2926. $this->_pos += 4 + $length;
  2927. // offset: 0; size: 2; index to row
  2928. $row = self::_GetInt2d($recordData, 0);
  2929. // offset: 2; size: 2; index to column
  2930. $column = self::_GetInt2d($recordData, 2);
  2931. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2932. // Read cell?
  2933. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2934. // offset: 4; size: 2; index to XF record
  2935. $xfIndex = self::_GetInt2d($recordData, 4);
  2936. // offset: 6; size: 4; RK value
  2937. $rknum = self::_GetInt4d($recordData, 6);
  2938. $numValue = self::_GetIEEE754($rknum);
  2939. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  2940. if (!$this->_readDataOnly) {
  2941. // add style information
  2942. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  2943. }
  2944. // add cell
  2945. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  2946. }
  2947. }
  2948. /**
  2949. * Read LABELSST record
  2950. * This record represents a cell that contains a string. It
  2951. * replaces the LABEL record and RSTRING record used in
  2952. * BIFF2-BIFF5.
  2953. *
  2954. * -- "OpenOffice.org's Documentation of the Microsoft
  2955. * Excel File Format"
  2956. */
  2957. private function _readLabelSst()
  2958. {
  2959. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  2960. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  2961. // move stream pointer to next record
  2962. $this->_pos += 4 + $length;
  2963. // offset: 0; size: 2; index to row
  2964. $row = self::_GetInt2d($recordData, 0);
  2965. // offset: 2; size: 2; index to column
  2966. $column = self::_GetInt2d($recordData, 2);
  2967. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  2968. // Read cell?
  2969. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  2970. // offset: 4; size: 2; index to XF record
  2971. $xfIndex = self::_GetInt2d($recordData, 4);
  2972. // offset: 6; size: 4; index to SST record
  2973. $index = self::_GetInt4d($recordData, 6);
  2974. // add cell
  2975. if (($fmtRuns = $this->_sst[$index]['fmtRuns']) && !$this->_readDataOnly) {
  2976. // then we should treat as rich text
  2977. $richText = new PHPExcel_RichText();
  2978. $charPos = 0;
  2979. $sstCount = count($this->_sst[$index]['fmtRuns']);
  2980. for ($i = 0; $i <= $sstCount; ++$i) {
  2981. if (isset($fmtRuns[$i])) {
  2982. $text = PHPExcel_Shared_String::Substring($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos);
  2983. $charPos = $fmtRuns[$i]['charPos'];
  2984. } else {
  2985. $text = PHPExcel_Shared_String::Substring($this->_sst[$index]['value'], $charPos, PHPExcel_Shared_String::CountCharacters($this->_sst[$index]['value']));
  2986. }
  2987. if (PHPExcel_Shared_String::CountCharacters($text) > 0) {
  2988. if ($i == 0) { // first text run, no style
  2989. $richText->createText($text);
  2990. } else {
  2991. $textRun = $richText->createTextRun($text);
  2992. if (isset($fmtRuns[$i - 1])) {
  2993. if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
  2994. $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
  2995. } else {
  2996. // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
  2997. // check the OpenOffice documentation of the FONT record
  2998. $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
  2999. }
  3000. $textRun->setFont(clone $this->_objFonts[$fontIndex]);
  3001. }
  3002. }
  3003. }
  3004. }
  3005. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3006. $cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING);
  3007. } else {
  3008. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3009. $cell->setValueExplicit($this->_sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
  3010. }
  3011. if (!$this->_readDataOnly) {
  3012. // add style information
  3013. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3014. }
  3015. }
  3016. }
  3017. /**
  3018. * Read MULRK record
  3019. * This record represents a cell range containing RK value
  3020. * cells. All cells are located in the same row.
  3021. *
  3022. * -- "OpenOffice.org's Documentation of the Microsoft
  3023. * Excel File Format"
  3024. */
  3025. private function _readMulRk()
  3026. {
  3027. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3028. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3029. // move stream pointer to next record
  3030. $this->_pos += 4 + $length;
  3031. // offset: 0; size: 2; index to row
  3032. $row = self::_GetInt2d($recordData, 0);
  3033. // offset: 2; size: 2; index to first column
  3034. $colFirst = self::_GetInt2d($recordData, 2);
  3035. // offset: var; size: 2; index to last column
  3036. $colLast = self::_GetInt2d($recordData, $length - 2);
  3037. $columns = $colLast - $colFirst + 1;
  3038. // offset within record data
  3039. $offset = 4;
  3040. for ($i = 0; $i < $columns; ++$i) {
  3041. $columnString = PHPExcel_Cell::stringFromColumnIndex($colFirst + $i);
  3042. // Read cell?
  3043. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3044. // offset: var; size: 2; index to XF record
  3045. $xfIndex = self::_GetInt2d($recordData, $offset);
  3046. // offset: var; size: 4; RK value
  3047. $numValue = self::_GetIEEE754(self::_GetInt4d($recordData, $offset + 2));
  3048. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3049. if (!$this->_readDataOnly) {
  3050. // add style
  3051. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3052. }
  3053. // add cell value
  3054. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  3055. }
  3056. $offset += 6;
  3057. }
  3058. }
  3059. /**
  3060. * Read NUMBER record
  3061. * This record represents a cell that contains a
  3062. * floating-point value.
  3063. *
  3064. * -- "OpenOffice.org's Documentation of the Microsoft
  3065. * Excel File Format"
  3066. */
  3067. private function _readNumber()
  3068. {
  3069. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3070. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3071. // move stream pointer to next record
  3072. $this->_pos += 4 + $length;
  3073. // offset: 0; size: 2; index to row
  3074. $row = self::_GetInt2d($recordData, 0);
  3075. // offset: 2; size 2; index to column
  3076. $column = self::_GetInt2d($recordData, 2);
  3077. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  3078. // Read cell?
  3079. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3080. // offset 4; size: 2; index to XF record
  3081. $xfIndex = self::_GetInt2d($recordData, 4);
  3082. $numValue = self::_extractNumber(substr($recordData, 6, 8));
  3083. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3084. if (!$this->_readDataOnly) {
  3085. // add cell style
  3086. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3087. }
  3088. // add cell value
  3089. $cell->setValueExplicit($numValue, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  3090. }
  3091. }
  3092. /**
  3093. * Read FORMULA record + perhaps a following STRING record if formula result is a string
  3094. * This record contains the token array and the result of a
  3095. * formula cell.
  3096. *
  3097. * -- "OpenOffice.org's Documentation of the Microsoft
  3098. * Excel File Format"
  3099. */
  3100. private function _readFormula()
  3101. {
  3102. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3103. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3104. // move stream pointer to next record
  3105. $this->_pos += 4 + $length;
  3106. // offset: 0; size: 2; row index
  3107. $row = self::_GetInt2d($recordData, 0);
  3108. // offset: 2; size: 2; col index
  3109. $column = self::_GetInt2d($recordData, 2);
  3110. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  3111. // offset: 20: size: variable; formula structure
  3112. $formulaStructure = substr($recordData, 20);
  3113. // offset: 14: size: 2; option flags, recalculate always, recalculate on open etc.
  3114. $options = self::_GetInt2d($recordData, 14);
  3115. // bit: 0; mask: 0x0001; 1 = recalculate always
  3116. // bit: 1; mask: 0x0002; 1 = calculate on open
  3117. // bit: 2; mask: 0x0008; 1 = part of a shared formula
  3118. $isPartOfSharedFormula = (bool) (0x0008 & $options);
  3119. // WARNING:
  3120. // We can apparently not rely on $isPartOfSharedFormula. Even when $isPartOfSharedFormula = true
  3121. // the formula data may be ordinary formula data, therefore we need to check
  3122. // explicitly for the tExp token (0x01)
  3123. $isPartOfSharedFormula = $isPartOfSharedFormula && ord($formulaStructure{2}) == 0x01;
  3124. if ($isPartOfSharedFormula) {
  3125. // part of shared formula which means there will be a formula with a tExp token and nothing else
  3126. // get the base cell, grab tExp token
  3127. $baseRow = self::_GetInt2d($formulaStructure, 3);
  3128. $baseCol = self::_GetInt2d($formulaStructure, 5);
  3129. $this->_baseCell = PHPExcel_Cell::stringFromColumnIndex($baseCol). ($baseRow + 1);
  3130. }
  3131. // Read cell?
  3132. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3133. if ($isPartOfSharedFormula) {
  3134. // formula is added to this cell after the sheet has been read
  3135. $this->_sharedFormulaParts[$columnString . ($row + 1)] = $this->_baseCell;
  3136. }
  3137. // offset: 16: size: 4; not used
  3138. // offset: 4; size: 2; XF index
  3139. $xfIndex = self::_GetInt2d($recordData, 4);
  3140. // offset: 6; size: 8; result of the formula
  3141. if ( (ord($recordData{6}) == 0)
  3142. && (ord($recordData{12}) == 255)
  3143. && (ord($recordData{13}) == 255) ) {
  3144. // String formula. Result follows in appended STRING record
  3145. $dataType = PHPExcel_Cell_DataType::TYPE_STRING;
  3146. // read possible SHAREDFMLA record
  3147. $code = self::_GetInt2d($this->_data, $this->_pos);
  3148. if ($code == self::XLS_Type_SHAREDFMLA) {
  3149. $this->_readSharedFmla();
  3150. }
  3151. // read STRING record
  3152. $value = $this->_readString();
  3153. } elseif ((ord($recordData{6}) == 1)
  3154. && (ord($recordData{12}) == 255)
  3155. && (ord($recordData{13}) == 255)) {
  3156. // Boolean formula. Result is in +2; 0=false, 1=true
  3157. $dataType = PHPExcel_Cell_DataType::TYPE_BOOL;
  3158. $value = (bool) ord($recordData{8});
  3159. } elseif ((ord($recordData{6}) == 2)
  3160. && (ord($recordData{12}) == 255)
  3161. && (ord($recordData{13}) == 255)) {
  3162. // Error formula. Error code is in +2
  3163. $dataType = PHPExcel_Cell_DataType::TYPE_ERROR;
  3164. $value = self::_mapErrorCode(ord($recordData{8}));
  3165. } elseif ((ord($recordData{6}) == 3)
  3166. && (ord($recordData{12}) == 255)
  3167. && (ord($recordData{13}) == 255)) {
  3168. // Formula result is a null string
  3169. $dataType = PHPExcel_Cell_DataType::TYPE_NULL;
  3170. $value = '';
  3171. } else {
  3172. // forumla result is a number, first 14 bytes like _NUMBER record
  3173. $dataType = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  3174. $value = self::_extractNumber(substr($recordData, 6, 8));
  3175. }
  3176. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3177. if (!$this->_readDataOnly) {
  3178. // add cell style
  3179. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3180. }
  3181. // store the formula
  3182. if (!$isPartOfSharedFormula) {
  3183. // not part of shared formula
  3184. // add cell value. If we can read formula, populate with formula, otherwise just used cached value
  3185. try {
  3186. if ($this->_version != self::XLS_BIFF8) {
  3187. throw new PHPExcel_Reader_Exception('Not BIFF8. Can only read BIFF8 formulas');
  3188. }
  3189. $formula = $this->_getFormulaFromStructure($formulaStructure); // get formula in human language
  3190. $cell->setValueExplicit('=' . $formula, PHPExcel_Cell_DataType::TYPE_FORMULA);
  3191. } catch (PHPExcel_Exception $e) {
  3192. $cell->setValueExplicit($value, $dataType);
  3193. }
  3194. } else {
  3195. if ($this->_version == self::XLS_BIFF8) {
  3196. // do nothing at this point, formula id added later in the code
  3197. } else {
  3198. $cell->setValueExplicit($value, $dataType);
  3199. }
  3200. }
  3201. // store the cached calculated value
  3202. $cell->setCalculatedValue($value);
  3203. }
  3204. }
  3205. /**
  3206. * Read a SHAREDFMLA record. This function just stores the binary shared formula in the reader,
  3207. * which usually contains relative references.
  3208. * These will be used to construct the formula in each shared formula part after the sheet is read.
  3209. */
  3210. private function _readSharedFmla()
  3211. {
  3212. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3213. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3214. // move stream pointer to next record
  3215. $this->_pos += 4 + $length;
  3216. // offset: 0, size: 6; cell range address of the area used by the shared formula, not used for anything
  3217. $cellRange = substr($recordData, 0, 6);
  3218. $cellRange = $this->_readBIFF5CellRangeAddressFixed($cellRange); // note: even BIFF8 uses BIFF5 syntax
  3219. // offset: 6, size: 1; not used
  3220. // offset: 7, size: 1; number of existing FORMULA records for this shared formula
  3221. $no = ord($recordData{7});
  3222. // offset: 8, size: var; Binary token array of the shared formula
  3223. $formula = substr($recordData, 8);
  3224. // at this point we only store the shared formula for later use
  3225. $this->_sharedFormulas[$this->_baseCell] = $formula;
  3226. }
  3227. /**
  3228. * Read a STRING record from current stream position and advance the stream pointer to next record
  3229. * This record is used for storing result from FORMULA record when it is a string, and
  3230. * it occurs directly after the FORMULA record
  3231. *
  3232. * @return string The string contents as UTF-8
  3233. */
  3234. private function _readString()
  3235. {
  3236. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3237. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3238. // move stream pointer to next record
  3239. $this->_pos += 4 + $length;
  3240. if ($this->_version == self::XLS_BIFF8) {
  3241. $string = self::_readUnicodeStringLong($recordData);
  3242. $value = $string['value'];
  3243. } else {
  3244. $string = $this->_readByteStringLong($recordData);
  3245. $value = $string['value'];
  3246. }
  3247. return $value;
  3248. }
  3249. /**
  3250. * Read BOOLERR record
  3251. * This record represents a Boolean value or error value
  3252. * cell.
  3253. *
  3254. * -- "OpenOffice.org's Documentation of the Microsoft
  3255. * Excel File Format"
  3256. */
  3257. private function _readBoolErr()
  3258. {
  3259. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3260. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3261. // move stream pointer to next record
  3262. $this->_pos += 4 + $length;
  3263. // offset: 0; size: 2; row index
  3264. $row = self::_GetInt2d($recordData, 0);
  3265. // offset: 2; size: 2; column index
  3266. $column = self::_GetInt2d($recordData, 2);
  3267. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  3268. // Read cell?
  3269. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3270. // offset: 4; size: 2; index to XF record
  3271. $xfIndex = self::_GetInt2d($recordData, 4);
  3272. // offset: 6; size: 1; the boolean value or error value
  3273. $boolErr = ord($recordData{6});
  3274. // offset: 7; size: 1; 0=boolean; 1=error
  3275. $isError = ord($recordData{7});
  3276. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3277. switch ($isError) {
  3278. case 0: // boolean
  3279. $value = (bool) $boolErr;
  3280. // add cell value
  3281. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_BOOL);
  3282. break;
  3283. case 1: // error type
  3284. $value = self::_mapErrorCode($boolErr);
  3285. // add cell value
  3286. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_ERROR);
  3287. break;
  3288. }
  3289. if (!$this->_readDataOnly) {
  3290. // add cell style
  3291. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3292. }
  3293. }
  3294. }
  3295. /**
  3296. * Read MULBLANK record
  3297. * This record represents a cell range of empty cells. All
  3298. * cells are located in the same row
  3299. *
  3300. * -- "OpenOffice.org's Documentation of the Microsoft
  3301. * Excel File Format"
  3302. */
  3303. private function _readMulBlank()
  3304. {
  3305. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3306. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3307. // move stream pointer to next record
  3308. $this->_pos += 4 + $length;
  3309. // offset: 0; size: 2; index to row
  3310. $row = self::_GetInt2d($recordData, 0);
  3311. // offset: 2; size: 2; index to first column
  3312. $fc = self::_GetInt2d($recordData, 2);
  3313. // offset: 4; size: 2 x nc; list of indexes to XF records
  3314. // add style information
  3315. if (!$this->_readDataOnly) {
  3316. for ($i = 0; $i < $length / 2 - 3; ++$i) {
  3317. $columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i);
  3318. // Read cell?
  3319. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3320. $xfIndex = self::_GetInt2d($recordData, 4 + 2 * $i);
  3321. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3322. }
  3323. }
  3324. }
  3325. // offset: 6; size 2; index to last column (not needed)
  3326. }
  3327. /**
  3328. * Read LABEL record
  3329. * This record represents a cell that contains a string. In
  3330. * BIFF8 it is usually replaced by the LABELSST record.
  3331. * Excel still uses this record, if it copies unformatted
  3332. * text cells to the clipboard.
  3333. *
  3334. * -- "OpenOffice.org's Documentation of the Microsoft
  3335. * Excel File Format"
  3336. */
  3337. private function _readLabel()
  3338. {
  3339. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3340. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3341. // move stream pointer to next record
  3342. $this->_pos += 4 + $length;
  3343. // offset: 0; size: 2; index to row
  3344. $row = self::_GetInt2d($recordData, 0);
  3345. // offset: 2; size: 2; index to column
  3346. $column = self::_GetInt2d($recordData, 2);
  3347. $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
  3348. // Read cell?
  3349. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3350. // offset: 4; size: 2; XF index
  3351. $xfIndex = self::_GetInt2d($recordData, 4);
  3352. // add cell value
  3353. // todo: what if string is very long? continue record
  3354. if ($this->_version == self::XLS_BIFF8) {
  3355. $string = self::_readUnicodeStringLong(substr($recordData, 6));
  3356. $value = $string['value'];
  3357. } else {
  3358. $string = $this->_readByteStringLong(substr($recordData, 6));
  3359. $value = $string['value'];
  3360. }
  3361. $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
  3362. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
  3363. if (!$this->_readDataOnly) {
  3364. // add cell style
  3365. $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3366. }
  3367. }
  3368. }
  3369. /**
  3370. * Read BLANK record
  3371. */
  3372. private function _readBlank()
  3373. {
  3374. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3375. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3376. // move stream pointer to next record
  3377. $this->_pos += 4 + $length;
  3378. // offset: 0; size: 2; row index
  3379. $row = self::_GetInt2d($recordData, 0);
  3380. // offset: 2; size: 2; col index
  3381. $col = self::_GetInt2d($recordData, 2);
  3382. $columnString = PHPExcel_Cell::stringFromColumnIndex($col);
  3383. // Read cell?
  3384. if (($this->getReadFilter() !== NULL) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle()) ) {
  3385. // offset: 4; size: 2; XF index
  3386. $xfIndex = self::_GetInt2d($recordData, 4);
  3387. // add style information
  3388. if (!$this->_readDataOnly) {
  3389. $this->_phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
  3390. }
  3391. }
  3392. }
  3393. /**
  3394. * Read MSODRAWING record
  3395. */
  3396. private function _readMsoDrawing()
  3397. {
  3398. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3399. // get spliced record data
  3400. $splicedRecordData = $this->_getSplicedRecordData();
  3401. $recordData = $splicedRecordData['recordData'];
  3402. $this->_drawingData .= $recordData;
  3403. }
  3404. /**
  3405. * Read OBJ record
  3406. */
  3407. private function _readObj()
  3408. {
  3409. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3410. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3411. // move stream pointer to next record
  3412. $this->_pos += 4 + $length;
  3413. if ($this->_readDataOnly || $this->_version != self::XLS_BIFF8) {
  3414. return;
  3415. }
  3416. // recordData consists of an array of subrecords looking like this:
  3417. // ft: 2 bytes; ftCmo type (0x15)
  3418. // cb: 2 bytes; size in bytes of ftCmo data
  3419. // ot: 2 bytes; Object Type
  3420. // id: 2 bytes; Object id number
  3421. // grbit: 2 bytes; Option Flags
  3422. // data: var; subrecord data
  3423. // for now, we are just interested in the second subrecord containing the object type
  3424. $ftCmoType = self::_GetInt2d($recordData, 0);
  3425. $cbCmoSize = self::_GetInt2d($recordData, 2);
  3426. $otObjType = self::_GetInt2d($recordData, 4);
  3427. $idObjID = self::_GetInt2d($recordData, 6);
  3428. $grbitOpts = self::_GetInt2d($recordData, 6);
  3429. $this->_objs[] = array(
  3430. 'ftCmoType' => $ftCmoType,
  3431. 'cbCmoSize' => $cbCmoSize,
  3432. 'otObjType' => $otObjType,
  3433. 'idObjID' => $idObjID,
  3434. 'grbitOpts' => $grbitOpts
  3435. );
  3436. $this->textObjRef = $idObjID;
  3437. // echo '<b>_readObj()</b><br />';
  3438. // var_dump(end($this->_objs));
  3439. // echo '<br />';
  3440. }
  3441. /**
  3442. * Read WINDOW2 record
  3443. */
  3444. private function _readWindow2()
  3445. {
  3446. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3447. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3448. // move stream pointer to next record
  3449. $this->_pos += 4 + $length;
  3450. // offset: 0; size: 2; option flags
  3451. $options = self::_GetInt2d($recordData, 0);
  3452. // offset: 2; size: 2; index to first visible row
  3453. $firstVisibleRow = self::_GetInt2d($recordData, 2);
  3454. // offset: 4; size: 2; index to first visible colum
  3455. $firstVisibleColumn = self::_GetInt2d($recordData, 4);
  3456. if ($this->_version === self::XLS_BIFF8) {
  3457. // offset: 8; size: 2; not used
  3458. // offset: 10; size: 2; cached magnification factor in page break preview (in percent); 0 = Default (60%)
  3459. // offset: 12; size: 2; cached magnification factor in normal view (in percent); 0 = Default (100%)
  3460. // offset: 14; size: 4; not used
  3461. $zoomscaleInPageBreakPreview = self::_GetInt2d($recordData, 10);
  3462. if ($zoomscaleInPageBreakPreview === 0) $zoomscaleInPageBreakPreview = 60;
  3463. $zoomscaleInNormalView = self::_GetInt2d($recordData, 12);
  3464. if ($zoomscaleInNormalView === 0) $zoomscaleInNormalView = 100;
  3465. }
  3466. // bit: 1; mask: 0x0002; 0 = do not show gridlines, 1 = show gridlines
  3467. $showGridlines = (bool) ((0x0002 & $options) >> 1);
  3468. $this->_phpSheet->setShowGridlines($showGridlines);
  3469. // bit: 2; mask: 0x0004; 0 = do not show headers, 1 = show headers
  3470. $showRowColHeaders = (bool) ((0x0004 & $options) >> 2);
  3471. $this->_phpSheet->setShowRowColHeaders($showRowColHeaders);
  3472. // bit: 3; mask: 0x0008; 0 = panes are not frozen, 1 = panes are frozen
  3473. $this->_frozen = (bool) ((0x0008 & $options) >> 3);
  3474. // bit: 6; mask: 0x0040; 0 = columns from left to right, 1 = columns from right to left
  3475. $this->_phpSheet->setRightToLeft((bool)((0x0040 & $options) >> 6));
  3476. // bit: 10; mask: 0x0400; 0 = sheet not active, 1 = sheet active
  3477. $isActive = (bool) ((0x0400 & $options) >> 10);
  3478. if ($isActive) {
  3479. $this->_phpExcel->setActiveSheetIndex($this->_phpExcel->getIndex($this->_phpSheet));
  3480. }
  3481. // bit: 11; mask: 0x0800; 0 = normal view, 1 = page break view
  3482. $isPageBreakPreview = (bool) ((0x0800 & $options) >> 11);
  3483. //FIXME: set $firstVisibleRow and $firstVisibleColumn
  3484. if ($this->_phpSheet->getSheetView()->getView() !== PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_LAYOUT) {
  3485. //NOTE: this setting is inferior to page layout view(Excel2007-)
  3486. $view = $isPageBreakPreview? PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW :
  3487. PHPExcel_Worksheet_SheetView::SHEETVIEW_NORMAL;
  3488. $this->_phpSheet->getSheetView()->setView($view);
  3489. if ($this->_version === self::XLS_BIFF8) {
  3490. $zoomScale = $isPageBreakPreview? $zoomscaleInPageBreakPreview : $zoomscaleInNormalView;
  3491. $this->_phpSheet->getSheetView()->setZoomScale($zoomScale);
  3492. $this->_phpSheet->getSheetView()->setZoomScaleNormal($zoomscaleInNormalView);
  3493. }
  3494. }
  3495. }
  3496. /**
  3497. * Read PLV Record(Created by Excel2007 or upper)
  3498. */
  3499. private function _readPageLayoutView(){
  3500. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3501. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3502. // move stream pointer to next record
  3503. $this->_pos += 4 + $length;
  3504. //var_dump(unpack("vrt/vgrbitFrt/V2reserved/vwScalePLV/vgrbit", $recordData));
  3505. // offset: 0; size: 2; rt
  3506. //->ignore
  3507. $rt = self::_GetInt2d($recordData, 0);
  3508. // offset: 2; size: 2; grbitfr
  3509. //->ignore
  3510. $grbitFrt = self::_GetInt2d($recordData, 2);
  3511. // offset: 4; size: 8; reserved
  3512. //->ignore
  3513. // offset: 12; size 2; zoom scale
  3514. $wScalePLV = self::_GetInt2d($recordData, 12);
  3515. // offset: 14; size 2; grbit
  3516. $grbit = self::_GetInt2d($recordData, 14);
  3517. // decomprise grbit
  3518. $fPageLayoutView = $grbit & 0x01;
  3519. $fRulerVisible = ($grbit >> 1) & 0x01; //no support
  3520. $fWhitespaceHidden = ($grbit >> 3) & 0x01; //no support
  3521. if ($fPageLayoutView === 1) {
  3522. $this->_phpSheet->getSheetView()->setView(PHPExcel_Worksheet_SheetView::SHEETVIEW_PAGE_LAYOUT);
  3523. $this->_phpSheet->getSheetView()->setZoomScale($wScalePLV); //set by Excel2007 only if SHEETVIEW_PAGE_LAYOUT
  3524. }
  3525. //otherwise, we cannot know whether SHEETVIEW_PAGE_LAYOUT or SHEETVIEW_PAGE_BREAK_PREVIEW.
  3526. }
  3527. /**
  3528. * Read SCL record
  3529. */
  3530. private function _readScl()
  3531. {
  3532. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3533. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3534. // move stream pointer to next record
  3535. $this->_pos += 4 + $length;
  3536. // offset: 0; size: 2; numerator of the view magnification
  3537. $numerator = self::_GetInt2d($recordData, 0);
  3538. // offset: 2; size: 2; numerator of the view magnification
  3539. $denumerator = self::_GetInt2d($recordData, 2);
  3540. // set the zoom scale (in percent)
  3541. $this->_phpSheet->getSheetView()->setZoomScale($numerator * 100 / $denumerator);
  3542. }
  3543. /**
  3544. * Read PANE record
  3545. */
  3546. private function _readPane()
  3547. {
  3548. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3549. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3550. // move stream pointer to next record
  3551. $this->_pos += 4 + $length;
  3552. if (!$this->_readDataOnly) {
  3553. // offset: 0; size: 2; position of vertical split
  3554. $px = self::_GetInt2d($recordData, 0);
  3555. // offset: 2; size: 2; position of horizontal split
  3556. $py = self::_GetInt2d($recordData, 2);
  3557. if ($this->_frozen) {
  3558. // frozen panes
  3559. $this->_phpSheet->freezePane(PHPExcel_Cell::stringFromColumnIndex($px) . ($py + 1));
  3560. } else {
  3561. // unfrozen panes; split windows; not supported by PHPExcel core
  3562. }
  3563. }
  3564. }
  3565. /**
  3566. * Read SELECTION record. There is one such record for each pane in the sheet.
  3567. */
  3568. private function _readSelection()
  3569. {
  3570. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3571. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3572. // move stream pointer to next record
  3573. $this->_pos += 4 + $length;
  3574. if (!$this->_readDataOnly) {
  3575. // offset: 0; size: 1; pane identifier
  3576. $paneId = ord($recordData{0});
  3577. // offset: 1; size: 2; index to row of the active cell
  3578. $r = self::_GetInt2d($recordData, 1);
  3579. // offset: 3; size: 2; index to column of the active cell
  3580. $c = self::_GetInt2d($recordData, 3);
  3581. // offset: 5; size: 2; index into the following cell range list to the
  3582. // entry that contains the active cell
  3583. $index = self::_GetInt2d($recordData, 5);
  3584. // offset: 7; size: var; cell range address list containing all selected cell ranges
  3585. $data = substr($recordData, 7);
  3586. $cellRangeAddressList = $this->_readBIFF5CellRangeAddressList($data); // note: also BIFF8 uses BIFF5 syntax
  3587. $selectedCells = $cellRangeAddressList['cellRangeAddresses'][0];
  3588. // first row '1' + last row '16384' indicates that full column is selected (apparently also in BIFF8!)
  3589. if (preg_match('/^([A-Z]+1\:[A-Z]+)16384$/', $selectedCells)) {
  3590. $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)16384$/', '${1}1048576', $selectedCells);
  3591. }
  3592. // first row '1' + last row '65536' indicates that full column is selected
  3593. if (preg_match('/^([A-Z]+1\:[A-Z]+)65536$/', $selectedCells)) {
  3594. $selectedCells = preg_replace('/^([A-Z]+1\:[A-Z]+)65536$/', '${1}1048576', $selectedCells);
  3595. }
  3596. // first column 'A' + last column 'IV' indicates that full row is selected
  3597. if (preg_match('/^(A[0-9]+\:)IV([0-9]+)$/', $selectedCells)) {
  3598. $selectedCells = preg_replace('/^(A[0-9]+\:)IV([0-9]+)$/', '${1}XFD${2}', $selectedCells);
  3599. }
  3600. $this->_phpSheet->setSelectedCells($selectedCells);
  3601. }
  3602. }
  3603. private function _includeCellRangeFiltered($cellRangeAddress)
  3604. {
  3605. $includeCellRange = true;
  3606. if ($this->getReadFilter() !== NULL) {
  3607. $includeCellRange = false;
  3608. $rangeBoundaries = PHPExcel_Cell::getRangeBoundaries($cellRangeAddress);
  3609. $rangeBoundaries[1][0]++;
  3610. for ($row = $rangeBoundaries[0][1]; $row <= $rangeBoundaries[1][1]; $row++) {
  3611. for ($column = $rangeBoundaries[0][0]; $column != $rangeBoundaries[1][0]; $column++) {
  3612. if ($this->getReadFilter()->readCell($column, $row, $this->_phpSheet->getTitle())) {
  3613. $includeCellRange = true;
  3614. break 2;
  3615. }
  3616. }
  3617. }
  3618. }
  3619. return $includeCellRange;
  3620. }
  3621. /**
  3622. * MERGEDCELLS
  3623. *
  3624. * This record contains the addresses of merged cell ranges
  3625. * in the current sheet.
  3626. *
  3627. * -- "OpenOffice.org's Documentation of the Microsoft
  3628. * Excel File Format"
  3629. */
  3630. private function _readMergedCells()
  3631. {
  3632. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3633. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3634. // move stream pointer to next record
  3635. $this->_pos += 4 + $length;
  3636. if ($this->_version == self::XLS_BIFF8 && !$this->_readDataOnly) {
  3637. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($recordData);
  3638. foreach ($cellRangeAddressList['cellRangeAddresses'] as $cellRangeAddress) {
  3639. if ((strpos($cellRangeAddress,':') !== FALSE) &&
  3640. ($this->_includeCellRangeFiltered($cellRangeAddress))) {
  3641. $this->_phpSheet->mergeCells($cellRangeAddress);
  3642. }
  3643. }
  3644. }
  3645. }
  3646. /**
  3647. * Read HYPERLINK record
  3648. */
  3649. private function _readHyperLink()
  3650. {
  3651. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3652. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3653. // move stream pointer forward to next record
  3654. $this->_pos += 4 + $length;
  3655. if (!$this->_readDataOnly) {
  3656. // offset: 0; size: 8; cell range address of all cells containing this hyperlink
  3657. try {
  3658. $cellRange = $this->_readBIFF8CellRangeAddressFixed($recordData, 0, 8);
  3659. } catch (PHPExcel_Exception $e) {
  3660. return;
  3661. }
  3662. // offset: 8, size: 16; GUID of StdLink
  3663. // offset: 24, size: 4; unknown value
  3664. // offset: 28, size: 4; option flags
  3665. // bit: 0; mask: 0x00000001; 0 = no link or extant, 1 = file link or URL
  3666. $isFileLinkOrUrl = (0x00000001 & self::_GetInt2d($recordData, 28)) >> 0;
  3667. // bit: 1; mask: 0x00000002; 0 = relative path, 1 = absolute path or URL
  3668. $isAbsPathOrUrl = (0x00000001 & self::_GetInt2d($recordData, 28)) >> 1;
  3669. // bit: 2 (and 4); mask: 0x00000014; 0 = no description
  3670. $hasDesc = (0x00000014 & self::_GetInt2d($recordData, 28)) >> 2;
  3671. // bit: 3; mask: 0x00000008; 0 = no text, 1 = has text
  3672. $hasText = (0x00000008 & self::_GetInt2d($recordData, 28)) >> 3;
  3673. // bit: 7; mask: 0x00000080; 0 = no target frame, 1 = has target frame
  3674. $hasFrame = (0x00000080 & self::_GetInt2d($recordData, 28)) >> 7;
  3675. // bit: 8; mask: 0x00000100; 0 = file link or URL, 1 = UNC path (inc. server name)
  3676. $isUNC = (0x00000100 & self::_GetInt2d($recordData, 28)) >> 8;
  3677. // offset within record data
  3678. $offset = 32;
  3679. if ($hasDesc) {
  3680. // offset: 32; size: var; character count of description text
  3681. $dl = self::_GetInt4d($recordData, 32);
  3682. // offset: 36; size: var; character array of description text, no Unicode string header, always 16-bit characters, zero terminated
  3683. $desc = self::_encodeUTF16(substr($recordData, 36, 2 * ($dl - 1)), false);
  3684. $offset += 4 + 2 * $dl;
  3685. }
  3686. if ($hasFrame) {
  3687. $fl = self::_GetInt4d($recordData, $offset);
  3688. $offset += 4 + 2 * $fl;
  3689. }
  3690. // detect type of hyperlink (there are 4 types)
  3691. $hyperlinkType = null;
  3692. if ($isUNC) {
  3693. $hyperlinkType = 'UNC';
  3694. } else if (!$isFileLinkOrUrl) {
  3695. $hyperlinkType = 'workbook';
  3696. } else if (ord($recordData{$offset}) == 0x03) {
  3697. $hyperlinkType = 'local';
  3698. } else if (ord($recordData{$offset}) == 0xE0) {
  3699. $hyperlinkType = 'URL';
  3700. }
  3701. switch ($hyperlinkType) {
  3702. case 'URL':
  3703. // section 5.58.2: Hyperlink containing a URL
  3704. // e.g. http://example.org/index.php
  3705. // offset: var; size: 16; GUID of URL Moniker
  3706. $offset += 16;
  3707. // offset: var; size: 4; size (in bytes) of character array of the URL including trailing zero word
  3708. $us = self::_GetInt4d($recordData, $offset);
  3709. $offset += 4;
  3710. // offset: var; size: $us; character array of the URL, no Unicode string header, always 16-bit characters, zero-terminated
  3711. $url = self::_encodeUTF16(substr($recordData, $offset, $us - 2), false);
  3712. $nullOffset = strpos($url, 0x00);
  3713. if ($nullOffset)
  3714. $url = substr($url,0,$nullOffset);
  3715. $url .= $hasText ? '#' : '';
  3716. $offset += $us;
  3717. break;
  3718. case 'local':
  3719. // section 5.58.3: Hyperlink to local file
  3720. // examples:
  3721. // mydoc.txt
  3722. // ../../somedoc.xls#Sheet!A1
  3723. // offset: var; size: 16; GUI of File Moniker
  3724. $offset += 16;
  3725. // offset: var; size: 2; directory up-level count.
  3726. $upLevelCount = self::_GetInt2d($recordData, $offset);
  3727. $offset += 2;
  3728. // offset: var; size: 4; character count of the shortened file path and name, including trailing zero word
  3729. $sl = self::_GetInt4d($recordData, $offset);
  3730. $offset += 4;
  3731. // offset: var; size: sl; character array of the shortened file path and name in 8.3-DOS-format (compressed Unicode string)
  3732. $shortenedFilePath = substr($recordData, $offset, $sl);
  3733. $shortenedFilePath = self::_encodeUTF16($shortenedFilePath, true);
  3734. $shortenedFilePath = substr($shortenedFilePath, 0, -1); // remove trailing zero
  3735. $offset += $sl;
  3736. // offset: var; size: 24; unknown sequence
  3737. $offset += 24;
  3738. // extended file path
  3739. // offset: var; size: 4; size of the following file link field including string lenth mark
  3740. $sz = self::_GetInt4d($recordData, $offset);
  3741. $offset += 4;
  3742. // only present if $sz > 0
  3743. if ($sz > 0) {
  3744. // offset: var; size: 4; size of the character array of the extended file path and name
  3745. $xl = self::_GetInt4d($recordData, $offset);
  3746. $offset += 4;
  3747. // offset: var; size 2; unknown
  3748. $offset += 2;
  3749. // offset: var; size $xl; character array of the extended file path and name.
  3750. $extendedFilePath = substr($recordData, $offset, $xl);
  3751. $extendedFilePath = self::_encodeUTF16($extendedFilePath, false);
  3752. $offset += $xl;
  3753. }
  3754. // construct the path
  3755. $url = str_repeat('..\\', $upLevelCount);
  3756. $url .= ($sz > 0) ?
  3757. $extendedFilePath : $shortenedFilePath; // use extended path if available
  3758. $url .= $hasText ? '#' : '';
  3759. break;
  3760. case 'UNC':
  3761. // section 5.58.4: Hyperlink to a File with UNC (Universal Naming Convention) Path
  3762. // todo: implement
  3763. return;
  3764. case 'workbook':
  3765. // section 5.58.5: Hyperlink to the Current Workbook
  3766. // e.g. Sheet2!B1:C2, stored in text mark field
  3767. $url = 'sheet://';
  3768. break;
  3769. default:
  3770. return;
  3771. }
  3772. if ($hasText) {
  3773. // offset: var; size: 4; character count of text mark including trailing zero word
  3774. $tl = self::_GetInt4d($recordData, $offset);
  3775. $offset += 4;
  3776. // offset: var; size: var; character array of the text mark without the # sign, no Unicode header, always 16-bit characters, zero-terminated
  3777. $text = self::_encodeUTF16(substr($recordData, $offset, 2 * ($tl - 1)), false);
  3778. $url .= $text;
  3779. }
  3780. // apply the hyperlink to all the relevant cells
  3781. foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cellRange) as $coordinate) {
  3782. $this->_phpSheet->getCell($coordinate)->getHyperLink()->setUrl($url);
  3783. }
  3784. }
  3785. }
  3786. /**
  3787. * Read DATAVALIDATIONS record
  3788. */
  3789. private function _readDataValidations()
  3790. {
  3791. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3792. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3793. // move stream pointer forward to next record
  3794. $this->_pos += 4 + $length;
  3795. }
  3796. /**
  3797. * Read DATAVALIDATION record
  3798. */
  3799. private function _readDataValidation()
  3800. {
  3801. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3802. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3803. // move stream pointer forward to next record
  3804. $this->_pos += 4 + $length;
  3805. if ($this->_readDataOnly) {
  3806. return;
  3807. }
  3808. // offset: 0; size: 4; Options
  3809. $options = self::_GetInt4d($recordData, 0);
  3810. // bit: 0-3; mask: 0x0000000F; type
  3811. $type = (0x0000000F & $options) >> 0;
  3812. switch ($type) {
  3813. case 0x00: $type = PHPExcel_Cell_DataValidation::TYPE_NONE; break;
  3814. case 0x01: $type = PHPExcel_Cell_DataValidation::TYPE_WHOLE; break;
  3815. case 0x02: $type = PHPExcel_Cell_DataValidation::TYPE_DECIMAL; break;
  3816. case 0x03: $type = PHPExcel_Cell_DataValidation::TYPE_LIST; break;
  3817. case 0x04: $type = PHPExcel_Cell_DataValidation::TYPE_DATE; break;
  3818. case 0x05: $type = PHPExcel_Cell_DataValidation::TYPE_TIME; break;
  3819. case 0x06: $type = PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH; break;
  3820. case 0x07: $type = PHPExcel_Cell_DataValidation::TYPE_CUSTOM; break;
  3821. }
  3822. // bit: 4-6; mask: 0x00000070; error type
  3823. $errorStyle = (0x00000070 & $options) >> 4;
  3824. switch ($errorStyle) {
  3825. case 0x00: $errorStyle = PHPExcel_Cell_DataValidation::STYLE_STOP; break;
  3826. case 0x01: $errorStyle = PHPExcel_Cell_DataValidation::STYLE_WARNING; break;
  3827. case 0x02: $errorStyle = PHPExcel_Cell_DataValidation::STYLE_INFORMATION; break;
  3828. }
  3829. // bit: 7; mask: 0x00000080; 1= formula is explicit (only applies to list)
  3830. // I have only seen cases where this is 1
  3831. $explicitFormula = (0x00000080 & $options) >> 7;
  3832. // bit: 8; mask: 0x00000100; 1= empty cells allowed
  3833. $allowBlank = (0x00000100 & $options) >> 8;
  3834. // bit: 9; mask: 0x00000200; 1= suppress drop down arrow in list type validity
  3835. $suppressDropDown = (0x00000200 & $options) >> 9;
  3836. // bit: 18; mask: 0x00040000; 1= show prompt box if cell selected
  3837. $showInputMessage = (0x00040000 & $options) >> 18;
  3838. // bit: 19; mask: 0x00080000; 1= show error box if invalid values entered
  3839. $showErrorMessage = (0x00080000 & $options) >> 19;
  3840. // bit: 20-23; mask: 0x00F00000; condition operator
  3841. $operator = (0x00F00000 & $options) >> 20;
  3842. switch ($operator) {
  3843. case 0x00: $operator = PHPExcel_Cell_DataValidation::OPERATOR_BETWEEN ; break;
  3844. case 0x01: $operator = PHPExcel_Cell_DataValidation::OPERATOR_NOTBETWEEN ; break;
  3845. case 0x02: $operator = PHPExcel_Cell_DataValidation::OPERATOR_EQUAL ; break;
  3846. case 0x03: $operator = PHPExcel_Cell_DataValidation::OPERATOR_NOTEQUAL ; break;
  3847. case 0x04: $operator = PHPExcel_Cell_DataValidation::OPERATOR_GREATERTHAN ; break;
  3848. case 0x05: $operator = PHPExcel_Cell_DataValidation::OPERATOR_LESSTHAN ; break;
  3849. case 0x06: $operator = PHPExcel_Cell_DataValidation::OPERATOR_GREATERTHANOREQUAL; break;
  3850. case 0x07: $operator = PHPExcel_Cell_DataValidation::OPERATOR_LESSTHANOREQUAL ; break;
  3851. }
  3852. // offset: 4; size: var; title of the prompt box
  3853. $offset = 4;
  3854. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3855. $promptTitle = $string['value'] !== chr(0) ?
  3856. $string['value'] : '';
  3857. $offset += $string['size'];
  3858. // offset: var; size: var; title of the error box
  3859. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3860. $errorTitle = $string['value'] !== chr(0) ?
  3861. $string['value'] : '';
  3862. $offset += $string['size'];
  3863. // offset: var; size: var; text of the prompt box
  3864. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3865. $prompt = $string['value'] !== chr(0) ?
  3866. $string['value'] : '';
  3867. $offset += $string['size'];
  3868. // offset: var; size: var; text of the error box
  3869. $string = self::_readUnicodeStringLong(substr($recordData, $offset));
  3870. $error = $string['value'] !== chr(0) ?
  3871. $string['value'] : '';
  3872. $offset += $string['size'];
  3873. // offset: var; size: 2; size of the formula data for the first condition
  3874. $sz1 = self::_GetInt2d($recordData, $offset);
  3875. $offset += 2;
  3876. // offset: var; size: 2; not used
  3877. $offset += 2;
  3878. // offset: var; size: $sz1; formula data for first condition (without size field)
  3879. $formula1 = substr($recordData, $offset, $sz1);
  3880. $formula1 = pack('v', $sz1) . $formula1; // prepend the length
  3881. try {
  3882. $formula1 = $this->_getFormulaFromStructure($formula1);
  3883. // in list type validity, null characters are used as item separators
  3884. if ($type == PHPExcel_Cell_DataValidation::TYPE_LIST) {
  3885. $formula1 = str_replace(chr(0), ',', $formula1);
  3886. }
  3887. } catch (PHPExcel_Exception $e) {
  3888. return;
  3889. }
  3890. $offset += $sz1;
  3891. // offset: var; size: 2; size of the formula data for the first condition
  3892. $sz2 = self::_GetInt2d($recordData, $offset);
  3893. $offset += 2;
  3894. // offset: var; size: 2; not used
  3895. $offset += 2;
  3896. // offset: var; size: $sz2; formula data for second condition (without size field)
  3897. $formula2 = substr($recordData, $offset, $sz2);
  3898. $formula2 = pack('v', $sz2) . $formula2; // prepend the length
  3899. try {
  3900. $formula2 = $this->_getFormulaFromStructure($formula2);
  3901. } catch (PHPExcel_Exception $e) {
  3902. return;
  3903. }
  3904. $offset += $sz2;
  3905. // offset: var; size: var; cell range address list with
  3906. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList(substr($recordData, $offset));
  3907. $cellRangeAddresses = $cellRangeAddressList['cellRangeAddresses'];
  3908. foreach ($cellRangeAddresses as $cellRange) {
  3909. $stRange = $this->_phpSheet->shrinkRangeToFit($cellRange);
  3910. $stRange = PHPExcel_Cell::extractAllCellReferencesInRange($stRange);
  3911. foreach ($stRange as $coordinate) {
  3912. $objValidation = $this->_phpSheet->getCell($coordinate)->getDataValidation();
  3913. $objValidation->setType($type);
  3914. $objValidation->setErrorStyle($errorStyle);
  3915. $objValidation->setAllowBlank((bool)$allowBlank);
  3916. $objValidation->setShowInputMessage((bool)$showInputMessage);
  3917. $objValidation->setShowErrorMessage((bool)$showErrorMessage);
  3918. $objValidation->setShowDropDown(!$suppressDropDown);
  3919. $objValidation->setOperator($operator);
  3920. $objValidation->setErrorTitle($errorTitle);
  3921. $objValidation->setError($error);
  3922. $objValidation->setPromptTitle($promptTitle);
  3923. $objValidation->setPrompt($prompt);
  3924. $objValidation->setFormula1($formula1);
  3925. $objValidation->setFormula2($formula2);
  3926. }
  3927. }
  3928. }
  3929. /**
  3930. * Read SHEETLAYOUT record. Stores sheet tab color information.
  3931. */
  3932. private function _readSheetLayout()
  3933. {
  3934. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3935. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3936. // move stream pointer to next record
  3937. $this->_pos += 4 + $length;
  3938. // local pointer in record data
  3939. $offset = 0;
  3940. if (!$this->_readDataOnly) {
  3941. // offset: 0; size: 2; repeated record identifier 0x0862
  3942. // offset: 2; size: 10; not used
  3943. // offset: 12; size: 4; size of record data
  3944. // Excel 2003 uses size of 0x14 (documented), Excel 2007 uses size of 0x28 (not documented?)
  3945. $sz = self::_GetInt4d($recordData, 12);
  3946. switch ($sz) {
  3947. case 0x14:
  3948. // offset: 16; size: 2; color index for sheet tab
  3949. $colorIndex = self::_GetInt2d($recordData, 16);
  3950. $color = self::_readColor($colorIndex,$this->_palette,$this->_version);
  3951. $this->_phpSheet->getTabColor()->setRGB($color['rgb']);
  3952. break;
  3953. case 0x28:
  3954. // TODO: Investigate structure for .xls SHEETLAYOUT record as saved by MS Office Excel 2007
  3955. return;
  3956. break;
  3957. }
  3958. }
  3959. }
  3960. /**
  3961. * Read SHEETPROTECTION record (FEATHEADR)
  3962. */
  3963. private function _readSheetProtection()
  3964. {
  3965. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  3966. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  3967. // move stream pointer to next record
  3968. $this->_pos += 4 + $length;
  3969. if ($this->_readDataOnly) {
  3970. return;
  3971. }
  3972. // offset: 0; size: 2; repeated record header
  3973. // offset: 2; size: 2; FRT cell reference flag (=0 currently)
  3974. // offset: 4; size: 8; Currently not used and set to 0
  3975. // offset: 12; size: 2; Shared feature type index (2=Enhanced Protetion, 4=SmartTag)
  3976. $isf = self::_GetInt2d($recordData, 12);
  3977. if ($isf != 2) {
  3978. return;
  3979. }
  3980. // offset: 14; size: 1; =1 since this is a feat header
  3981. // offset: 15; size: 4; size of rgbHdrSData
  3982. // rgbHdrSData, assume "Enhanced Protection"
  3983. // offset: 19; size: 2; option flags
  3984. $options = self::_GetInt2d($recordData, 19);
  3985. // bit: 0; mask 0x0001; 1 = user may edit objects, 0 = users must not edit objects
  3986. $bool = (0x0001 & $options) >> 0;
  3987. $this->_phpSheet->getProtection()->setObjects(!$bool);
  3988. // bit: 1; mask 0x0002; edit scenarios
  3989. $bool = (0x0002 & $options) >> 1;
  3990. $this->_phpSheet->getProtection()->setScenarios(!$bool);
  3991. // bit: 2; mask 0x0004; format cells
  3992. $bool = (0x0004 & $options) >> 2;
  3993. $this->_phpSheet->getProtection()->setFormatCells(!$bool);
  3994. // bit: 3; mask 0x0008; format columns
  3995. $bool = (0x0008 & $options) >> 3;
  3996. $this->_phpSheet->getProtection()->setFormatColumns(!$bool);
  3997. // bit: 4; mask 0x0010; format rows
  3998. $bool = (0x0010 & $options) >> 4;
  3999. $this->_phpSheet->getProtection()->setFormatRows(!$bool);
  4000. // bit: 5; mask 0x0020; insert columns
  4001. $bool = (0x0020 & $options) >> 5;
  4002. $this->_phpSheet->getProtection()->setInsertColumns(!$bool);
  4003. // bit: 6; mask 0x0040; insert rows
  4004. $bool = (0x0040 & $options) >> 6;
  4005. $this->_phpSheet->getProtection()->setInsertRows(!$bool);
  4006. // bit: 7; mask 0x0080; insert hyperlinks
  4007. $bool = (0x0080 & $options) >> 7;
  4008. $this->_phpSheet->getProtection()->setInsertHyperlinks(!$bool);
  4009. // bit: 8; mask 0x0100; delete columns
  4010. $bool = (0x0100 & $options) >> 8;
  4011. $this->_phpSheet->getProtection()->setDeleteColumns(!$bool);
  4012. // bit: 9; mask 0x0200; delete rows
  4013. $bool = (0x0200 & $options) >> 9;
  4014. $this->_phpSheet->getProtection()->setDeleteRows(!$bool);
  4015. // bit: 10; mask 0x0400; select locked cells
  4016. $bool = (0x0400 & $options) >> 10;
  4017. $this->_phpSheet->getProtection()->setSelectLockedCells(!$bool);
  4018. // bit: 11; mask 0x0800; sort cell range
  4019. $bool = (0x0800 & $options) >> 11;
  4020. $this->_phpSheet->getProtection()->setSort(!$bool);
  4021. // bit: 12; mask 0x1000; auto filter
  4022. $bool = (0x1000 & $options) >> 12;
  4023. $this->_phpSheet->getProtection()->setAutoFilter(!$bool);
  4024. // bit: 13; mask 0x2000; pivot tables
  4025. $bool = (0x2000 & $options) >> 13;
  4026. $this->_phpSheet->getProtection()->setPivotTables(!$bool);
  4027. // bit: 14; mask 0x4000; select unlocked cells
  4028. $bool = (0x4000 & $options) >> 14;
  4029. $this->_phpSheet->getProtection()->setSelectUnlockedCells(!$bool);
  4030. // offset: 21; size: 2; not used
  4031. }
  4032. /**
  4033. * Read RANGEPROTECTION record
  4034. * Reading of this record is based on Microsoft Office Excel 97-2000 Binary File Format Specification,
  4035. * where it is referred to as FEAT record
  4036. */
  4037. private function _readRangeProtection()
  4038. {
  4039. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  4040. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  4041. // move stream pointer to next record
  4042. $this->_pos += 4 + $length;
  4043. // local pointer in record data
  4044. $offset = 0;
  4045. if (!$this->_readDataOnly) {
  4046. $offset += 12;
  4047. // offset: 12; size: 2; shared feature type, 2 = enhanced protection, 4 = smart tag
  4048. $isf = self::_GetInt2d($recordData, 12);
  4049. if ($isf != 2) {
  4050. // we only read FEAT records of type 2
  4051. return;
  4052. }
  4053. $offset += 2;
  4054. $offset += 5;
  4055. // offset: 19; size: 2; count of ref ranges this feature is on
  4056. $cref = self::_GetInt2d($recordData, 19);
  4057. $offset += 2;
  4058. $offset += 6;
  4059. // offset: 27; size: 8 * $cref; list of cell ranges (like in hyperlink record)
  4060. $cellRanges = array();
  4061. for ($i = 0; $i < $cref; ++$i) {
  4062. try {
  4063. $cellRange = $this->_readBIFF8CellRangeAddressFixed(substr($recordData, 27 + 8 * $i, 8));
  4064. } catch (PHPExcel_Exception $e) {
  4065. return;
  4066. }
  4067. $cellRanges[] = $cellRange;
  4068. $offset += 8;
  4069. }
  4070. // offset: var; size: var; variable length of feature specific data
  4071. $rgbFeat = substr($recordData, $offset);
  4072. $offset += 4;
  4073. // offset: var; size: 4; the encrypted password (only 16-bit although field is 32-bit)
  4074. $wPassword = self::_GetInt4d($recordData, $offset);
  4075. $offset += 4;
  4076. // Apply range protection to sheet
  4077. if ($cellRanges) {
  4078. $this->_phpSheet->protectCells(implode(' ', $cellRanges), strtoupper(dechex($wPassword)), true);
  4079. }
  4080. }
  4081. }
  4082. /**
  4083. * Read IMDATA record
  4084. */
  4085. private function _readImData()
  4086. {
  4087. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  4088. // get spliced record data
  4089. $splicedRecordData = $this->_getSplicedRecordData();
  4090. $recordData = $splicedRecordData['recordData'];
  4091. // UNDER CONSTRUCTION
  4092. // offset: 0; size: 2; image format
  4093. $cf = self::_GetInt2d($recordData, 0);
  4094. // offset: 2; size: 2; environment from which the file was written
  4095. $env = self::_GetInt2d($recordData, 2);
  4096. // offset: 4; size: 4; length of the image data
  4097. $lcb = self::_GetInt4d($recordData, 4);
  4098. // offset: 8; size: var; image data
  4099. $iData = substr($recordData, 8);
  4100. switch ($cf) {
  4101. case 0x09: // Windows bitmap format
  4102. // BITMAPCOREINFO
  4103. // 1. BITMAPCOREHEADER
  4104. // offset: 0; size: 4; bcSize, Specifies the number of bytes required by the structure
  4105. $bcSize = self::_GetInt4d($iData, 0);
  4106. // var_dump($bcSize);
  4107. // offset: 4; size: 2; bcWidth, specifies the width of the bitmap, in pixels
  4108. $bcWidth = self::_GetInt2d($iData, 4);
  4109. // var_dump($bcWidth);
  4110. // offset: 6; size: 2; bcHeight, specifies the height of the bitmap, in pixels.
  4111. $bcHeight = self::_GetInt2d($iData, 6);
  4112. // var_dump($bcHeight);
  4113. $ih = imagecreatetruecolor($bcWidth, $bcHeight);
  4114. // offset: 8; size: 2; bcPlanes, specifies the number of planes for the target device. This value must be 1
  4115. // offset: 10; size: 2; bcBitCount specifies the number of bits-per-pixel. This value must be 1, 4, 8, or 24
  4116. $bcBitCount = self::_GetInt2d($iData, 10);
  4117. // var_dump($bcBitCount);
  4118. $rgbString = substr($iData, 12);
  4119. $rgbTriples = array();
  4120. while (strlen($rgbString) > 0) {
  4121. $rgbTriples[] = unpack('Cb/Cg/Cr', $rgbString);
  4122. $rgbString = substr($rgbString, 3);
  4123. }
  4124. $x = 0;
  4125. $y = 0;
  4126. foreach ($rgbTriples as $i => $rgbTriple) {
  4127. $color = imagecolorallocate($ih, $rgbTriple['r'], $rgbTriple['g'], $rgbTriple['b']);
  4128. imagesetpixel($ih, $x, $bcHeight - 1 - $y, $color);
  4129. $x = ($x + 1) % $bcWidth;
  4130. $y = $y + floor(($x + 1) / $bcWidth);
  4131. }
  4132. //imagepng($ih, 'image.png');
  4133. $drawing = new PHPExcel_Worksheet_Drawing();
  4134. $drawing->setPath($filename);
  4135. $drawing->setWorksheet($this->_phpSheet);
  4136. break;
  4137. case 0x02: // Windows metafile or Macintosh PICT format
  4138. case 0x0e: // native format
  4139. default;
  4140. break;
  4141. }
  4142. // _getSplicedRecordData() takes care of moving current position in data stream
  4143. }
  4144. /**
  4145. * Read a free CONTINUE record. Free CONTINUE record may be a camouflaged MSODRAWING record
  4146. * When MSODRAWING data on a sheet exceeds 8224 bytes, CONTINUE records are used instead. Undocumented.
  4147. * In this case, we must treat the CONTINUE record as a MSODRAWING record
  4148. */
  4149. private function _readContinue()
  4150. {
  4151. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  4152. $recordData = $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  4153. // check if we are reading drawing data
  4154. // this is in case a free CONTINUE record occurs in other circumstances we are unaware of
  4155. if ($this->_drawingData == '') {
  4156. // move stream pointer to next record
  4157. $this->_pos += 4 + $length;
  4158. return;
  4159. }
  4160. // check if record data is at least 4 bytes long, otherwise there is no chance this is MSODRAWING data
  4161. if ($length < 4) {
  4162. // move stream pointer to next record
  4163. $this->_pos += 4 + $length;
  4164. return;
  4165. }
  4166. // dirty check to see if CONTINUE record could be a camouflaged MSODRAWING record
  4167. // look inside CONTINUE record to see if it looks like a part of an Escher stream
  4168. // we know that Escher stream may be split at least at
  4169. // 0xF003 MsofbtSpgrContainer
  4170. // 0xF004 MsofbtSpContainer
  4171. // 0xF00D MsofbtClientTextbox
  4172. $validSplitPoints = array(0xF003, 0xF004, 0xF00D); // add identifiers if we find more
  4173. $splitPoint = self::_GetInt2d($recordData, 2);
  4174. if (in_array($splitPoint, $validSplitPoints)) {
  4175. // get spliced record data (and move pointer to next record)
  4176. $splicedRecordData = $this->_getSplicedRecordData();
  4177. $this->_drawingData .= $splicedRecordData['recordData'];
  4178. return;
  4179. }
  4180. // move stream pointer to next record
  4181. $this->_pos += 4 + $length;
  4182. }
  4183. /**
  4184. * Reads a record from current position in data stream and continues reading data as long as CONTINUE
  4185. * records are found. Splices the record data pieces and returns the combined string as if record data
  4186. * is in one piece.
  4187. * Moves to next current position in data stream to start of next record different from a CONtINUE record
  4188. *
  4189. * @return array
  4190. */
  4191. private function _getSplicedRecordData()
  4192. {
  4193. $data = '';
  4194. $spliceOffsets = array();
  4195. $i = 0;
  4196. $spliceOffsets[0] = 0;
  4197. do {
  4198. ++$i;
  4199. // offset: 0; size: 2; identifier
  4200. $identifier = self::_GetInt2d($this->_data, $this->_pos);
  4201. // offset: 2; size: 2; length
  4202. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  4203. $data .= $this->_readRecordData($this->_data, $this->_pos + 4, $length);
  4204. $spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
  4205. $this->_pos += 4 + $length;
  4206. $nextIdentifier = self::_GetInt2d($this->_data, $this->_pos);
  4207. }
  4208. while ($nextIdentifier == self::XLS_Type_CONTINUE);
  4209. $splicedData = array(
  4210. 'recordData' => $data,
  4211. 'spliceOffsets' => $spliceOffsets,
  4212. );
  4213. return $splicedData;
  4214. }
  4215. /**
  4216. * Convert formula structure into human readable Excel formula like 'A3+A5*5'
  4217. *
  4218. * @param string $formulaStructure The complete binary data for the formula
  4219. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4220. * @return string Human readable formula
  4221. */
  4222. private function _getFormulaFromStructure($formulaStructure, $baseCell = 'A1')
  4223. {
  4224. // offset: 0; size: 2; size of the following formula data
  4225. $sz = self::_GetInt2d($formulaStructure, 0);
  4226. // offset: 2; size: sz
  4227. $formulaData = substr($formulaStructure, 2, $sz);
  4228. // for debug: dump the formula data
  4229. //echo '<xmp>';
  4230. //echo 'size: ' . $sz . "\n";
  4231. //echo 'the entire formula data: ';
  4232. //Debug::dump($formulaData);
  4233. //echo "\n----\n";
  4234. // offset: 2 + sz; size: variable (optional)
  4235. if (strlen($formulaStructure) > 2 + $sz) {
  4236. $additionalData = substr($formulaStructure, 2 + $sz);
  4237. // for debug: dump the additional data
  4238. //echo 'the entire additional data: ';
  4239. //Debug::dump($additionalData);
  4240. //echo "\n----\n";
  4241. } else {
  4242. $additionalData = '';
  4243. }
  4244. return $this->_getFormulaFromData($formulaData, $additionalData, $baseCell);
  4245. }
  4246. /**
  4247. * Take formula data and additional data for formula and return human readable formula
  4248. *
  4249. * @param string $formulaData The binary data for the formula itself
  4250. * @param string $additionalData Additional binary data going with the formula
  4251. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4252. * @return string Human readable formula
  4253. */
  4254. private function _getFormulaFromData($formulaData, $additionalData = '', $baseCell = 'A1')
  4255. {
  4256. // start parsing the formula data
  4257. $tokens = array();
  4258. while (strlen($formulaData) > 0 and $token = $this->_getNextToken($formulaData, $baseCell)) {
  4259. $tokens[] = $token;
  4260. $formulaData = substr($formulaData, $token['size']);
  4261. // for debug: dump the token
  4262. //var_dump($token);
  4263. }
  4264. $formulaString = $this->_createFormulaFromTokens($tokens, $additionalData);
  4265. return $formulaString;
  4266. }
  4267. /**
  4268. * Take array of tokens together with additional data for formula and return human readable formula
  4269. *
  4270. * @param array $tokens
  4271. * @param array $additionalData Additional binary data going with the formula
  4272. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4273. * @return string Human readable formula
  4274. */
  4275. private function _createFormulaFromTokens($tokens, $additionalData)
  4276. {
  4277. // empty formula?
  4278. if (empty($tokens)) {
  4279. return '';
  4280. }
  4281. $formulaStrings = array();
  4282. foreach ($tokens as $token) {
  4283. // initialize spaces
  4284. $space0 = isset($space0) ? $space0 : ''; // spaces before next token, not tParen
  4285. $space1 = isset($space1) ? $space1 : ''; // carriage returns before next token, not tParen
  4286. $space2 = isset($space2) ? $space2 : ''; // spaces before opening parenthesis
  4287. $space3 = isset($space3) ? $space3 : ''; // carriage returns before opening parenthesis
  4288. $space4 = isset($space4) ? $space4 : ''; // spaces before closing parenthesis
  4289. $space5 = isset($space5) ? $space5 : ''; // carriage returns before closing parenthesis
  4290. switch ($token['name']) {
  4291. case 'tAdd': // addition
  4292. case 'tConcat': // addition
  4293. case 'tDiv': // division
  4294. case 'tEQ': // equality
  4295. case 'tGE': // greater than or equal
  4296. case 'tGT': // greater than
  4297. case 'tIsect': // intersection
  4298. case 'tLE': // less than or equal
  4299. case 'tList': // less than or equal
  4300. case 'tLT': // less than
  4301. case 'tMul': // multiplication
  4302. case 'tNE': // multiplication
  4303. case 'tPower': // power
  4304. case 'tRange': // range
  4305. case 'tSub': // subtraction
  4306. $op2 = array_pop($formulaStrings);
  4307. $op1 = array_pop($formulaStrings);
  4308. $formulaStrings[] = "$op1$space1$space0{$token['data']}$op2";
  4309. unset($space0, $space1);
  4310. break;
  4311. case 'tUplus': // unary plus
  4312. case 'tUminus': // unary minus
  4313. $op = array_pop($formulaStrings);
  4314. $formulaStrings[] = "$space1$space0{$token['data']}$op";
  4315. unset($space0, $space1);
  4316. break;
  4317. case 'tPercent': // percent sign
  4318. $op = array_pop($formulaStrings);
  4319. $formulaStrings[] = "$op$space1$space0{$token['data']}";
  4320. unset($space0, $space1);
  4321. break;
  4322. case 'tAttrVolatile': // indicates volatile function
  4323. case 'tAttrIf':
  4324. case 'tAttrSkip':
  4325. case 'tAttrChoose':
  4326. // token is only important for Excel formula evaluator
  4327. // do nothing
  4328. break;
  4329. case 'tAttrSpace': // space / carriage return
  4330. // space will be used when next token arrives, do not alter formulaString stack
  4331. switch ($token['data']['spacetype']) {
  4332. case 'type0':
  4333. $space0 = str_repeat(' ', $token['data']['spacecount']);
  4334. break;
  4335. case 'type1':
  4336. $space1 = str_repeat("\n", $token['data']['spacecount']);
  4337. break;
  4338. case 'type2':
  4339. $space2 = str_repeat(' ', $token['data']['spacecount']);
  4340. break;
  4341. case 'type3':
  4342. $space3 = str_repeat("\n", $token['data']['spacecount']);
  4343. break;
  4344. case 'type4':
  4345. $space4 = str_repeat(' ', $token['data']['spacecount']);
  4346. break;
  4347. case 'type5':
  4348. $space5 = str_repeat("\n", $token['data']['spacecount']);
  4349. break;
  4350. }
  4351. break;
  4352. case 'tAttrSum': // SUM function with one parameter
  4353. $op = array_pop($formulaStrings);
  4354. $formulaStrings[] = "{$space1}{$space0}SUM($op)";
  4355. unset($space0, $space1);
  4356. break;
  4357. case 'tFunc': // function with fixed number of arguments
  4358. case 'tFuncV': // function with variable number of arguments
  4359. if ($token['data']['function'] != '') {
  4360. // normal function
  4361. $ops = array(); // array of operators
  4362. for ($i = 0; $i < $token['data']['args']; ++$i) {
  4363. $ops[] = array_pop($formulaStrings);
  4364. }
  4365. $ops = array_reverse($ops);
  4366. $formulaStrings[] = "$space1$space0{$token['data']['function']}(" . implode(',', $ops) . ")";
  4367. unset($space0, $space1);
  4368. } else {
  4369. // add-in function
  4370. $ops = array(); // array of operators
  4371. for ($i = 0; $i < $token['data']['args'] - 1; ++$i) {
  4372. $ops[] = array_pop($formulaStrings);
  4373. }
  4374. $ops = array_reverse($ops);
  4375. $function = array_pop($formulaStrings);
  4376. $formulaStrings[] = "$space1$space0$function(" . implode(',', $ops) . ")";
  4377. unset($space0, $space1);
  4378. }
  4379. break;
  4380. case 'tParen': // parenthesis
  4381. $expression = array_pop($formulaStrings);
  4382. $formulaStrings[] = "$space3$space2($expression$space5$space4)";
  4383. unset($space2, $space3, $space4, $space5);
  4384. break;
  4385. case 'tArray': // array constant
  4386. $constantArray = self::_readBIFF8ConstantArray($additionalData);
  4387. $formulaStrings[] = $space1 . $space0 . $constantArray['value'];
  4388. $additionalData = substr($additionalData, $constantArray['size']); // bite of chunk of additional data
  4389. unset($space0, $space1);
  4390. break;
  4391. case 'tMemArea':
  4392. // bite off chunk of additional data
  4393. $cellRangeAddressList = $this->_readBIFF8CellRangeAddressList($additionalData);
  4394. $additionalData = substr($additionalData, $cellRangeAddressList['size']);
  4395. $formulaStrings[] = "$space1$space0{$token['data']}";
  4396. unset($space0, $space1);
  4397. break;
  4398. case 'tArea': // cell range address
  4399. case 'tBool': // boolean
  4400. case 'tErr': // error code
  4401. case 'tInt': // integer
  4402. case 'tMemErr':
  4403. case 'tMemFunc':
  4404. case 'tMissArg':
  4405. case 'tName':
  4406. case 'tNameX':
  4407. case 'tNum': // number
  4408. case 'tRef': // single cell reference
  4409. case 'tRef3d': // 3d cell reference
  4410. case 'tArea3d': // 3d cell range reference
  4411. case 'tRefN':
  4412. case 'tAreaN':
  4413. case 'tStr': // string
  4414. $formulaStrings[] = "$space1$space0{$token['data']}";
  4415. unset($space0, $space1);
  4416. break;
  4417. }
  4418. }
  4419. $formulaString = $formulaStrings[0];
  4420. // for debug: dump the human readable formula
  4421. //echo '----' . "\n";
  4422. //echo 'Formula: ' . $formulaString;
  4423. return $formulaString;
  4424. }
  4425. /**
  4426. * Fetch next token from binary formula data
  4427. *
  4428. * @param string Formula data
  4429. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  4430. * @return array
  4431. * @throws PHPExcel_Reader_Exception
  4432. */
  4433. private function _getNextToken($formulaData, $baseCell = 'A1')
  4434. {
  4435. // offset: 0; size: 1; token id
  4436. $id = ord($formulaData[0]); // token id
  4437. $name = false; // initialize token name
  4438. switch ($id) {
  4439. case 0x03: $name = 'tAdd'; $size = 1; $data = '+'; break;
  4440. case 0x04: $name = 'tSub'; $size = 1; $data = '-'; break;
  4441. case 0x05: $name = 'tMul'; $size = 1; $data = '*'; break;
  4442. case 0x06: $name = 'tDiv'; $size = 1; $data = '/'; break;
  4443. case 0x07: $name = 'tPower'; $size = 1; $data = '^'; break;
  4444. case 0x08: $name = 'tConcat'; $size = 1; $data = '&'; break;
  4445. case 0x09: $name = 'tLT'; $size = 1; $data = '<'; break;
  4446. case 0x0A: $name = 'tLE'; $size = 1; $data = '<='; break;
  4447. case 0x0B: $name = 'tEQ'; $size = 1; $data = '='; break;
  4448. case 0x0C: $name = 'tGE'; $size = 1; $data = '>='; break;
  4449. case 0x0D: $name = 'tGT'; $size = 1; $data = '>'; break;
  4450. case 0x0E: $name = 'tNE'; $size = 1; $data = '<>'; break;
  4451. case 0x0F: $name = 'tIsect'; $size = 1; $data = ' '; break;
  4452. case 0x10: $name = 'tList'; $size = 1; $data = ','; break;
  4453. case 0x11: $name = 'tRange'; $size = 1; $data = ':'; break;
  4454. case 0x12: $name = 'tUplus'; $size = 1; $data = '+'; break;
  4455. case 0x13: $name = 'tUminus'; $size = 1; $data = '-'; break;
  4456. case 0x14: $name = 'tPercent'; $size = 1; $data = '%'; break;
  4457. case 0x15: // parenthesis
  4458. $name = 'tParen';
  4459. $size = 1;
  4460. $data = null;
  4461. break;
  4462. case 0x16: // missing argument
  4463. $name = 'tMissArg';
  4464. $size = 1;
  4465. $data = '';
  4466. break;
  4467. case 0x17: // string
  4468. $name = 'tStr';
  4469. // offset: 1; size: var; Unicode string, 8-bit string length
  4470. $string = self::_readUnicodeStringShort(substr($formulaData, 1));
  4471. $size = 1 + $string['size'];
  4472. $data = self::_UTF8toExcelDoubleQuoted($string['value']);
  4473. break;
  4474. case 0x19: // Special attribute
  4475. // offset: 1; size: 1; attribute type flags:
  4476. switch (ord($formulaData[1])) {
  4477. case 0x01:
  4478. $name = 'tAttrVolatile';
  4479. $size = 4;
  4480. $data = null;
  4481. break;
  4482. case 0x02:
  4483. $name = 'tAttrIf';
  4484. $size = 4;
  4485. $data = null;
  4486. break;
  4487. case 0x04:
  4488. $name = 'tAttrChoose';
  4489. // offset: 2; size: 2; number of choices in the CHOOSE function ($nc, number of parameters decreased by 1)
  4490. $nc = self::_GetInt2d($formulaData, 2);
  4491. // offset: 4; size: 2 * $nc
  4492. // offset: 4 + 2 * $nc; size: 2
  4493. $size = 2 * $nc + 6;
  4494. $data = null;
  4495. break;
  4496. case 0x08:
  4497. $name = 'tAttrSkip';
  4498. $size = 4;
  4499. $data = null;
  4500. break;
  4501. case 0x10:
  4502. $name = 'tAttrSum';
  4503. $size = 4;
  4504. $data = null;
  4505. break;
  4506. case 0x40:
  4507. case 0x41:
  4508. $name = 'tAttrSpace';
  4509. $size = 4;
  4510. // offset: 2; size: 2; space type and position
  4511. switch (ord($formulaData[2])) {
  4512. case 0x00:
  4513. $spacetype = 'type0';
  4514. break;
  4515. case 0x01:
  4516. $spacetype = 'type1';
  4517. break;
  4518. case 0x02:
  4519. $spacetype = 'type2';
  4520. break;
  4521. case 0x03:
  4522. $spacetype = 'type3';
  4523. break;
  4524. case 0x04:
  4525. $spacetype = 'type4';
  4526. break;
  4527. case 0x05:
  4528. $spacetype = 'type5';
  4529. break;
  4530. default:
  4531. throw new PHPExcel_Reader_Exception('Unrecognized space type in tAttrSpace token');
  4532. break;
  4533. }
  4534. // offset: 3; size: 1; number of inserted spaces/carriage returns
  4535. $spacecount = ord($formulaData[3]);
  4536. $data = array('spacetype' => $spacetype, 'spacecount' => $spacecount);
  4537. break;
  4538. default:
  4539. throw new PHPExcel_Reader_Exception('Unrecognized attribute flag in tAttr token');
  4540. break;
  4541. }
  4542. break;
  4543. case 0x1C: // error code
  4544. // offset: 1; size: 1; error code
  4545. $name = 'tErr';
  4546. $size = 2;
  4547. $data = self::_mapErrorCode(ord($formulaData[1]));
  4548. break;
  4549. case 0x1D: // boolean
  4550. // offset: 1; size: 1; 0 = false, 1 = true;
  4551. $name = 'tBool';
  4552. $size = 2;
  4553. $data = ord($formulaData[1]) ? 'TRUE' : 'FALSE';
  4554. break;
  4555. case 0x1E: // integer
  4556. // offset: 1; size: 2; unsigned 16-bit integer
  4557. $name = 'tInt';
  4558. $size = 3;
  4559. $data = self::_GetInt2d($formulaData, 1);
  4560. break;
  4561. case 0x1F: // number
  4562. // offset: 1; size: 8;
  4563. $name = 'tNum';
  4564. $size = 9;
  4565. $data = self::_extractNumber(substr($formulaData, 1));
  4566. $data = str_replace(',', '.', (string)$data); // in case non-English locale
  4567. break;
  4568. case 0x20: // array constant
  4569. case 0x40:
  4570. case 0x60:
  4571. // offset: 1; size: 7; not used
  4572. $name = 'tArray';
  4573. $size = 8;
  4574. $data = null;
  4575. break;
  4576. case 0x21: // function with fixed number of arguments
  4577. case 0x41:
  4578. case 0x61:
  4579. $name = 'tFunc';
  4580. $size = 3;
  4581. // offset: 1; size: 2; index to built-in sheet function
  4582. switch (self::_GetInt2d($formulaData, 1)) {
  4583. case 2: $function = 'ISNA'; $args = 1; break;
  4584. case 3: $function = 'ISERROR'; $args = 1; break;
  4585. case 10: $function = 'NA'; $args = 0; break;
  4586. case 15: $function = 'SIN'; $args = 1; break;
  4587. case 16: $function = 'COS'; $args = 1; break;
  4588. case 17: $function = 'TAN'; $args = 1; break;
  4589. case 18: $function = 'ATAN'; $args = 1; break;
  4590. case 19: $function = 'PI'; $args = 0; break;
  4591. case 20: $function = 'SQRT'; $args = 1; break;
  4592. case 21: $function = 'EXP'; $args = 1; break;
  4593. case 22: $function = 'LN'; $args = 1; break;
  4594. case 23: $function = 'LOG10'; $args = 1; break;
  4595. case 24: $function = 'ABS'; $args = 1; break;
  4596. case 25: $function = 'INT'; $args = 1; break;
  4597. case 26: $function = 'SIGN'; $args = 1; break;
  4598. case 27: $function = 'ROUND'; $args = 2; break;
  4599. case 30: $function = 'REPT'; $args = 2; break;
  4600. case 31: $function = 'MID'; $args = 3; break;
  4601. case 32: $function = 'LEN'; $args = 1; break;
  4602. case 33: $function = 'VALUE'; $args = 1; break;
  4603. case 34: $function = 'TRUE'; $args = 0; break;
  4604. case 35: $function = 'FALSE'; $args = 0; break;
  4605. case 38: $function = 'NOT'; $args = 1; break;
  4606. case 39: $function = 'MOD'; $args = 2; break;
  4607. case 40: $function = 'DCOUNT'; $args = 3; break;
  4608. case 41: $function = 'DSUM'; $args = 3; break;
  4609. case 42: $function = 'DAVERAGE'; $args = 3; break;
  4610. case 43: $function = 'DMIN'; $args = 3; break;
  4611. case 44: $function = 'DMAX'; $args = 3; break;
  4612. case 45: $function = 'DSTDEV'; $args = 3; break;
  4613. case 48: $function = 'TEXT'; $args = 2; break;
  4614. case 61: $function = 'MIRR'; $args = 3; break;
  4615. case 63: $function = 'RAND'; $args = 0; break;
  4616. case 65: $function = 'DATE'; $args = 3; break;
  4617. case 66: $function = 'TIME'; $args = 3; break;
  4618. case 67: $function = 'DAY'; $args = 1; break;
  4619. case 68: $function = 'MONTH'; $args = 1; break;
  4620. case 69: $function = 'YEAR'; $args = 1; break;
  4621. case 71: $function = 'HOUR'; $args = 1; break;
  4622. case 72: $function = 'MINUTE'; $args = 1; break;
  4623. case 73: $function = 'SECOND'; $args = 1; break;
  4624. case 74: $function = 'NOW'; $args = 0; break;
  4625. case 75: $function = 'AREAS'; $args = 1; break;
  4626. case 76: $function = 'ROWS'; $args = 1; break;
  4627. case 77: $function = 'COLUMNS'; $args = 1; break;
  4628. case 83: $function = 'TRANSPOSE'; $args = 1; break;
  4629. case 86: $function = 'TYPE'; $args = 1; break;
  4630. case 97: $function = 'ATAN2'; $args = 2; break;
  4631. case 98: $function = 'ASIN'; $args = 1; break;
  4632. case 99: $function = 'ACOS'; $args = 1; break;
  4633. case 105: $function = 'ISREF'; $args = 1; break;
  4634. case 111: $function = 'CHAR'; $args = 1; break;
  4635. case 112: $function = 'LOWER'; $args = 1; break;
  4636. case 113: $function = 'UPPER'; $args = 1; break;
  4637. case 114: $function = 'PROPER'; $args = 1; break;
  4638. case 117: $function = 'EXACT'; $args = 2; break;
  4639. case 118: $function = 'TRIM'; $args = 1; break;
  4640. case 119: $function = 'REPLACE'; $args = 4; break;
  4641. case 121: $function = 'CODE'; $args = 1; break;
  4642. case 126: $function = 'ISERR'; $args = 1; break;
  4643. case 127: $function = 'ISTEXT'; $args = 1; break;
  4644. case 128: $function = 'ISNUMBER'; $args = 1; break;
  4645. case 129: $function = 'ISBLANK'; $args = 1; break;
  4646. case 130: $function = 'T'; $args = 1; break;
  4647. case 131: $function = 'N'; $args = 1; break;
  4648. case 140: $function = 'DATEVALUE'; $args = 1; break;
  4649. case 141: $function = 'TIMEVALUE'; $args = 1; break;
  4650. case 142: $function = 'SLN'; $args = 3; break;
  4651. case 143: $function = 'SYD'; $args = 4; break;
  4652. case 162: $function = 'CLEAN'; $args = 1; break;
  4653. case 163: $function = 'MDETERM'; $args = 1; break;
  4654. case 164: $function = 'MINVERSE'; $args = 1; break;
  4655. case 165: $function = 'MMULT'; $args = 2; break;
  4656. case 184: $function = 'FACT'; $args = 1; break;
  4657. case 189: $function = 'DPRODUCT'; $args = 3; break;
  4658. case 190: $function = 'ISNONTEXT'; $args = 1; break;
  4659. case 195: $function = 'DSTDEVP'; $args = 3; break;
  4660. case 196: $function = 'DVARP'; $args = 3; break;
  4661. case 198: $function = 'ISLOGICAL'; $args = 1; break;
  4662. case 199: $function = 'DCOUNTA'; $args = 3; break;
  4663. case 207: $function = 'REPLACEB'; $args = 4; break;
  4664. case 210: $function = 'MIDB'; $args = 3; break;
  4665. case 211: $function = 'LENB'; $args = 1; break;
  4666. case 212: $function = 'ROUNDUP'; $args = 2; break;
  4667. case 213: $function = 'ROUNDDOWN'; $args = 2; break;
  4668. case 214: $function = 'ASC'; $args = 1; break;
  4669. case 215: $function = 'DBCS'; $args = 1; break;
  4670. case 221: $function = 'TODAY'; $args = 0; break;
  4671. case 229: $function = 'SINH'; $args = 1; break;
  4672. case 230: $function = 'COSH'; $args = 1; break;
  4673. case 231: $function = 'TANH'; $args = 1; break;
  4674. case 232: $function = 'ASINH'; $args = 1; break;
  4675. case 233: $function = 'ACOSH'; $args = 1; break;
  4676. case 234: $function = 'ATANH'; $args = 1; break;
  4677. case 235: $function = 'DGET'; $args = 3; break;
  4678. case 244: $function = 'INFO'; $args = 1; break;
  4679. case 252: $function = 'FREQUENCY'; $args = 2; break;
  4680. case 261: $function = 'ERROR.TYPE'; $args = 1; break;
  4681. case 271: $function = 'GAMMALN'; $args = 1; break;
  4682. case 273: $function = 'BINOMDIST'; $args = 4; break;
  4683. case 274: $function = 'CHIDIST'; $args = 2; break;
  4684. case 275: $function = 'CHIINV'; $args = 2; break;
  4685. case 276: $function = 'COMBIN'; $args = 2; break;
  4686. case 277: $function = 'CONFIDENCE'; $args = 3; break;
  4687. case 278: $function = 'CRITBINOM'; $args = 3; break;
  4688. case 279: $function = 'EVEN'; $args = 1; break;
  4689. case 280: $function = 'EXPONDIST'; $args = 3; break;
  4690. case 281: $function = 'FDIST'; $args = 3; break;
  4691. case 282: $function = 'FINV'; $args = 3; break;
  4692. case 283: $function = 'FISHER'; $args = 1; break;
  4693. case 284: $function = 'FISHERINV'; $args = 1; break;
  4694. case 285: $function = 'FLOOR'; $args = 2; break;
  4695. case 286: $function = 'GAMMADIST'; $args = 4; break;
  4696. case 287: $function = 'GAMMAINV'; $args = 3; break;
  4697. case 288: $function = 'CEILING'; $args = 2; break;
  4698. case 289: $function = 'HYPGEOMDIST'; $args = 4; break;
  4699. case 290: $function = 'LOGNORMDIST'; $args = 3; break;
  4700. case 291: $function = 'LOGINV'; $args = 3; break;
  4701. case 292: $function = 'NEGBINOMDIST'; $args = 3; break;
  4702. case 293: $function = 'NORMDIST'; $args = 4; break;
  4703. case 294: $function = 'NORMSDIST'; $args = 1; break;
  4704. case 295: $function = 'NORMINV'; $args = 3; break;
  4705. case 296: $function = 'NORMSINV'; $args = 1; break;
  4706. case 297: $function = 'STANDARDIZE'; $args = 3; break;
  4707. case 298: $function = 'ODD'; $args = 1; break;
  4708. case 299: $function = 'PERMUT'; $args = 2; break;
  4709. case 300: $function = 'POISSON'; $args = 3; break;
  4710. case 301: $function = 'TDIST'; $args = 3; break;
  4711. case 302: $function = 'WEIBULL'; $args = 4; break;
  4712. case 303: $function = 'SUMXMY2'; $args = 2; break;
  4713. case 304: $function = 'SUMX2MY2'; $args = 2; break;
  4714. case 305: $function = 'SUMX2PY2'; $args = 2; break;
  4715. case 306: $function = 'CHITEST'; $args = 2; break;
  4716. case 307: $function = 'CORREL'; $args = 2; break;
  4717. case 308: $function = 'COVAR'; $args = 2; break;
  4718. case 309: $function = 'FORECAST'; $args = 3; break;
  4719. case 310: $function = 'FTEST'; $args = 2; break;
  4720. case 311: $function = 'INTERCEPT'; $args = 2; break;
  4721. case 312: $function = 'PEARSON'; $args = 2; break;
  4722. case 313: $function = 'RSQ'; $args = 2; break;
  4723. case 314: $function = 'STEYX'; $args = 2; break;
  4724. case 315: $function = 'SLOPE'; $args = 2; break;
  4725. case 316: $function = 'TTEST'; $args = 4; break;
  4726. case 325: $function = 'LARGE'; $args = 2; break;
  4727. case 326: $function = 'SMALL'; $args = 2; break;
  4728. case 327: $function = 'QUARTILE'; $args = 2; break;
  4729. case 328: $function = 'PERCENTILE'; $args = 2; break;
  4730. case 331: $function = 'TRIMMEAN'; $args = 2; break;
  4731. case 332: $function = 'TINV'; $args = 2; break;
  4732. case 337: $function = 'POWER'; $args = 2; break;
  4733. case 342: $function = 'RADIANS'; $args = 1; break;
  4734. case 343: $function = 'DEGREES'; $args = 1; break;
  4735. case 346: $function = 'COUNTIF'; $args = 2; break;
  4736. case 347: $function = 'COUNTBLANK'; $args = 1; break;
  4737. case 350: $function = 'ISPMT'; $args = 4; break;
  4738. case 351: $function = 'DATEDIF'; $args = 3; break;
  4739. case 352: $function = 'DATESTRING'; $args = 1; break;
  4740. case 353: $function = 'NUMBERSTRING'; $args = 2; break;
  4741. case 360: $function = 'PHONETIC'; $args = 1; break;
  4742. case 368: $function = 'BAHTTEXT'; $args = 1; break;
  4743. default:
  4744. throw new PHPExcel_Reader_Exception('Unrecognized function in formula');
  4745. break;
  4746. }
  4747. $data = array('function' => $function, 'args' => $args);
  4748. break;
  4749. case 0x22: // function with variable number of arguments
  4750. case 0x42:
  4751. case 0x62:
  4752. $name = 'tFuncV';
  4753. $size = 4;
  4754. // offset: 1; size: 1; number of arguments
  4755. $args = ord($formulaData[1]);
  4756. // offset: 2: size: 2; index to built-in sheet function
  4757. $index = self::_GetInt2d($formulaData, 2);
  4758. switch ($index) {
  4759. case 0: $function = 'COUNT'; break;
  4760. case 1: $function = 'IF'; break;
  4761. case 4: $function = 'SUM'; break;
  4762. case 5: $function = 'AVERAGE'; break;
  4763. case 6: $function = 'MIN'; break;
  4764. case 7: $function = 'MAX'; break;
  4765. case 8: $function = 'ROW'; break;
  4766. case 9: $function = 'COLUMN'; break;
  4767. case 11: $function = 'NPV'; break;
  4768. case 12: $function = 'STDEV'; break;
  4769. case 13: $function = 'DOLLAR'; break;
  4770. case 14: $function = 'FIXED'; break;
  4771. case 28: $function = 'LOOKUP'; break;
  4772. case 29: $function = 'INDEX'; break;
  4773. case 36: $function = 'AND'; break;
  4774. case 37: $function = 'OR'; break;
  4775. case 46: $function = 'VAR'; break;
  4776. case 49: $function = 'LINEST'; break;
  4777. case 50: $function = 'TREND'; break;
  4778. case 51: $function = 'LOGEST'; break;
  4779. case 52: $function = 'GROWTH'; break;
  4780. case 56: $function = 'PV'; break;
  4781. case 57: $function = 'FV'; break;
  4782. case 58: $function = 'NPER'; break;
  4783. case 59: $function = 'PMT'; break;
  4784. case 60: $function = 'RATE'; break;
  4785. case 62: $function = 'IRR'; break;
  4786. case 64: $function = 'MATCH'; break;
  4787. case 70: $function = 'WEEKDAY'; break;
  4788. case 78: $function = 'OFFSET'; break;
  4789. case 82: $function = 'SEARCH'; break;
  4790. case 100: $function = 'CHOOSE'; break;
  4791. case 101: $function = 'HLOOKUP'; break;
  4792. case 102: $function = 'VLOOKUP'; break;
  4793. case 109: $function = 'LOG'; break;
  4794. case 115: $function = 'LEFT'; break;
  4795. case 116: $function = 'RIGHT'; break;
  4796. case 120: $function = 'SUBSTITUTE'; break;
  4797. case 124: $function = 'FIND'; break;
  4798. case 125: $function = 'CELL'; break;
  4799. case 144: $function = 'DDB'; break;
  4800. case 148: $function = 'INDIRECT'; break;
  4801. case 167: $function = 'IPMT'; break;
  4802. case 168: $function = 'PPMT'; break;
  4803. case 169: $function = 'COUNTA'; break;
  4804. case 183: $function = 'PRODUCT'; break;
  4805. case 193: $function = 'STDEVP'; break;
  4806. case 194: $function = 'VARP'; break;
  4807. case 197: $function = 'TRUNC'; break;
  4808. case 204: $function = 'USDOLLAR'; break;
  4809. case 205: $function = 'FINDB'; break;
  4810. case 206: $function = 'SEARCHB'; break;
  4811. case 208: $function = 'LEFTB'; break;
  4812. case 209: $function = 'RIGHTB'; break;
  4813. case 216: $function = 'RANK'; break;
  4814. case 219: $function = 'ADDRESS'; break;
  4815. case 220: $function = 'DAYS360'; break;
  4816. case 222: $function = 'VDB'; break;
  4817. case 227: $function = 'MEDIAN'; break;
  4818. case 228: $function = 'SUMPRODUCT'; break;
  4819. case 247: $function = 'DB'; break;
  4820. case 255: $function = ''; break;
  4821. case 269: $function = 'AVEDEV'; break;
  4822. case 270: $function = 'BETADIST'; break;
  4823. case 272: $function = 'BETAINV'; break;
  4824. case 317: $function = 'PROB'; break;
  4825. case 318: $function = 'DEVSQ'; break;
  4826. case 319: $function = 'GEOMEAN'; break;
  4827. case 320: $function = 'HARMEAN'; break;
  4828. case 321: $function = 'SUMSQ'; break;
  4829. case 322: $function = 'KURT'; break;
  4830. case 323: $function = 'SKEW'; break;
  4831. case 324: $function = 'ZTEST'; break;
  4832. case 329: $function = 'PERCENTRANK'; break;
  4833. case 330: $function = 'MODE'; break;
  4834. case 336: $function = 'CONCATENATE'; break;
  4835. case 344: $function = 'SUBTOTAL'; break;
  4836. case 345: $function = 'SUMIF'; break;
  4837. case 354: $function = 'ROMAN'; break;
  4838. case 358: $function = 'GETPIVOTDATA'; break;
  4839. case 359: $function = 'HYPERLINK'; break;
  4840. case 361: $function = 'AVERAGEA'; break;
  4841. case 362: $function = 'MAXA'; break;
  4842. case 363: $function = 'MINA'; break;
  4843. case 364: $function = 'STDEVPA'; break;
  4844. case 365: $function = 'VARPA'; break;
  4845. case 366: $function = 'STDEVA'; break;
  4846. case 367: $function = 'VARA'; break;
  4847. default:
  4848. throw new PHPExcel_Reader_Exception('Unrecognized function in formula');
  4849. break;
  4850. }
  4851. $data = array('function' => $function, 'args' => $args);
  4852. break;
  4853. case 0x23: // index to defined name
  4854. case 0x43:
  4855. case 0x63:
  4856. $name = 'tName';
  4857. $size = 5;
  4858. // offset: 1; size: 2; one-based index to definedname record
  4859. $definedNameIndex = self::_GetInt2d($formulaData, 1) - 1;
  4860. // offset: 2; size: 2; not used
  4861. $data = $this->_definedname[$definedNameIndex]['name'];
  4862. break;
  4863. case 0x24: // single cell reference e.g. A5
  4864. case 0x44:
  4865. case 0x64:
  4866. $name = 'tRef';
  4867. $size = 5;
  4868. $data = $this->_readBIFF8CellAddress(substr($formulaData, 1, 4));
  4869. break;
  4870. case 0x25: // cell range reference to cells in the same sheet (2d)
  4871. case 0x45:
  4872. case 0x65:
  4873. $name = 'tArea';
  4874. $size = 9;
  4875. $data = $this->_readBIFF8CellRangeAddress(substr($formulaData, 1, 8));
  4876. break;
  4877. case 0x26: // Constant reference sub-expression
  4878. case 0x46:
  4879. case 0x66:
  4880. $name = 'tMemArea';
  4881. // offset: 1; size: 4; not used
  4882. // offset: 5; size: 2; size of the following subexpression
  4883. $subSize = self::_GetInt2d($formulaData, 5);
  4884. $size = 7 + $subSize;
  4885. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  4886. break;
  4887. case 0x27: // Deleted constant reference sub-expression
  4888. case 0x47:
  4889. case 0x67:
  4890. $name = 'tMemErr';
  4891. // offset: 1; size: 4; not used
  4892. // offset: 5; size: 2; size of the following subexpression
  4893. $subSize = self::_GetInt2d($formulaData, 5);
  4894. $size = 7 + $subSize;
  4895. $data = $this->_getFormulaFromData(substr($formulaData, 7, $subSize));
  4896. break;
  4897. case 0x29: // Variable reference sub-expression
  4898. case 0x49:
  4899. case 0x69:
  4900. $name = 'tMemFunc';
  4901. // offset: 1; size: 2; size of the following sub-expression
  4902. $subSize = self::_GetInt2d($formulaData, 1);
  4903. $size = 3 + $subSize;
  4904. $data = $this->_getFormulaFromData(substr($formulaData, 3, $subSize));
  4905. break;
  4906. case 0x2C: // Relative 2d cell reference reference, used in shared formulas and some other places
  4907. case 0x4C:
  4908. case 0x6C:
  4909. $name = 'tRefN';
  4910. $size = 5;
  4911. $data = $this->_readBIFF8CellAddressB(substr($formulaData, 1, 4), $baseCell);
  4912. break;
  4913. case 0x2D: // Relative 2d range reference
  4914. case 0x4D:
  4915. case 0x6D:
  4916. $name = 'tAreaN';
  4917. $size = 9;
  4918. $data = $this->_readBIFF8CellRangeAddressB(substr($formulaData, 1, 8), $baseCell);
  4919. break;
  4920. case 0x39: // External name
  4921. case 0x59:
  4922. case 0x79:
  4923. $name = 'tNameX';
  4924. $size = 7;
  4925. // offset: 1; size: 2; index to REF entry in EXTERNSHEET record
  4926. // offset: 3; size: 2; one-based index to DEFINEDNAME or EXTERNNAME record
  4927. $index = self::_GetInt2d($formulaData, 3);
  4928. // assume index is to EXTERNNAME record
  4929. $data = $this->_externalNames[$index - 1]['name'];
  4930. // offset: 5; size: 2; not used
  4931. break;
  4932. case 0x3A: // 3d reference to cell
  4933. case 0x5A:
  4934. case 0x7A:
  4935. $name = 'tRef3d';
  4936. $size = 7;
  4937. try {
  4938. // offset: 1; size: 2; index to REF entry
  4939. $sheetRange = $this->_readSheetRangeByRefIndex(self::_GetInt2d($formulaData, 1));
  4940. // offset: 3; size: 4; cell address
  4941. $cellAddress = $this->_readBIFF8CellAddress(substr($formulaData, 3, 4));
  4942. $data = "$sheetRange!$cellAddress";
  4943. } catch (PHPExcel_Exception $e) {
  4944. // deleted sheet reference
  4945. $data = '#REF!';
  4946. }
  4947. break;
  4948. case 0x3B: // 3d reference to cell range
  4949. case 0x5B:
  4950. case 0x7B:
  4951. $name = 'tArea3d';
  4952. $size = 11;
  4953. try {
  4954. // offset: 1; size: 2; index to REF entry
  4955. $sheetRange = $this->_readSheetRangeByRefIndex(self::_GetInt2d($formulaData, 1));
  4956. // offset: 3; size: 8; cell address
  4957. $cellRangeAddress = $this->_readBIFF8CellRangeAddress(substr($formulaData, 3, 8));
  4958. $data = "$sheetRange!$cellRangeAddress";
  4959. } catch (PHPExcel_Exception $e) {
  4960. // deleted sheet reference
  4961. $data = '#REF!';
  4962. }
  4963. break;
  4964. // Unknown cases // don't know how to deal with
  4965. default:
  4966. throw new PHPExcel_Reader_Exception('Unrecognized token ' . sprintf('%02X', $id) . ' in formula');
  4967. break;
  4968. }
  4969. return array(
  4970. 'id' => $id,
  4971. 'name' => $name,
  4972. 'size' => $size,
  4973. 'data' => $data,
  4974. );
  4975. }
  4976. /**
  4977. * Reads a cell address in BIFF8 e.g. 'A2' or '$A$2'
  4978. * section 3.3.4
  4979. *
  4980. * @param string $cellAddressStructure
  4981. * @return string
  4982. */
  4983. private function _readBIFF8CellAddress($cellAddressStructure)
  4984. {
  4985. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  4986. $row = self::_GetInt2d($cellAddressStructure, 0) + 1;
  4987. // offset: 2; size: 2; index to column or column offset + relative flags
  4988. // bit: 7-0; mask 0x00FF; column index
  4989. $column = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($cellAddressStructure, 2));
  4990. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  4991. if (!(0x4000 & self::_GetInt2d($cellAddressStructure, 2))) {
  4992. $column = '$' . $column;
  4993. }
  4994. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  4995. if (!(0x8000 & self::_GetInt2d($cellAddressStructure, 2))) {
  4996. $row = '$' . $row;
  4997. }
  4998. return $column . $row;
  4999. }
  5000. /**
  5001. * Reads a cell address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  5002. * to indicate offsets from a base cell
  5003. * section 3.3.4
  5004. *
  5005. * @param string $cellAddressStructure
  5006. * @param string $baseCell Base cell, only needed when formula contains tRefN tokens, e.g. with shared formulas
  5007. * @return string
  5008. */
  5009. private function _readBIFF8CellAddressB($cellAddressStructure, $baseCell = 'A1')
  5010. {
  5011. list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
  5012. $baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
  5013. // offset: 0; size: 2; index to row (0... 65535) (or offset (-32768... 32767))
  5014. $rowIndex = self::_GetInt2d($cellAddressStructure, 0);
  5015. $row = self::_GetInt2d($cellAddressStructure, 0) + 1;
  5016. // offset: 2; size: 2; index to column or column offset + relative flags
  5017. // bit: 7-0; mask 0x00FF; column index
  5018. $colIndex = 0x00FF & self::_GetInt2d($cellAddressStructure, 2);
  5019. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5020. if (!(0x4000 & self::_GetInt2d($cellAddressStructure, 2))) {
  5021. $column = PHPExcel_Cell::stringFromColumnIndex($colIndex);
  5022. $column = '$' . $column;
  5023. } else {
  5024. $colIndex = ($colIndex <= 127) ? $colIndex : $colIndex - 256;
  5025. $column = PHPExcel_Cell::stringFromColumnIndex($baseCol + $colIndex);
  5026. }
  5027. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5028. if (!(0x8000 & self::_GetInt2d($cellAddressStructure, 2))) {
  5029. $row = '$' . $row;
  5030. } else {
  5031. $rowIndex = ($rowIndex <= 32767) ? $rowIndex : $rowIndex - 65536;
  5032. $row = $baseRow + $rowIndex;
  5033. }
  5034. return $column . $row;
  5035. }
  5036. /**
  5037. * Reads a cell range address in BIFF5 e.g. 'A2:B6' or 'A1'
  5038. * always fixed range
  5039. * section 2.5.14
  5040. *
  5041. * @param string $subData
  5042. * @return string
  5043. * @throws PHPExcel_Reader_Exception
  5044. */
  5045. private function _readBIFF5CellRangeAddressFixed($subData)
  5046. {
  5047. // offset: 0; size: 2; index to first row
  5048. $fr = self::_GetInt2d($subData, 0) + 1;
  5049. // offset: 2; size: 2; index to last row
  5050. $lr = self::_GetInt2d($subData, 2) + 1;
  5051. // offset: 4; size: 1; index to first column
  5052. $fc = ord($subData{4});
  5053. // offset: 5; size: 1; index to last column
  5054. $lc = ord($subData{5});
  5055. // check values
  5056. if ($fr > $lr || $fc > $lc) {
  5057. throw new PHPExcel_Reader_Exception('Not a cell range address');
  5058. }
  5059. // column index to letter
  5060. $fc = PHPExcel_Cell::stringFromColumnIndex($fc);
  5061. $lc = PHPExcel_Cell::stringFromColumnIndex($lc);
  5062. if ($fr == $lr and $fc == $lc) {
  5063. return "$fc$fr";
  5064. }
  5065. return "$fc$fr:$lc$lr";
  5066. }
  5067. /**
  5068. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or 'A1'
  5069. * always fixed range
  5070. * section 2.5.14
  5071. *
  5072. * @param string $subData
  5073. * @return string
  5074. * @throws PHPExcel_Reader_Exception
  5075. */
  5076. private function _readBIFF8CellRangeAddressFixed($subData)
  5077. {
  5078. // offset: 0; size: 2; index to first row
  5079. $fr = self::_GetInt2d($subData, 0) + 1;
  5080. // offset: 2; size: 2; index to last row
  5081. $lr = self::_GetInt2d($subData, 2) + 1;
  5082. // offset: 4; size: 2; index to first column
  5083. $fc = self::_GetInt2d($subData, 4);
  5084. // offset: 6; size: 2; index to last column
  5085. $lc = self::_GetInt2d($subData, 6);
  5086. // check values
  5087. if ($fr > $lr || $fc > $lc) {
  5088. throw new PHPExcel_Reader_Exception('Not a cell range address');
  5089. }
  5090. // column index to letter
  5091. $fc = PHPExcel_Cell::stringFromColumnIndex($fc);
  5092. $lc = PHPExcel_Cell::stringFromColumnIndex($lc);
  5093. if ($fr == $lr and $fc == $lc) {
  5094. return "$fc$fr";
  5095. }
  5096. return "$fc$fr:$lc$lr";
  5097. }
  5098. /**
  5099. * Reads a cell range address in BIFF8 e.g. 'A2:B6' or '$A$2:$B$6'
  5100. * there are flags indicating whether column/row index is relative
  5101. * section 3.3.4
  5102. *
  5103. * @param string $subData
  5104. * @return string
  5105. */
  5106. private function _readBIFF8CellRangeAddress($subData)
  5107. {
  5108. // todo: if cell range is just a single cell, should this funciton
  5109. // not just return e.g. 'A1' and not 'A1:A1' ?
  5110. // offset: 0; size: 2; index to first row (0... 65535) (or offset (-32768... 32767))
  5111. $fr = self::_GetInt2d($subData, 0) + 1;
  5112. // offset: 2; size: 2; index to last row (0... 65535) (or offset (-32768... 32767))
  5113. $lr = self::_GetInt2d($subData, 2) + 1;
  5114. // offset: 4; size: 2; index to first column or column offset + relative flags
  5115. // bit: 7-0; mask 0x00FF; column index
  5116. $fc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($subData, 4));
  5117. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5118. if (!(0x4000 & self::_GetInt2d($subData, 4))) {
  5119. $fc = '$' . $fc;
  5120. }
  5121. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5122. if (!(0x8000 & self::_GetInt2d($subData, 4))) {
  5123. $fr = '$' . $fr;
  5124. }
  5125. // offset: 6; size: 2; index to last column or column offset + relative flags
  5126. // bit: 7-0; mask 0x00FF; column index
  5127. $lc = PHPExcel_Cell::stringFromColumnIndex(0x00FF & self::_GetInt2d($subData, 6));
  5128. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5129. if (!(0x4000 & self::_GetInt2d($subData, 6))) {
  5130. $lc = '$' . $lc;
  5131. }
  5132. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5133. if (!(0x8000 & self::_GetInt2d($subData, 6))) {
  5134. $lr = '$' . $lr;
  5135. }
  5136. return "$fc$fr:$lc$lr";
  5137. }
  5138. /**
  5139. * Reads a cell range address in BIFF8 for shared formulas. Uses positive and negative values for row and column
  5140. * to indicate offsets from a base cell
  5141. * section 3.3.4
  5142. *
  5143. * @param string $subData
  5144. * @param string $baseCell Base cell
  5145. * @return string Cell range address
  5146. */
  5147. private function _readBIFF8CellRangeAddressB($subData, $baseCell = 'A1')
  5148. {
  5149. list($baseCol, $baseRow) = PHPExcel_Cell::coordinateFromString($baseCell);
  5150. $baseCol = PHPExcel_Cell::columnIndexFromString($baseCol) - 1;
  5151. // TODO: if cell range is just a single cell, should this funciton
  5152. // not just return e.g. 'A1' and not 'A1:A1' ?
  5153. // offset: 0; size: 2; first row
  5154. $frIndex = self::_GetInt2d($subData, 0); // adjust below
  5155. // offset: 2; size: 2; relative index to first row (0... 65535) should be treated as offset (-32768... 32767)
  5156. $lrIndex = self::_GetInt2d($subData, 2); // adjust below
  5157. // offset: 4; size: 2; first column with relative/absolute flags
  5158. // bit: 7-0; mask 0x00FF; column index
  5159. $fcIndex = 0x00FF & self::_GetInt2d($subData, 4);
  5160. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5161. if (!(0x4000 & self::_GetInt2d($subData, 4))) {
  5162. // absolute column index
  5163. $fc = PHPExcel_Cell::stringFromColumnIndex($fcIndex);
  5164. $fc = '$' . $fc;
  5165. } else {
  5166. // column offset
  5167. $fcIndex = ($fcIndex <= 127) ? $fcIndex : $fcIndex - 256;
  5168. $fc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $fcIndex);
  5169. }
  5170. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5171. if (!(0x8000 & self::_GetInt2d($subData, 4))) {
  5172. // absolute row index
  5173. $fr = $frIndex + 1;
  5174. $fr = '$' . $fr;
  5175. } else {
  5176. // row offset
  5177. $frIndex = ($frIndex <= 32767) ? $frIndex : $frIndex - 65536;
  5178. $fr = $baseRow + $frIndex;
  5179. }
  5180. // offset: 6; size: 2; last column with relative/absolute flags
  5181. // bit: 7-0; mask 0x00FF; column index
  5182. $lcIndex = 0x00FF & self::_GetInt2d($subData, 6);
  5183. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  5184. $lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
  5185. // bit: 14; mask 0x4000; (1 = relative column index, 0 = absolute column index)
  5186. if (!(0x4000 & self::_GetInt2d($subData, 6))) {
  5187. // absolute column index
  5188. $lc = PHPExcel_Cell::stringFromColumnIndex($lcIndex);
  5189. $lc = '$' . $lc;
  5190. } else {
  5191. // column offset
  5192. $lcIndex = ($lcIndex <= 127) ? $lcIndex : $lcIndex - 256;
  5193. $lc = PHPExcel_Cell::stringFromColumnIndex($baseCol + $lcIndex);
  5194. }
  5195. // bit: 15; mask 0x8000; (1 = relative row index, 0 = absolute row index)
  5196. if (!(0x8000 & self::_GetInt2d($subData, 6))) {
  5197. // absolute row index
  5198. $lr = $lrIndex + 1;
  5199. $lr = '$' . $lr;
  5200. } else {
  5201. // row offset
  5202. $lrIndex = ($lrIndex <= 32767) ? $lrIndex : $lrIndex - 65536;
  5203. $lr = $baseRow + $lrIndex;
  5204. }
  5205. return "$fc$fr:$lc$lr";
  5206. }
  5207. /**
  5208. * Read BIFF8 cell range address list
  5209. * section 2.5.15
  5210. *
  5211. * @param string $subData
  5212. * @return array
  5213. */
  5214. private function _readBIFF8CellRangeAddressList($subData)
  5215. {
  5216. $cellRangeAddresses = array();
  5217. // offset: 0; size: 2; number of the following cell range addresses
  5218. $nm = self::_GetInt2d($subData, 0);
  5219. $offset = 2;
  5220. // offset: 2; size: 8 * $nm; list of $nm (fixed) cell range addresses
  5221. for ($i = 0; $i < $nm; ++$i) {
  5222. $cellRangeAddresses[] = $this->_readBIFF8CellRangeAddressFixed(substr($subData, $offset, 8));
  5223. $offset += 8;
  5224. }
  5225. return array(
  5226. 'size' => 2 + 8 * $nm,
  5227. 'cellRangeAddresses' => $cellRangeAddresses,
  5228. );
  5229. }
  5230. /**
  5231. * Read BIFF5 cell range address list
  5232. * section 2.5.15
  5233. *
  5234. * @param string $subData
  5235. * @return array
  5236. */
  5237. private function _readBIFF5CellRangeAddressList($subData)
  5238. {
  5239. $cellRangeAddresses = array();
  5240. // offset: 0; size: 2; number of the following cell range addresses
  5241. $nm = self::_GetInt2d($subData, 0);
  5242. $offset = 2;
  5243. // offset: 2; size: 6 * $nm; list of $nm (fixed) cell range addresses
  5244. for ($i = 0; $i < $nm; ++$i) {
  5245. $cellRangeAddresses[] = $this->_readBIFF5CellRangeAddressFixed(substr($subData, $offset, 6));
  5246. $offset += 6;
  5247. }
  5248. return array(
  5249. 'size' => 2 + 6 * $nm,
  5250. 'cellRangeAddresses' => $cellRangeAddresses,
  5251. );
  5252. }
  5253. /**
  5254. * Get a sheet range like Sheet1:Sheet3 from REF index
  5255. * Note: If there is only one sheet in the range, one gets e.g Sheet1
  5256. * It can also happen that the REF structure uses the -1 (FFFF) code to indicate deleted sheets,
  5257. * in which case an PHPExcel_Reader_Exception is thrown
  5258. *
  5259. * @param int $index
  5260. * @return string|false
  5261. * @throws PHPExcel_Reader_Exception
  5262. */
  5263. private function _readSheetRangeByRefIndex($index)
  5264. {
  5265. if (isset($this->_ref[$index])) {
  5266. $type = $this->_externalBooks[$this->_ref[$index]['externalBookIndex']]['type'];
  5267. switch ($type) {
  5268. case 'internal':
  5269. // check if we have a deleted 3d reference
  5270. if ($this->_ref[$index]['firstSheetIndex'] == 0xFFFF or $this->_ref[$index]['lastSheetIndex'] == 0xFFFF) {
  5271. throw new PHPExcel_Reader_Exception('Deleted sheet reference');
  5272. }
  5273. // we have normal sheet range (collapsed or uncollapsed)
  5274. $firstSheetName = $this->_sheets[$this->_ref[$index]['firstSheetIndex']]['name'];
  5275. $lastSheetName = $this->_sheets[$this->_ref[$index]['lastSheetIndex']]['name'];
  5276. if ($firstSheetName == $lastSheetName) {
  5277. // collapsed sheet range
  5278. $sheetRange = $firstSheetName;
  5279. } else {
  5280. $sheetRange = "$firstSheetName:$lastSheetName";
  5281. }
  5282. // escape the single-quotes
  5283. $sheetRange = str_replace("'", "''", $sheetRange);
  5284. // if there are special characters, we need to enclose the range in single-quotes
  5285. // todo: check if we have identified the whole set of special characters
  5286. // it seems that the following characters are not accepted for sheet names
  5287. // and we may assume that they are not present: []*/:\?
  5288. if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/", $sheetRange)) {
  5289. $sheetRange = "'$sheetRange'";
  5290. }
  5291. return $sheetRange;
  5292. break;
  5293. default:
  5294. // TODO: external sheet support
  5295. throw new PHPExcel_Reader_Exception('Excel5 reader only supports internal sheets in fomulas');
  5296. break;
  5297. }
  5298. }
  5299. return false;
  5300. }
  5301. /**
  5302. * read BIFF8 constant value array from array data
  5303. * returns e.g. array('value' => '{1,2;3,4}', 'size' => 40}
  5304. * section 2.5.8
  5305. *
  5306. * @param string $arrayData
  5307. * @return array
  5308. */
  5309. private static function _readBIFF8ConstantArray($arrayData)
  5310. {
  5311. // offset: 0; size: 1; number of columns decreased by 1
  5312. $nc = ord($arrayData[0]);
  5313. // offset: 1; size: 2; number of rows decreased by 1
  5314. $nr = self::_GetInt2d($arrayData, 1);
  5315. $size = 3; // initialize
  5316. $arrayData = substr($arrayData, 3);
  5317. // offset: 3; size: var; list of ($nc + 1) * ($nr + 1) constant values
  5318. $matrixChunks = array();
  5319. for ($r = 1; $r <= $nr + 1; ++$r) {
  5320. $items = array();
  5321. for ($c = 1; $c <= $nc + 1; ++$c) {
  5322. $constant = self::_readBIFF8Constant($arrayData);
  5323. $items[] = $constant['value'];
  5324. $arrayData = substr($arrayData, $constant['size']);
  5325. $size += $constant['size'];
  5326. }
  5327. $matrixChunks[] = implode(',', $items); // looks like e.g. '1,"hello"'
  5328. }
  5329. $matrix = '{' . implode(';', $matrixChunks) . '}';
  5330. return array(
  5331. 'value' => $matrix,
  5332. 'size' => $size,
  5333. );
  5334. }
  5335. /**
  5336. * read BIFF8 constant value which may be 'Empty Value', 'Number', 'String Value', 'Boolean Value', 'Error Value'
  5337. * section 2.5.7
  5338. * returns e.g. array('value' => '5', 'size' => 9)
  5339. *
  5340. * @param string $valueData
  5341. * @return array
  5342. */
  5343. private static function _readBIFF8Constant($valueData)
  5344. {
  5345. // offset: 0; size: 1; identifier for type of constant
  5346. $identifier = ord($valueData[0]);
  5347. switch ($identifier) {
  5348. case 0x00: // empty constant (what is this?)
  5349. $value = '';
  5350. $size = 9;
  5351. break;
  5352. case 0x01: // number
  5353. // offset: 1; size: 8; IEEE 754 floating-point value
  5354. $value = self::_extractNumber(substr($valueData, 1, 8));
  5355. $size = 9;
  5356. break;
  5357. case 0x02: // string value
  5358. // offset: 1; size: var; Unicode string, 16-bit string length
  5359. $string = self::_readUnicodeStringLong(substr($valueData, 1));
  5360. $value = '"' . $string['value'] . '"';
  5361. $size = 1 + $string['size'];
  5362. break;
  5363. case 0x04: // boolean
  5364. // offset: 1; size: 1; 0 = FALSE, 1 = TRUE
  5365. if (ord($valueData[1])) {
  5366. $value = 'TRUE';
  5367. } else {
  5368. $value = 'FALSE';
  5369. }
  5370. $size = 9;
  5371. break;
  5372. case 0x10: // error code
  5373. // offset: 1; size: 1; error code
  5374. $value = self::_mapErrorCode(ord($valueData[1]));
  5375. $size = 9;
  5376. break;
  5377. }
  5378. return array(
  5379. 'value' => $value,
  5380. 'size' => $size,
  5381. );
  5382. }
  5383. /**
  5384. * Extract RGB color
  5385. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.4
  5386. *
  5387. * @param string $rgb Encoded RGB value (4 bytes)
  5388. * @return array
  5389. */
  5390. private static function _readRGB($rgb)
  5391. {
  5392. // offset: 0; size 1; Red component
  5393. $r = ord($rgb{0});
  5394. // offset: 1; size: 1; Green component
  5395. $g = ord($rgb{1});
  5396. // offset: 2; size: 1; Blue component
  5397. $b = ord($rgb{2});
  5398. // HEX notation, e.g. 'FF00FC'
  5399. $rgb = sprintf('%02X%02X%02X', $r, $g, $b);
  5400. return array('rgb' => $rgb);
  5401. }
  5402. /**
  5403. * Read byte string (8-bit string length)
  5404. * OpenOffice documentation: 2.5.2
  5405. *
  5406. * @param string $subData
  5407. * @return array
  5408. */
  5409. private function _readByteStringShort($subData)
  5410. {
  5411. // offset: 0; size: 1; length of the string (character count)
  5412. $ln = ord($subData[0]);
  5413. // offset: 1: size: var; character array (8-bit characters)
  5414. $value = $this->_decodeCodepage(substr($subData, 1, $ln));
  5415. return array(
  5416. 'value' => $value,
  5417. 'size' => 1 + $ln, // size in bytes of data structure
  5418. );
  5419. }
  5420. /**
  5421. * Read byte string (16-bit string length)
  5422. * OpenOffice documentation: 2.5.2
  5423. *
  5424. * @param string $subData
  5425. * @return array
  5426. */
  5427. private function _readByteStringLong($subData)
  5428. {
  5429. // offset: 0; size: 2; length of the string (character count)
  5430. $ln = self::_GetInt2d($subData, 0);
  5431. // offset: 2: size: var; character array (8-bit characters)
  5432. $value = $this->_decodeCodepage(substr($subData, 2));
  5433. //return $string;
  5434. return array(
  5435. 'value' => $value,
  5436. 'size' => 2 + $ln, // size in bytes of data structure
  5437. );
  5438. }
  5439. /**
  5440. * Extracts an Excel Unicode short string (8-bit string length)
  5441. * OpenOffice documentation: 2.5.3
  5442. * function will automatically find out where the Unicode string ends.
  5443. *
  5444. * @param string $subData
  5445. * @return array
  5446. */
  5447. private static function _readUnicodeStringShort($subData)
  5448. {
  5449. $value = '';
  5450. // offset: 0: size: 1; length of the string (character count)
  5451. $characterCount = ord($subData[0]);
  5452. $string = self::_readUnicodeString(substr($subData, 1), $characterCount);
  5453. // add 1 for the string length
  5454. $string['size'] += 1;
  5455. return $string;
  5456. }
  5457. /**
  5458. * Extracts an Excel Unicode long string (16-bit string length)
  5459. * OpenOffice documentation: 2.5.3
  5460. * this function is under construction, needs to support rich text, and Asian phonetic settings
  5461. *
  5462. * @param string $subData
  5463. * @return array
  5464. */
  5465. private static function _readUnicodeStringLong($subData)
  5466. {
  5467. $value = '';
  5468. // offset: 0: size: 2; length of the string (character count)
  5469. $characterCount = self::_GetInt2d($subData, 0);
  5470. $string = self::_readUnicodeString(substr($subData, 2), $characterCount);
  5471. // add 2 for the string length
  5472. $string['size'] += 2;
  5473. return $string;
  5474. }
  5475. /**
  5476. * Read Unicode string with no string length field, but with known character count
  5477. * this function is under construction, needs to support rich text, and Asian phonetic settings
  5478. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
  5479. *
  5480. * @param string $subData
  5481. * @param int $characterCount
  5482. * @return array
  5483. */
  5484. private static function _readUnicodeString($subData, $characterCount)
  5485. {
  5486. $value = '';
  5487. // offset: 0: size: 1; option flags
  5488. // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  5489. $isCompressed = !((0x01 & ord($subData[0])) >> 0);
  5490. // bit: 2; mask: 0x04; Asian phonetic settings
  5491. $hasAsian = (0x04) & ord($subData[0]) >> 2;
  5492. // bit: 3; mask: 0x08; Rich-Text settings
  5493. $hasRichText = (0x08) & ord($subData[0]) >> 3;
  5494. // offset: 1: size: var; character array
  5495. // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  5496. // needs to be fixed
  5497. $value = self::_encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
  5498. return array(
  5499. 'value' => $value,
  5500. 'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount, // the size in bytes including the option flags
  5501. );
  5502. }
  5503. /**
  5504. * Convert UTF-8 string to string surounded by double quotes. Used for explicit string tokens in formulas.
  5505. * Example: hello"world --> "hello""world"
  5506. *
  5507. * @param string $value UTF-8 encoded string
  5508. * @return string
  5509. */
  5510. private static function _UTF8toExcelDoubleQuoted($value)
  5511. {
  5512. return '"' . str_replace('"', '""', $value) . '"';
  5513. }
  5514. /**
  5515. * Reads first 8 bytes of a string and return IEEE 754 float
  5516. *
  5517. * @param string $data Binary string that is at least 8 bytes long
  5518. * @return float
  5519. */
  5520. private static function _extractNumber($data)
  5521. {
  5522. $rknumhigh = self::_GetInt4d($data, 4);
  5523. $rknumlow = self::_GetInt4d($data, 0);
  5524. $sign = ($rknumhigh & 0x80000000) >> 31;
  5525. $exp = (($rknumhigh & 0x7ff00000) >> 20) - 1023;
  5526. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  5527. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  5528. $mantissalow2 = ($rknumlow & 0x7fffffff);
  5529. $value = $mantissa / pow( 2 , (20 - $exp));
  5530. if ($mantissalow1 != 0) {
  5531. $value += 1 / pow (2 , (21 - $exp));
  5532. }
  5533. $value += $mantissalow2 / pow (2 , (52 - $exp));
  5534. if ($sign) {
  5535. $value *= -1;
  5536. }
  5537. return $value;
  5538. }
  5539. private static function _GetIEEE754($rknum)
  5540. {
  5541. if (($rknum & 0x02) != 0) {
  5542. $value = $rknum >> 2;
  5543. } else {
  5544. // changes by mmp, info on IEEE754 encoding from
  5545. // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  5546. // The RK format calls for using only the most significant 30 bits
  5547. // of the 64 bit floating point value. The other 34 bits are assumed
  5548. // to be 0 so we use the upper 30 bits of $rknum as follows...
  5549. $sign = ($rknum & 0x80000000) >> 31;
  5550. $exp = ($rknum & 0x7ff00000) >> 20;
  5551. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  5552. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  5553. if ($sign) {
  5554. $value = -1 * $value;
  5555. }
  5556. //end of changes by mmp
  5557. }
  5558. if (($rknum & 0x01) != 0) {
  5559. $value /= 100;
  5560. }
  5561. return $value;
  5562. }
  5563. /**
  5564. * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
  5565. *
  5566. * @param string $string
  5567. * @param bool $compressed
  5568. * @return string
  5569. */
  5570. private static function _encodeUTF16($string, $compressed = '')
  5571. {
  5572. if ($compressed) {
  5573. $string = self::_uncompressByteString($string);
  5574. }
  5575. return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', 'UTF-16LE');
  5576. }
  5577. /**
  5578. * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
  5579. *
  5580. * @param string $string
  5581. * @return string
  5582. */
  5583. private static function _uncompressByteString($string)
  5584. {
  5585. $uncompressedString = '';
  5586. $strLen = strlen($string);
  5587. for ($i = 0; $i < $strLen; ++$i) {
  5588. $uncompressedString .= $string[$i] . "\0";
  5589. }
  5590. return $uncompressedString;
  5591. }
  5592. /**
  5593. * Convert string to UTF-8. Only used for BIFF5.
  5594. *
  5595. * @param string $string
  5596. * @return string
  5597. */
  5598. private function _decodeCodepage($string)
  5599. {
  5600. return PHPExcel_Shared_String::ConvertEncoding($string, 'UTF-8', $this->_codepage);
  5601. }
  5602. /**
  5603. * Read 16-bit unsigned integer
  5604. *
  5605. * @param string $data
  5606. * @param int $pos
  5607. * @return int
  5608. */
  5609. public static function _GetInt2d($data, $pos)
  5610. {
  5611. return ord($data[$pos]) | (ord($data[$pos+1]) << 8);
  5612. }
  5613. /**
  5614. * Read 32-bit signed integer
  5615. *
  5616. * @param string $data
  5617. * @param int $pos
  5618. * @return int
  5619. */
  5620. public static function _GetInt4d($data, $pos)
  5621. {
  5622. // FIX: represent numbers correctly on 64-bit system
  5623. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  5624. // Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  5625. $_or_24 = ord($data[$pos + 3]);
  5626. if ($_or_24 >= 128) {
  5627. // negative number
  5628. $_ord_24 = -abs((256 - $_or_24) << 24);
  5629. } else {
  5630. $_ord_24 = ($_or_24 & 127) << 24;
  5631. }
  5632. return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | $_ord_24;
  5633. }
  5634. /**
  5635. * Read color
  5636. *
  5637. * @param int $color Indexed color
  5638. * @param array $palette Color palette
  5639. * @return array RGB color value, example: array('rgb' => 'FF0000')
  5640. */
  5641. private static function _readColor($color,$palette,$version)
  5642. {
  5643. if ($color <= 0x07 || $color >= 0x40) {
  5644. // special built-in color
  5645. return self::_mapBuiltInColor($color);
  5646. } elseif (isset($palette) && isset($palette[$color - 8])) {
  5647. // palette color, color index 0x08 maps to pallete index 0
  5648. return $palette[$color - 8];
  5649. } else {
  5650. // default color table
  5651. if ($version == self::XLS_BIFF8) {
  5652. return self::_mapColor($color);
  5653. } else {
  5654. // BIFF5
  5655. return self::_mapColorBIFF5($color);
  5656. }
  5657. }
  5658. return $color;
  5659. }
  5660. /**
  5661. * Map border style
  5662. * OpenOffice documentation: 2.5.11
  5663. *
  5664. * @param int $index
  5665. * @return string
  5666. */
  5667. private static function _mapBorderStyle($index)
  5668. {
  5669. switch ($index) {
  5670. case 0x00: return PHPExcel_Style_Border::BORDER_NONE;
  5671. case 0x01: return PHPExcel_Style_Border::BORDER_THIN;
  5672. case 0x02: return PHPExcel_Style_Border::BORDER_MEDIUM;
  5673. case 0x03: return PHPExcel_Style_Border::BORDER_DASHED;
  5674. case 0x04: return PHPExcel_Style_Border::BORDER_DOTTED;
  5675. case 0x05: return PHPExcel_Style_Border::BORDER_THICK;
  5676. case 0x06: return PHPExcel_Style_Border::BORDER_DOUBLE;
  5677. case 0x07: return PHPExcel_Style_Border::BORDER_HAIR;
  5678. case 0x08: return PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
  5679. case 0x09: return PHPExcel_Style_Border::BORDER_DASHDOT;
  5680. case 0x0A: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
  5681. case 0x0B: return PHPExcel_Style_Border::BORDER_DASHDOTDOT;
  5682. case 0x0C: return PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  5683. case 0x0D: return PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
  5684. default: return PHPExcel_Style_Border::BORDER_NONE;
  5685. }
  5686. }
  5687. /**
  5688. * Get fill pattern from index
  5689. * OpenOffice documentation: 2.5.12
  5690. *
  5691. * @param int $index
  5692. * @return string
  5693. */
  5694. private static function _mapFillPattern($index)
  5695. {
  5696. switch ($index) {
  5697. case 0x00: return PHPExcel_Style_Fill::FILL_NONE;
  5698. case 0x01: return PHPExcel_Style_Fill::FILL_SOLID;
  5699. case 0x02: return PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
  5700. case 0x03: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
  5701. case 0x04: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
  5702. case 0x05: return PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
  5703. case 0x06: return PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
  5704. case 0x07: return PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
  5705. case 0x08: return PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
  5706. case 0x09: return PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
  5707. case 0x0A: return PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
  5708. case 0x0B: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
  5709. case 0x0C: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
  5710. case 0x0D: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
  5711. case 0x0E: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
  5712. case 0x0F: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
  5713. case 0x10: return PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
  5714. case 0x11: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
  5715. case 0x12: return PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
  5716. default: return PHPExcel_Style_Fill::FILL_NONE;
  5717. }
  5718. }
  5719. /**
  5720. * Map error code, e.g. '#N/A'
  5721. *
  5722. * @param int $subData
  5723. * @return string
  5724. */
  5725. private static function _mapErrorCode($subData)
  5726. {
  5727. switch ($subData) {
  5728. case 0x00: return '#NULL!'; break;
  5729. case 0x07: return '#DIV/0!'; break;
  5730. case 0x0F: return '#VALUE!'; break;
  5731. case 0x17: return '#REF!'; break;
  5732. case 0x1D: return '#NAME?'; break;
  5733. case 0x24: return '#NUM!'; break;
  5734. case 0x2A: return '#N/A'; break;
  5735. default: return false;
  5736. }
  5737. }
  5738. /**
  5739. * Map built-in color to RGB value
  5740. *
  5741. * @param int $color Indexed color
  5742. * @return array
  5743. */
  5744. private static function _mapBuiltInColor($color)
  5745. {
  5746. switch ($color) {
  5747. case 0x00: return array('rgb' => '000000');
  5748. case 0x01: return array('rgb' => 'FFFFFF');
  5749. case 0x02: return array('rgb' => 'FF0000');
  5750. case 0x03: return array('rgb' => '00FF00');
  5751. case 0x04: return array('rgb' => '0000FF');
  5752. case 0x05: return array('rgb' => 'FFFF00');
  5753. case 0x06: return array('rgb' => 'FF00FF');
  5754. case 0x07: return array('rgb' => '00FFFF');
  5755. case 0x40: return array('rgb' => '000000'); // system window text color
  5756. case 0x41: return array('rgb' => 'FFFFFF'); // system window background color
  5757. default: return array('rgb' => '000000');
  5758. }
  5759. }
  5760. /**
  5761. * Map color array from BIFF5 built-in color index
  5762. *
  5763. * @param int $subData
  5764. * @return array
  5765. */
  5766. private static function _mapColorBIFF5($subData)
  5767. {
  5768. switch ($subData) {
  5769. case 0x08: return array('rgb' => '000000');
  5770. case 0x09: return array('rgb' => 'FFFFFF');
  5771. case 0x0A: return array('rgb' => 'FF0000');
  5772. case 0x0B: return array('rgb' => '00FF00');
  5773. case 0x0C: return array('rgb' => '0000FF');
  5774. case 0x0D: return array('rgb' => 'FFFF00');
  5775. case 0x0E: return array('rgb' => 'FF00FF');
  5776. case 0x0F: return array('rgb' => '00FFFF');
  5777. case 0x10: return array('rgb' => '800000');
  5778. case 0x11: return array('rgb' => '008000');
  5779. case 0x12: return array('rgb' => '000080');
  5780. case 0x13: return array('rgb' => '808000');
  5781. case 0x14: return array('rgb' => '800080');
  5782. case 0x15: return array('rgb' => '008080');
  5783. case 0x16: return array('rgb' => 'C0C0C0');
  5784. case 0x17: return array('rgb' => '808080');
  5785. case 0x18: return array('rgb' => '8080FF');
  5786. case 0x19: return array('rgb' => '802060');
  5787. case 0x1A: return array('rgb' => 'FFFFC0');
  5788. case 0x1B: return array('rgb' => 'A0E0F0');
  5789. case 0x1C: return array('rgb' => '600080');
  5790. case 0x1D: return array('rgb' => 'FF8080');
  5791. case 0x1E: return array('rgb' => '0080C0');
  5792. case 0x1F: return array('rgb' => 'C0C0FF');
  5793. case 0x20: return array('rgb' => '000080');
  5794. case 0x21: return array('rgb' => 'FF00FF');
  5795. case 0x22: return array('rgb' => 'FFFF00');
  5796. case 0x23: return array('rgb' => '00FFFF');
  5797. case 0x24: return array('rgb' => '800080');
  5798. case 0x25: return array('rgb' => '800000');
  5799. case 0x26: return array('rgb' => '008080');
  5800. case 0x27: return array('rgb' => '0000FF');
  5801. case 0x28: return array('rgb' => '00CFFF');
  5802. case 0x29: return array('rgb' => '69FFFF');
  5803. case 0x2A: return array('rgb' => 'E0FFE0');
  5804. case 0x2B: return array('rgb' => 'FFFF80');
  5805. case 0x2C: return array('rgb' => 'A6CAF0');
  5806. case 0x2D: return array('rgb' => 'DD9CB3');
  5807. case 0x2E: return array('rgb' => 'B38FEE');
  5808. case 0x2F: return array('rgb' => 'E3E3E3');
  5809. case 0x30: return array('rgb' => '2A6FF9');
  5810. case 0x31: return array('rgb' => '3FB8CD');
  5811. case 0x32: return array('rgb' => '488436');
  5812. case 0x33: return array('rgb' => '958C41');
  5813. case 0x34: return array('rgb' => '8E5E42');
  5814. case 0x35: return array('rgb' => 'A0627A');
  5815. case 0x36: return array('rgb' => '624FAC');
  5816. case 0x37: return array('rgb' => '969696');
  5817. case 0x38: return array('rgb' => '1D2FBE');
  5818. case 0x39: return array('rgb' => '286676');
  5819. case 0x3A: return array('rgb' => '004500');
  5820. case 0x3B: return array('rgb' => '453E01');
  5821. case 0x3C: return array('rgb' => '6A2813');
  5822. case 0x3D: return array('rgb' => '85396A');
  5823. case 0x3E: return array('rgb' => '4A3285');
  5824. case 0x3F: return array('rgb' => '424242');
  5825. default: return array('rgb' => '000000');
  5826. }
  5827. }
  5828. /**
  5829. * Map color array from BIFF8 built-in color index
  5830. *
  5831. * @param int $subData
  5832. * @return array
  5833. */
  5834. private static function _mapColor($subData)
  5835. {
  5836. switch ($subData) {
  5837. case 0x08: return array('rgb' => '000000');
  5838. case 0x09: return array('rgb' => 'FFFFFF');
  5839. case 0x0A: return array('rgb' => 'FF0000');
  5840. case 0x0B: return array('rgb' => '00FF00');
  5841. case 0x0C: return array('rgb' => '0000FF');
  5842. case 0x0D: return array('rgb' => 'FFFF00');
  5843. case 0x0E: return array('rgb' => 'FF00FF');
  5844. case 0x0F: return array('rgb' => '00FFFF');
  5845. case 0x10: return array('rgb' => '800000');
  5846. case 0x11: return array('rgb' => '008000');
  5847. case 0x12: return array('rgb' => '000080');
  5848. case 0x13: return array('rgb' => '808000');
  5849. case 0x14: return array('rgb' => '800080');
  5850. case 0x15: return array('rgb' => '008080');
  5851. case 0x16: return array('rgb' => 'C0C0C0');
  5852. case 0x17: return array('rgb' => '808080');
  5853. case 0x18: return array('rgb' => '9999FF');
  5854. case 0x19: return array('rgb' => '993366');
  5855. case 0x1A: return array('rgb' => 'FFFFCC');
  5856. case 0x1B: return array('rgb' => 'CCFFFF');
  5857. case 0x1C: return array('rgb' => '660066');
  5858. case 0x1D: return array('rgb' => 'FF8080');
  5859. case 0x1E: return array('rgb' => '0066CC');
  5860. case 0x1F: return array('rgb' => 'CCCCFF');
  5861. case 0x20: return array('rgb' => '000080');
  5862. case 0x21: return array('rgb' => 'FF00FF');
  5863. case 0x22: return array('rgb' => 'FFFF00');
  5864. case 0x23: return array('rgb' => '00FFFF');
  5865. case 0x24: return array('rgb' => '800080');
  5866. case 0x25: return array('rgb' => '800000');
  5867. case 0x26: return array('rgb' => '008080');
  5868. case 0x27: return array('rgb' => '0000FF');
  5869. case 0x28: return array('rgb' => '00CCFF');
  5870. case 0x29: return array('rgb' => 'CCFFFF');
  5871. case 0x2A: return array('rgb' => 'CCFFCC');
  5872. case 0x2B: return array('rgb' => 'FFFF99');
  5873. case 0x2C: return array('rgb' => '99CCFF');
  5874. case 0x2D: return array('rgb' => 'FF99CC');
  5875. case 0x2E: return array('rgb' => 'CC99FF');
  5876. case 0x2F: return array('rgb' => 'FFCC99');
  5877. case 0x30: return array('rgb' => '3366FF');
  5878. case 0x31: return array('rgb' => '33CCCC');
  5879. case 0x32: return array('rgb' => '99CC00');
  5880. case 0x33: return array('rgb' => 'FFCC00');
  5881. case 0x34: return array('rgb' => 'FF9900');
  5882. case 0x35: return array('rgb' => 'FF6600');
  5883. case 0x36: return array('rgb' => '666699');
  5884. case 0x37: return array('rgb' => '969696');
  5885. case 0x38: return array('rgb' => '003366');
  5886. case 0x39: return array('rgb' => '339966');
  5887. case 0x3A: return array('rgb' => '003300');
  5888. case 0x3B: return array('rgb' => '333300');
  5889. case 0x3C: return array('rgb' => '993300');
  5890. case 0x3D: return array('rgb' => '993366');
  5891. case 0x3E: return array('rgb' => '333399');
  5892. case 0x3F: return array('rgb' => '333333');
  5893. default: return array('rgb' => '000000');
  5894. }
  5895. }
  5896. private function _parseRichText($is = '') {
  5897. $value = new PHPExcel_RichText();
  5898. $value->createText($is);
  5899. return $value;
  5900. }
  5901. }