Excel5.php 293 KB

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