Worksheet.php 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
  3. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  4. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  5. use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
  6. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  7. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  8. use PhpOffice\PhpSpreadsheet\RichText\Run;
  9. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  10. use PhpOffice\PhpSpreadsheet\Shared\Xls;
  11. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  12. use PhpOffice\PhpSpreadsheet\Style\Border;
  13. use PhpOffice\PhpSpreadsheet\Style\Color;
  14. use PhpOffice\PhpSpreadsheet\Style\Conditional;
  15. use PhpOffice\PhpSpreadsheet\Style\Fill;
  16. use PhpOffice\PhpSpreadsheet\Style\Protection;
  17. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  18. use PhpOffice\PhpSpreadsheet\Worksheet\SheetView;
  19. use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
  20. // Original file header of PEAR::Spreadsheet_Excel_Writer_Worksheet (used as the base for this class):
  21. // -----------------------------------------------------------------------------------------
  22. // /*
  23. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  24. // *
  25. // * The majority of this is _NOT_ my code. I simply ported it from the
  26. // * PERL Spreadsheet::WriteExcel module.
  27. // *
  28. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  29. // * <jmcnamara@cpan.org>
  30. // *
  31. // * I _DO_ maintain this code, and John McNamara has nothing to do with the
  32. // * porting of this code to PHP. Any questions directly related to this
  33. // * class library should be directed to me.
  34. // *
  35. // * License Information:
  36. // *
  37. // * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  38. // * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  39. // *
  40. // * This library is free software; you can redistribute it and/or
  41. // * modify it under the terms of the GNU Lesser General Public
  42. // * License as published by the Free Software Foundation; either
  43. // * version 2.1 of the License, or (at your option) any later version.
  44. // *
  45. // * This library is distributed in the hope that it will be useful,
  46. // * but WITHOUT ANY WARRANTY; without even the implied warranty of
  47. // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  48. // * Lesser General Public License for more details.
  49. // *
  50. // * You should have received a copy of the GNU Lesser General Public
  51. // * License along with this library; if not, write to the Free Software
  52. // * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  53. // */
  54. class Worksheet extends BIFFwriter
  55. {
  56. /**
  57. * Formula parser.
  58. *
  59. * @var \PhpOffice\PhpSpreadsheet\Writer\Xls\Parser
  60. */
  61. private $parser;
  62. /**
  63. * Maximum number of characters for a string (LABEL record in BIFF5).
  64. *
  65. * @var int
  66. */
  67. private $xlsStringMaxLength;
  68. /**
  69. * Array containing format information for columns.
  70. *
  71. * @var array
  72. */
  73. private $columnInfo;
  74. /**
  75. * Array containing the selected area for the worksheet.
  76. *
  77. * @var array
  78. */
  79. private $selection;
  80. /**
  81. * The active pane for the worksheet.
  82. *
  83. * @var int
  84. */
  85. private $activePane;
  86. /**
  87. * Whether to use outline.
  88. *
  89. * @var int
  90. */
  91. private $outlineOn;
  92. /**
  93. * Auto outline styles.
  94. *
  95. * @var bool
  96. */
  97. private $outlineStyle;
  98. /**
  99. * Whether to have outline summary below.
  100. *
  101. * @var bool
  102. */
  103. private $outlineBelow;
  104. /**
  105. * Whether to have outline summary at the right.
  106. *
  107. * @var bool
  108. */
  109. private $outlineRight;
  110. /**
  111. * Reference to the total number of strings in the workbook.
  112. *
  113. * @var int
  114. */
  115. private $stringTotal;
  116. /**
  117. * Reference to the number of unique strings in the workbook.
  118. *
  119. * @var int
  120. */
  121. private $stringUnique;
  122. /**
  123. * Reference to the array containing all the unique strings in the workbook.
  124. *
  125. * @var array
  126. */
  127. private $stringTable;
  128. /**
  129. * Color cache.
  130. */
  131. private $colors;
  132. /**
  133. * Index of first used row (at least 0).
  134. *
  135. * @var int
  136. */
  137. private $firstRowIndex;
  138. /**
  139. * Index of last used row. (no used rows means -1).
  140. *
  141. * @var int
  142. */
  143. private $lastRowIndex;
  144. /**
  145. * Index of first used column (at least 0).
  146. *
  147. * @var int
  148. */
  149. private $firstColumnIndex;
  150. /**
  151. * Index of last used column (no used columns means -1).
  152. *
  153. * @var int
  154. */
  155. private $lastColumnIndex;
  156. /**
  157. * Sheet object.
  158. *
  159. * @var \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  160. */
  161. public $phpSheet;
  162. /**
  163. * Count cell style Xfs.
  164. *
  165. * @var int
  166. */
  167. private $countCellStyleXfs;
  168. /**
  169. * Escher object corresponding to MSODRAWING.
  170. *
  171. * @var \PhpOffice\PhpSpreadsheet\Shared\Escher
  172. */
  173. private $escher;
  174. /**
  175. * Array of font hashes associated to FONT records index.
  176. *
  177. * @var array
  178. */
  179. public $fontHashIndex;
  180. /**
  181. * @var bool
  182. */
  183. private $preCalculateFormulas;
  184. /**
  185. * @var int
  186. */
  187. private $printHeaders;
  188. /**
  189. * Constructor.
  190. *
  191. * @param int $str_total Total number of strings
  192. * @param int $str_unique Total number of unique strings
  193. * @param array &$str_table String Table
  194. * @param array &$colors Colour Table
  195. * @param Parser $parser The formula parser created for the Workbook
  196. * @param bool $preCalculateFormulas Flag indicating whether formulas should be calculated or just written
  197. * @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet The worksheet to write
  198. */
  199. public function __construct(&$str_total, &$str_unique, &$str_table, &$colors, Parser $parser, $preCalculateFormulas, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet)
  200. {
  201. // It needs to call its parent's constructor explicitly
  202. parent::__construct();
  203. $this->preCalculateFormulas = $preCalculateFormulas;
  204. $this->stringTotal = &$str_total;
  205. $this->stringUnique = &$str_unique;
  206. $this->stringTable = &$str_table;
  207. $this->colors = &$colors;
  208. $this->parser = $parser;
  209. $this->phpSheet = $phpSheet;
  210. $this->xlsStringMaxLength = 255;
  211. $this->columnInfo = [];
  212. $this->selection = [0, 0, 0, 0];
  213. $this->activePane = 3;
  214. $this->printHeaders = 0;
  215. $this->outlineStyle = 0;
  216. $this->outlineBelow = 1;
  217. $this->outlineRight = 1;
  218. $this->outlineOn = 1;
  219. $this->fontHashIndex = [];
  220. // calculate values for DIMENSIONS record
  221. $minR = 1;
  222. $minC = 'A';
  223. $maxR = $this->phpSheet->getHighestRow();
  224. $maxC = $this->phpSheet->getHighestColumn();
  225. // Determine lowest and highest column and row
  226. $this->lastRowIndex = ($maxR > 65535) ? 65535 : $maxR;
  227. $this->firstColumnIndex = Coordinate::columnIndexFromString($minC);
  228. $this->lastColumnIndex = Coordinate::columnIndexFromString($maxC);
  229. // if ($this->firstColumnIndex > 255) $this->firstColumnIndex = 255;
  230. if ($this->lastColumnIndex > 255) {
  231. $this->lastColumnIndex = 255;
  232. }
  233. $this->countCellStyleXfs = count($phpSheet->getParent()->getCellStyleXfCollection());
  234. }
  235. /**
  236. * Add data to the beginning of the workbook (note the reverse order)
  237. * and to the end of the workbook.
  238. *
  239. * @see \PhpOffice\PhpSpreadsheet\Writer\Xls\Workbook::storeWorkbook()
  240. */
  241. public function close()
  242. {
  243. $phpSheet = $this->phpSheet;
  244. // Write BOF record
  245. $this->storeBof(0x0010);
  246. // Write PRINTHEADERS
  247. $this->writePrintHeaders();
  248. // Write PRINTGRIDLINES
  249. $this->writePrintGridlines();
  250. // Write GRIDSET
  251. $this->writeGridset();
  252. // Calculate column widths
  253. $phpSheet->calculateColumnWidths();
  254. // Column dimensions
  255. if (($defaultWidth = $phpSheet->getDefaultColumnDimension()->getWidth()) < 0) {
  256. $defaultWidth = \PhpOffice\PhpSpreadsheet\Shared\Font::getDefaultColumnWidthByFont($phpSheet->getParent()->getDefaultStyle()->getFont());
  257. }
  258. $columnDimensions = $phpSheet->getColumnDimensions();
  259. $maxCol = $this->lastColumnIndex - 1;
  260. for ($i = 0; $i <= $maxCol; ++$i) {
  261. $hidden = 0;
  262. $level = 0;
  263. $xfIndex = 15; // there are 15 cell style Xfs
  264. $width = $defaultWidth;
  265. $columnLetter = Coordinate::stringFromColumnIndex($i + 1);
  266. if (isset($columnDimensions[$columnLetter])) {
  267. $columnDimension = $columnDimensions[$columnLetter];
  268. if ($columnDimension->getWidth() >= 0) {
  269. $width = $columnDimension->getWidth();
  270. }
  271. $hidden = $columnDimension->getVisible() ? 0 : 1;
  272. $level = $columnDimension->getOutlineLevel();
  273. $xfIndex = $columnDimension->getXfIndex() + 15; // there are 15 cell style Xfs
  274. }
  275. // Components of columnInfo:
  276. // $firstcol first column on the range
  277. // $lastcol last column on the range
  278. // $width width to set
  279. // $xfIndex The optional cell style Xf index to apply to the columns
  280. // $hidden The optional hidden atribute
  281. // $level The optional outline level
  282. $this->columnInfo[] = [$i, $i, $width, $xfIndex, $hidden, $level];
  283. }
  284. // Write GUTS
  285. $this->writeGuts();
  286. // Write DEFAULTROWHEIGHT
  287. $this->writeDefaultRowHeight();
  288. // Write WSBOOL
  289. $this->writeWsbool();
  290. // Write horizontal and vertical page breaks
  291. $this->writeBreaks();
  292. // Write page header
  293. $this->writeHeader();
  294. // Write page footer
  295. $this->writeFooter();
  296. // Write page horizontal centering
  297. $this->writeHcenter();
  298. // Write page vertical centering
  299. $this->writeVcenter();
  300. // Write left margin
  301. $this->writeMarginLeft();
  302. // Write right margin
  303. $this->writeMarginRight();
  304. // Write top margin
  305. $this->writeMarginTop();
  306. // Write bottom margin
  307. $this->writeMarginBottom();
  308. // Write page setup
  309. $this->writeSetup();
  310. // Write sheet protection
  311. $this->writeProtect();
  312. // Write SCENPROTECT
  313. $this->writeScenProtect();
  314. // Write OBJECTPROTECT
  315. $this->writeObjectProtect();
  316. // Write sheet password
  317. $this->writePassword();
  318. // Write DEFCOLWIDTH record
  319. $this->writeDefcol();
  320. // Write the COLINFO records if they exist
  321. if (!empty($this->columnInfo)) {
  322. $colcount = count($this->columnInfo);
  323. for ($i = 0; $i < $colcount; ++$i) {
  324. $this->writeColinfo($this->columnInfo[$i]);
  325. }
  326. }
  327. $autoFilterRange = $phpSheet->getAutoFilter()->getRange();
  328. if (!empty($autoFilterRange)) {
  329. // Write AUTOFILTERINFO
  330. $this->writeAutoFilterInfo();
  331. }
  332. // Write sheet dimensions
  333. $this->writeDimensions();
  334. // Row dimensions
  335. foreach ($phpSheet->getRowDimensions() as $rowDimension) {
  336. $xfIndex = $rowDimension->getXfIndex() + 15; // there are 15 cellXfs
  337. $this->writeRow($rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), $xfIndex, ($rowDimension->getVisible() ? '0' : '1'), $rowDimension->getOutlineLevel());
  338. }
  339. // Write Cells
  340. foreach ($phpSheet->getCoordinates() as $coordinate) {
  341. $cell = $phpSheet->getCell($coordinate);
  342. $row = $cell->getRow() - 1;
  343. $column = Coordinate::columnIndexFromString($cell->getColumn()) - 1;
  344. // Don't break Excel break the code!
  345. if ($row > 65535 || $column > 255) {
  346. throw new WriterException('Rows or columns overflow! Excel5 has limit to 65535 rows and 255 columns. Use XLSX instead.');
  347. }
  348. // Write cell value
  349. $xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs
  350. $cVal = $cell->getValue();
  351. if ($cVal instanceof RichText) {
  352. $arrcRun = [];
  353. $str_len = StringHelper::countCharacters($cVal->getPlainText(), 'UTF-8');
  354. $str_pos = 0;
  355. $elements = $cVal->getRichTextElements();
  356. foreach ($elements as $element) {
  357. // FONT Index
  358. if ($element instanceof Run) {
  359. $str_fontidx = $this->fontHashIndex[$element->getFont()->getHashCode()];
  360. } else {
  361. $str_fontidx = 0;
  362. }
  363. $arrcRun[] = ['strlen' => $str_pos, 'fontidx' => $str_fontidx];
  364. // Position FROM
  365. $str_pos += StringHelper::countCharacters($element->getText(), 'UTF-8');
  366. }
  367. $this->writeRichTextString($row, $column, $cVal->getPlainText(), $xfIndex, $arrcRun);
  368. } else {
  369. switch ($cell->getDatatype()) {
  370. case DataType::TYPE_STRING:
  371. case DataType::TYPE_NULL:
  372. if ($cVal === '' || $cVal === null) {
  373. $this->writeBlank($row, $column, $xfIndex);
  374. } else {
  375. $this->writeString($row, $column, $cVal, $xfIndex);
  376. }
  377. break;
  378. case DataType::TYPE_NUMERIC:
  379. $this->writeNumber($row, $column, $cVal, $xfIndex);
  380. break;
  381. case DataType::TYPE_FORMULA:
  382. $calculatedValue = $this->preCalculateFormulas ?
  383. $cell->getCalculatedValue() : null;
  384. $this->writeFormula($row, $column, $cVal, $xfIndex, $calculatedValue);
  385. break;
  386. case DataType::TYPE_BOOL:
  387. $this->writeBoolErr($row, $column, $cVal, 0, $xfIndex);
  388. break;
  389. case DataType::TYPE_ERROR:
  390. $this->writeBoolErr($row, $column, self::mapErrorCode($cVal), 1, $xfIndex);
  391. break;
  392. }
  393. }
  394. }
  395. // Append
  396. $this->writeMsoDrawing();
  397. // Write WINDOW2 record
  398. $this->writeWindow2();
  399. // Write PLV record
  400. $this->writePageLayoutView();
  401. // Write ZOOM record
  402. $this->writeZoom();
  403. if ($phpSheet->getFreezePane()) {
  404. $this->writePanes();
  405. }
  406. // Write SELECTION record
  407. $this->writeSelection();
  408. // Write MergedCellsTable Record
  409. $this->writeMergedCells();
  410. // Hyperlinks
  411. foreach ($phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) {
  412. list($column, $row) = Coordinate::coordinateFromString($coordinate);
  413. $url = $hyperlink->getUrl();
  414. if (strpos($url, 'sheet://') !== false) {
  415. // internal to current workbook
  416. $url = str_replace('sheet://', 'internal:', $url);
  417. } elseif (preg_match('/^(http:|https:|ftp:|mailto:)/', $url)) {
  418. // URL
  419. } else {
  420. // external (local file)
  421. $url = 'external:' . $url;
  422. }
  423. $this->writeUrl($row - 1, Coordinate::columnIndexFromString($column) - 1, $url);
  424. }
  425. $this->writeDataValidity();
  426. $this->writeSheetLayout();
  427. // Write SHEETPROTECTION record
  428. $this->writeSheetProtection();
  429. $this->writeRangeProtection();
  430. $arrConditionalStyles = $phpSheet->getConditionalStylesCollection();
  431. if (!empty($arrConditionalStyles)) {
  432. $arrConditional = [];
  433. // @todo CFRule & CFHeader
  434. // Write CFHEADER record
  435. $this->writeCFHeader();
  436. // Write ConditionalFormattingTable records
  437. foreach ($arrConditionalStyles as $cellCoordinate => $conditionalStyles) {
  438. foreach ($conditionalStyles as $conditional) {
  439. if ($conditional->getConditionType() == Conditional::CONDITION_EXPRESSION
  440. || $conditional->getConditionType() == Conditional::CONDITION_CELLIS) {
  441. if (!isset($arrConditional[$conditional->getHashCode()])) {
  442. // This hash code has been handled
  443. $arrConditional[$conditional->getHashCode()] = true;
  444. // Write CFRULE record
  445. $this->writeCFRule($conditional);
  446. }
  447. }
  448. }
  449. }
  450. }
  451. $this->storeEof();
  452. }
  453. /**
  454. * Write a cell range address in BIFF8
  455. * always fixed range
  456. * See section 2.5.14 in OpenOffice.org's Documentation of the Microsoft Excel File Format.
  457. *
  458. * @param string $range E.g. 'A1' or 'A1:B6'
  459. *
  460. * @return string Binary data
  461. */
  462. private function writeBIFF8CellRangeAddressFixed($range)
  463. {
  464. $explodes = explode(':', $range);
  465. // extract first cell, e.g. 'A1'
  466. $firstCell = $explodes[0];
  467. // extract last cell, e.g. 'B6'
  468. if (count($explodes) == 1) {
  469. $lastCell = $firstCell;
  470. } else {
  471. $lastCell = $explodes[1];
  472. }
  473. $firstCellCoordinates = Coordinate::coordinateFromString($firstCell); // e.g. array(0, 1)
  474. $lastCellCoordinates = Coordinate::coordinateFromString($lastCell); // e.g. array(1, 6)
  475. return pack('vvvv', $firstCellCoordinates[1] - 1, $lastCellCoordinates[1] - 1, Coordinate::columnIndexFromString($firstCellCoordinates[0]) - 1, Coordinate::columnIndexFromString($lastCellCoordinates[0]) - 1);
  476. }
  477. /**
  478. * Retrieves data from memory in one chunk, or from disk in $buffer
  479. * sized chunks.
  480. *
  481. * @return string The data
  482. */
  483. public function getData()
  484. {
  485. $buffer = 4096;
  486. // Return data stored in memory
  487. if (isset($this->_data)) {
  488. $tmp = $this->_data;
  489. unset($this->_data);
  490. return $tmp;
  491. }
  492. // No data to return
  493. return false;
  494. }
  495. /**
  496. * Set the option to print the row and column headers on the printed page.
  497. *
  498. * @param int $print Whether to print the headers or not. Defaults to 1 (print).
  499. */
  500. public function printRowColHeaders($print = 1)
  501. {
  502. $this->printHeaders = $print;
  503. }
  504. /**
  505. * This method sets the properties for outlining and grouping. The defaults
  506. * correspond to Excel's defaults.
  507. *
  508. * @param bool $visible
  509. * @param bool $symbols_below
  510. * @param bool $symbols_right
  511. * @param bool $auto_style
  512. */
  513. public function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
  514. {
  515. $this->outlineOn = $visible;
  516. $this->outlineBelow = $symbols_below;
  517. $this->outlineRight = $symbols_right;
  518. $this->outlineStyle = $auto_style;
  519. // Ensure this is a boolean vale for Window2
  520. if ($this->outlineOn) {
  521. $this->outlineOn = 1;
  522. }
  523. }
  524. /**
  525. * Write a double to the specified row and column (zero indexed).
  526. * An integer can be written as a double. Excel will display an
  527. * integer. $format is optional.
  528. *
  529. * Returns 0 : normal termination
  530. * -2 : row or column out of range
  531. *
  532. * @param int $row Zero indexed row
  533. * @param int $col Zero indexed column
  534. * @param float $num The number to write
  535. * @param mixed $xfIndex The optional XF format
  536. *
  537. * @return int
  538. */
  539. private function writeNumber($row, $col, $num, $xfIndex)
  540. {
  541. $record = 0x0203; // Record identifier
  542. $length = 0x000E; // Number of bytes to follow
  543. $header = pack('vv', $record, $length);
  544. $data = pack('vvv', $row, $col, $xfIndex);
  545. $xl_double = pack('d', $num);
  546. if (self::getByteOrder()) { // if it's Big Endian
  547. $xl_double = strrev($xl_double);
  548. }
  549. $this->append($header . $data . $xl_double);
  550. return 0;
  551. }
  552. /**
  553. * Write a LABELSST record or a LABEL record. Which one depends on BIFF version.
  554. *
  555. * @param int $row Row index (0-based)
  556. * @param int $col Column index (0-based)
  557. * @param string $str The string
  558. * @param int $xfIndex Index to XF record
  559. */
  560. private function writeString($row, $col, $str, $xfIndex)
  561. {
  562. $this->writeLabelSst($row, $col, $str, $xfIndex);
  563. }
  564. /**
  565. * Write a LABELSST record or a LABEL record. Which one depends on BIFF version
  566. * It differs from writeString by the writing of rich text strings.
  567. *
  568. * @param int $row Row index (0-based)
  569. * @param int $col Column index (0-based)
  570. * @param string $str The string
  571. * @param int $xfIndex The XF format index for the cell
  572. * @param array $arrcRun Index to Font record and characters beginning
  573. */
  574. private function writeRichTextString($row, $col, $str, $xfIndex, $arrcRun)
  575. {
  576. $record = 0x00FD; // Record identifier
  577. $length = 0x000A; // Bytes to follow
  578. $str = StringHelper::UTF8toBIFF8UnicodeShort($str, $arrcRun);
  579. // check if string is already present
  580. if (!isset($this->stringTable[$str])) {
  581. $this->stringTable[$str] = $this->stringUnique++;
  582. }
  583. ++$this->stringTotal;
  584. $header = pack('vv', $record, $length);
  585. $data = pack('vvvV', $row, $col, $xfIndex, $this->stringTable[$str]);
  586. $this->append($header . $data);
  587. }
  588. /**
  589. * Write a string to the specified row and column (zero indexed).
  590. * This is the BIFF8 version (no 255 chars limit).
  591. * $format is optional.
  592. *
  593. * @param int $row Zero indexed row
  594. * @param int $col Zero indexed column
  595. * @param string $str The string to write
  596. * @param mixed $xfIndex The XF format index for the cell
  597. */
  598. private function writeLabelSst($row, $col, $str, $xfIndex)
  599. {
  600. $record = 0x00FD; // Record identifier
  601. $length = 0x000A; // Bytes to follow
  602. $str = StringHelper::UTF8toBIFF8UnicodeLong($str);
  603. // check if string is already present
  604. if (!isset($this->stringTable[$str])) {
  605. $this->stringTable[$str] = $this->stringUnique++;
  606. }
  607. ++$this->stringTotal;
  608. $header = pack('vv', $record, $length);
  609. $data = pack('vvvV', $row, $col, $xfIndex, $this->stringTable[$str]);
  610. $this->append($header . $data);
  611. }
  612. /**
  613. * Write a blank cell to the specified row and column (zero indexed).
  614. * A blank cell is used to specify formatting without adding a string
  615. * or a number.
  616. *
  617. * A blank cell without a format serves no purpose. Therefore, we don't write
  618. * a BLANK record unless a format is specified.
  619. *
  620. * Returns 0 : normal termination (including no format)
  621. * -1 : insufficient number of arguments
  622. * -2 : row or column out of range
  623. *
  624. * @param int $row Zero indexed row
  625. * @param int $col Zero indexed column
  626. * @param mixed $xfIndex The XF format index
  627. *
  628. * @return int
  629. */
  630. public function writeBlank($row, $col, $xfIndex)
  631. {
  632. $record = 0x0201; // Record identifier
  633. $length = 0x0006; // Number of bytes to follow
  634. $header = pack('vv', $record, $length);
  635. $data = pack('vvv', $row, $col, $xfIndex);
  636. $this->append($header . $data);
  637. return 0;
  638. }
  639. /**
  640. * Write a boolean or an error type to the specified row and column (zero indexed).
  641. *
  642. * @param int $row Row index (0-based)
  643. * @param int $col Column index (0-based)
  644. * @param int $value
  645. * @param bool $isError Error or Boolean?
  646. * @param int $xfIndex
  647. *
  648. * @return int
  649. */
  650. private function writeBoolErr($row, $col, $value, $isError, $xfIndex)
  651. {
  652. $record = 0x0205;
  653. $length = 8;
  654. $header = pack('vv', $record, $length);
  655. $data = pack('vvvCC', $row, $col, $xfIndex, $value, $isError);
  656. $this->append($header . $data);
  657. return 0;
  658. }
  659. /**
  660. * Write a formula to the specified row and column (zero indexed).
  661. * The textual representation of the formula is passed to the parser in
  662. * Parser.php which returns a packed binary string.
  663. *
  664. * Returns 0 : normal termination
  665. * -1 : formula errors (bad formula)
  666. * -2 : row or column out of range
  667. *
  668. * @param int $row Zero indexed row
  669. * @param int $col Zero indexed column
  670. * @param string $formula The formula text string
  671. * @param mixed $xfIndex The XF format index
  672. * @param mixed $calculatedValue Calculated value
  673. *
  674. * @return int
  675. */
  676. private function writeFormula($row, $col, $formula, $xfIndex, $calculatedValue)
  677. {
  678. $record = 0x0006; // Record identifier
  679. // Initialize possible additional value for STRING record that should be written after the FORMULA record?
  680. $stringValue = null;
  681. // calculated value
  682. if (isset($calculatedValue)) {
  683. // Since we can't yet get the data type of the calculated value,
  684. // we use best effort to determine data type
  685. if (is_bool($calculatedValue)) {
  686. // Boolean value
  687. $num = pack('CCCvCv', 0x01, 0x00, (int) $calculatedValue, 0x00, 0x00, 0xFFFF);
  688. } elseif (is_int($calculatedValue) || is_float($calculatedValue)) {
  689. // Numeric value
  690. $num = pack('d', $calculatedValue);
  691. } elseif (is_string($calculatedValue)) {
  692. $errorCodes = DataType::getErrorCodes();
  693. if (isset($errorCodes[$calculatedValue])) {
  694. // Error value
  695. $num = pack('CCCvCv', 0x02, 0x00, self::mapErrorCode($calculatedValue), 0x00, 0x00, 0xFFFF);
  696. } elseif ($calculatedValue === '') {
  697. // Empty string (and BIFF8)
  698. $num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
  699. } else {
  700. // Non-empty string value (or empty string BIFF5)
  701. $stringValue = $calculatedValue;
  702. $num = pack('CCCvCv', 0x00, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
  703. }
  704. } else {
  705. // We are really not supposed to reach here
  706. $num = pack('d', 0x00);
  707. }
  708. } else {
  709. $num = pack('d', 0x00);
  710. }
  711. $grbit = 0x03; // Option flags
  712. $unknown = 0x0000; // Must be zero
  713. // Strip the '=' or '@' sign at the beginning of the formula string
  714. if ($formula[0] == '=') {
  715. $formula = substr($formula, 1);
  716. } else {
  717. // Error handling
  718. $this->writeString($row, $col, 'Unrecognised character for formula');
  719. return -1;
  720. }
  721. // Parse the formula using the parser in Parser.php
  722. try {
  723. $error = $this->parser->parse($formula);
  724. $formula = $this->parser->toReversePolish();
  725. $formlen = strlen($formula); // Length of the binary string
  726. $length = 0x16 + $formlen; // Length of the record data
  727. $header = pack('vv', $record, $length);
  728. $data = pack('vvv', $row, $col, $xfIndex)
  729. . $num
  730. . pack('vVv', $grbit, $unknown, $formlen);
  731. $this->append($header . $data . $formula);
  732. // Append also a STRING record if necessary
  733. if ($stringValue !== null) {
  734. $this->writeStringRecord($stringValue);
  735. }
  736. return 0;
  737. } catch (PhpSpreadsheetException $e) {
  738. // do nothing
  739. }
  740. }
  741. /**
  742. * Write a STRING record. This.
  743. *
  744. * @param string $stringValue
  745. */
  746. private function writeStringRecord($stringValue)
  747. {
  748. $record = 0x0207; // Record identifier
  749. $data = StringHelper::UTF8toBIFF8UnicodeLong($stringValue);
  750. $length = strlen($data);
  751. $header = pack('vv', $record, $length);
  752. $this->append($header . $data);
  753. }
  754. /**
  755. * Write a hyperlink.
  756. * This is comprised of two elements: the visible label and
  757. * the invisible link. The visible label is the same as the link unless an
  758. * alternative string is specified. The label is written using the
  759. * writeString() method. Therefore the 255 characters string limit applies.
  760. * $string and $format are optional.
  761. *
  762. * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
  763. * directory url.
  764. *
  765. * Returns 0 : normal termination
  766. * -2 : row or column out of range
  767. * -3 : long string truncated to 255 chars
  768. *
  769. * @param int $row Row
  770. * @param int $col Column
  771. * @param string $url URL string
  772. *
  773. * @return int
  774. */
  775. private function writeUrl($row, $col, $url)
  776. {
  777. // Add start row and col to arg list
  778. return $this->writeUrlRange($row, $col, $row, $col, $url);
  779. }
  780. /**
  781. * This is the more general form of writeUrl(). It allows a hyperlink to be
  782. * written to a range of cells. This function also decides the type of hyperlink
  783. * to be written. These are either, Web (http, ftp, mailto), Internal
  784. * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
  785. *
  786. * @see writeUrl()
  787. *
  788. * @param int $row1 Start row
  789. * @param int $col1 Start column
  790. * @param int $row2 End row
  791. * @param int $col2 End column
  792. * @param string $url URL string
  793. *
  794. * @return int
  795. */
  796. public function writeUrlRange($row1, $col1, $row2, $col2, $url)
  797. {
  798. // Check for internal/external sheet links or default to web link
  799. if (preg_match('[^internal:]', $url)) {
  800. return $this->writeUrlInternal($row1, $col1, $row2, $col2, $url);
  801. }
  802. if (preg_match('[^external:]', $url)) {
  803. return $this->writeUrlExternal($row1, $col1, $row2, $col2, $url);
  804. }
  805. return $this->writeUrlWeb($row1, $col1, $row2, $col2, $url);
  806. }
  807. /**
  808. * Used to write http, ftp and mailto hyperlinks.
  809. * The link type ($options) is 0x03 is the same as absolute dir ref without
  810. * sheet. However it is differentiated by the $unknown2 data stream.
  811. *
  812. * @see writeUrl()
  813. *
  814. * @param int $row1 Start row
  815. * @param int $col1 Start column
  816. * @param int $row2 End row
  817. * @param int $col2 End column
  818. * @param string $url URL string
  819. *
  820. * @return int
  821. */
  822. public function writeUrlWeb($row1, $col1, $row2, $col2, $url)
  823. {
  824. $record = 0x01B8; // Record identifier
  825. $length = 0x00000; // Bytes to follow
  826. // Pack the undocumented parts of the hyperlink stream
  827. $unknown1 = pack('H*', 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
  828. $unknown2 = pack('H*', 'E0C9EA79F9BACE118C8200AA004BA90B');
  829. // Pack the option flags
  830. $options = pack('V', 0x03);
  831. // Convert URL to a null terminated wchar string
  832. $url = implode("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
  833. $url = $url . "\0\0\0";
  834. // Pack the length of the URL
  835. $url_len = pack('V', strlen($url));
  836. // Calculate the data length
  837. $length = 0x34 + strlen($url);
  838. // Pack the header data
  839. $header = pack('vv', $record, $length);
  840. $data = pack('vvvv', $row1, $row2, $col1, $col2);
  841. // Write the packed data
  842. $this->append($header . $data . $unknown1 . $options . $unknown2 . $url_len . $url);
  843. return 0;
  844. }
  845. /**
  846. * Used to write internal reference hyperlinks such as "Sheet1!A1".
  847. *
  848. * @see writeUrl()
  849. *
  850. * @param int $row1 Start row
  851. * @param int $col1 Start column
  852. * @param int $row2 End row
  853. * @param int $col2 End column
  854. * @param string $url URL string
  855. *
  856. * @return int
  857. */
  858. public function writeUrlInternal($row1, $col1, $row2, $col2, $url)
  859. {
  860. $record = 0x01B8; // Record identifier
  861. $length = 0x00000; // Bytes to follow
  862. // Strip URL type
  863. $url = preg_replace('/^internal:/', '', $url);
  864. // Pack the undocumented parts of the hyperlink stream
  865. $unknown1 = pack('H*', 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
  866. // Pack the option flags
  867. $options = pack('V', 0x08);
  868. // Convert the URL type and to a null terminated wchar string
  869. $url .= "\0";
  870. // character count
  871. $url_len = StringHelper::countCharacters($url);
  872. $url_len = pack('V', $url_len);
  873. $url = StringHelper::convertEncoding($url, 'UTF-16LE', 'UTF-8');
  874. // Calculate the data length
  875. $length = 0x24 + strlen($url);
  876. // Pack the header data
  877. $header = pack('vv', $record, $length);
  878. $data = pack('vvvv', $row1, $row2, $col1, $col2);
  879. // Write the packed data
  880. $this->append($header . $data . $unknown1 . $options . $url_len . $url);
  881. return 0;
  882. }
  883. /**
  884. * Write links to external directory names such as 'c:\foo.xls',
  885. * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
  886. *
  887. * Note: Excel writes some relative links with the $dir_long string. We ignore
  888. * these cases for the sake of simpler code.
  889. *
  890. * @see writeUrl()
  891. *
  892. * @param int $row1 Start row
  893. * @param int $col1 Start column
  894. * @param int $row2 End row
  895. * @param int $col2 End column
  896. * @param string $url URL string
  897. *
  898. * @return int
  899. */
  900. public function writeUrlExternal($row1, $col1, $row2, $col2, $url)
  901. {
  902. // Network drives are different. We will handle them separately
  903. // MS/Novell network drives and shares start with \\
  904. if (preg_match('[^external:\\\\]', $url)) {
  905. return; //($this->writeUrlExternal_net($row1, $col1, $row2, $col2, $url, $str, $format));
  906. }
  907. $record = 0x01B8; // Record identifier
  908. $length = 0x00000; // Bytes to follow
  909. // Strip URL type and change Unix dir separator to Dos style (if needed)
  910. //
  911. $url = preg_replace('/^external:/', '', $url);
  912. $url = preg_replace('/\//', '\\', $url);
  913. // Determine if the link is relative or absolute:
  914. // relative if link contains no dir separator, "somefile.xls"
  915. // relative if link starts with up-dir, "..\..\somefile.xls"
  916. // otherwise, absolute
  917. $absolute = 0x00; // relative path
  918. if (preg_match('/^[A-Z]:/', $url)) {
  919. $absolute = 0x02; // absolute path on Windows, e.g. C:\...
  920. }
  921. $link_type = 0x01 | $absolute;
  922. // Determine if the link contains a sheet reference and change some of the
  923. // parameters accordingly.
  924. // Split the dir name and sheet name (if it exists)
  925. $dir_long = $url;
  926. if (preg_match('/\\#/', $url)) {
  927. $link_type |= 0x08;
  928. }
  929. // Pack the link type
  930. $link_type = pack('V', $link_type);
  931. // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
  932. $up_count = preg_match_all("/\\.\\.\\\/", $dir_long, $useless);
  933. $up_count = pack('v', $up_count);
  934. // Store the short dos dir name (null terminated)
  935. $dir_short = preg_replace("/\\.\\.\\\/", '', $dir_long) . "\0";
  936. // Store the long dir name as a wchar string (non-null terminated)
  937. $dir_long = $dir_long . "\0";
  938. // Pack the lengths of the dir strings
  939. $dir_short_len = pack('V', strlen($dir_short));
  940. $dir_long_len = pack('V', strlen($dir_long));
  941. $stream_len = pack('V', 0); //strlen($dir_long) + 0x06);
  942. // Pack the undocumented parts of the hyperlink stream
  943. $unknown1 = pack('H*', 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
  944. $unknown2 = pack('H*', '0303000000000000C000000000000046');
  945. $unknown3 = pack('H*', 'FFFFADDE000000000000000000000000000000000000000');
  946. $unknown4 = pack('v', 0x03);
  947. // Pack the main data stream
  948. $data = pack('vvvv', $row1, $row2, $col1, $col2) .
  949. $unknown1 .
  950. $link_type .
  951. $unknown2 .
  952. $up_count .
  953. $dir_short_len .
  954. $dir_short .
  955. $unknown3 .
  956. $stream_len; /*.
  957. $dir_long_len .
  958. $unknown4 .
  959. $dir_long .
  960. $sheet_len .
  961. $sheet ;*/
  962. // Pack the header data
  963. $length = strlen($data);
  964. $header = pack('vv', $record, $length);
  965. // Write the packed data
  966. $this->append($header . $data);
  967. return 0;
  968. }
  969. /**
  970. * This method is used to set the height and format for a row.
  971. *
  972. * @param int $row The row to set
  973. * @param int $height Height we are giving to the row.
  974. * Use null to set XF without setting height
  975. * @param int $xfIndex The optional cell style Xf index to apply to the columns
  976. * @param bool $hidden The optional hidden attribute
  977. * @param int $level The optional outline level for row, in range [0,7]
  978. */
  979. private function writeRow($row, $height, $xfIndex, $hidden = false, $level = 0)
  980. {
  981. $record = 0x0208; // Record identifier
  982. $length = 0x0010; // Number of bytes to follow
  983. $colMic = 0x0000; // First defined column
  984. $colMac = 0x0000; // Last defined column
  985. $irwMac = 0x0000; // Used by Excel to optimise loading
  986. $reserved = 0x0000; // Reserved
  987. $grbit = 0x0000; // Option flags
  988. $ixfe = $xfIndex;
  989. if ($height < 0) {
  990. $height = null;
  991. }
  992. // Use writeRow($row, null, $XF) to set XF format without setting height
  993. if ($height != null) {
  994. $miyRw = $height * 20; // row height
  995. } else {
  996. $miyRw = 0xff; // default row height is 256
  997. }
  998. // Set the options flags. fUnsynced is used to show that the font and row
  999. // heights are not compatible. This is usually the case for WriteExcel.
  1000. // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
  1001. // is collapsed. Instead it is used to indicate that the previous row is
  1002. // collapsed. The zero height flag, 0x20, is used to collapse a row.
  1003. $grbit |= $level;
  1004. if ($hidden) {
  1005. $grbit |= 0x0030;
  1006. }
  1007. if ($height !== null) {
  1008. $grbit |= 0x0040; // fUnsynced
  1009. }
  1010. if ($xfIndex !== 0xF) {
  1011. $grbit |= 0x0080;
  1012. }
  1013. $grbit |= 0x0100;
  1014. $header = pack('vv', $record, $length);
  1015. $data = pack('vvvvvvvv', $row, $colMic, $colMac, $miyRw, $irwMac, $reserved, $grbit, $ixfe);
  1016. $this->append($header . $data);
  1017. }
  1018. /**
  1019. * Writes Excel DIMENSIONS to define the area in which there is data.
  1020. */
  1021. private function writeDimensions()
  1022. {
  1023. $record = 0x0200; // Record identifier
  1024. $length = 0x000E;
  1025. $data = pack('VVvvv', $this->firstRowIndex, $this->lastRowIndex + 1, $this->firstColumnIndex, $this->lastColumnIndex + 1, 0x0000); // reserved
  1026. $header = pack('vv', $record, $length);
  1027. $this->append($header . $data);
  1028. }
  1029. /**
  1030. * Write BIFF record Window2.
  1031. */
  1032. private function writeWindow2()
  1033. {
  1034. $record = 0x023E; // Record identifier
  1035. $length = 0x0012;
  1036. $grbit = 0x00B6; // Option flags
  1037. $rwTop = 0x0000; // Top row visible in window
  1038. $colLeft = 0x0000; // Leftmost column visible in window
  1039. // The options flags that comprise $grbit
  1040. $fDspFmla = 0; // 0 - bit
  1041. $fDspGrid = $this->phpSheet->getShowGridlines() ? 1 : 0; // 1
  1042. $fDspRwCol = $this->phpSheet->getShowRowColHeaders() ? 1 : 0; // 2
  1043. $fFrozen = $this->phpSheet->getFreezePane() ? 1 : 0; // 3
  1044. $fDspZeros = 1; // 4
  1045. $fDefaultHdr = 1; // 5
  1046. $fArabic = $this->phpSheet->getRightToLeft() ? 1 : 0; // 6
  1047. $fDspGuts = $this->outlineOn; // 7
  1048. $fFrozenNoSplit = 0; // 0 - bit
  1049. // no support in PhpSpreadsheet for selected sheet, therefore sheet is only selected if it is the active sheet
  1050. $fSelected = ($this->phpSheet === $this->phpSheet->getParent()->getActiveSheet()) ? 1 : 0;
  1051. $fPaged = 1; // 2
  1052. $fPageBreakPreview = $this->phpSheet->getSheetView()->getView() === SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW;
  1053. $grbit = $fDspFmla;
  1054. $grbit |= $fDspGrid << 1;
  1055. $grbit |= $fDspRwCol << 2;
  1056. $grbit |= $fFrozen << 3;
  1057. $grbit |= $fDspZeros << 4;
  1058. $grbit |= $fDefaultHdr << 5;
  1059. $grbit |= $fArabic << 6;
  1060. $grbit |= $fDspGuts << 7;
  1061. $grbit |= $fFrozenNoSplit << 8;
  1062. $grbit |= $fSelected << 9;
  1063. $grbit |= $fPaged << 10;
  1064. $grbit |= $fPageBreakPreview << 11;
  1065. $header = pack('vv', $record, $length);
  1066. $data = pack('vvv', $grbit, $rwTop, $colLeft);
  1067. // FIXME !!!
  1068. $rgbHdr = 0x0040; // Row/column heading and gridline color index
  1069. $zoom_factor_page_break = ($fPageBreakPreview ? $this->phpSheet->getSheetView()->getZoomScale() : 0x0000);
  1070. $zoom_factor_normal = $this->phpSheet->getSheetView()->getZoomScaleNormal();
  1071. $data .= pack('vvvvV', $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
  1072. $this->append($header . $data);
  1073. }
  1074. /**
  1075. * Write BIFF record DEFAULTROWHEIGHT.
  1076. */
  1077. private function writeDefaultRowHeight()
  1078. {
  1079. $defaultRowHeight = $this->phpSheet->getDefaultRowDimension()->getRowHeight();
  1080. if ($defaultRowHeight < 0) {
  1081. return;
  1082. }
  1083. // convert to twips
  1084. $defaultRowHeight = (int) 20 * $defaultRowHeight;
  1085. $record = 0x0225; // Record identifier
  1086. $length = 0x0004; // Number of bytes to follow
  1087. $header = pack('vv', $record, $length);
  1088. $data = pack('vv', 1, $defaultRowHeight);
  1089. $this->append($header . $data);
  1090. }
  1091. /**
  1092. * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
  1093. */
  1094. private function writeDefcol()
  1095. {
  1096. $defaultColWidth = 8;
  1097. $record = 0x0055; // Record identifier
  1098. $length = 0x0002; // Number of bytes to follow
  1099. $header = pack('vv', $record, $length);
  1100. $data = pack('v', $defaultColWidth);
  1101. $this->append($header . $data);
  1102. }
  1103. /**
  1104. * Write BIFF record COLINFO to define column widths.
  1105. *
  1106. * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
  1107. * length record.
  1108. *
  1109. * @param array $col_array This is the only parameter received and is composed of the following:
  1110. * 0 => First formatted column,
  1111. * 1 => Last formatted column,
  1112. * 2 => Col width (8.43 is Excel default),
  1113. * 3 => The optional XF format of the column,
  1114. * 4 => Option flags.
  1115. * 5 => Optional outline level
  1116. */
  1117. private function writeColinfo($col_array)
  1118. {
  1119. if (isset($col_array[0])) {
  1120. $colFirst = $col_array[0];
  1121. }
  1122. if (isset($col_array[1])) {
  1123. $colLast = $col_array[1];
  1124. }
  1125. if (isset($col_array[2])) {
  1126. $coldx = $col_array[2];
  1127. } else {
  1128. $coldx = 8.43;
  1129. }
  1130. if (isset($col_array[3])) {
  1131. $xfIndex = $col_array[3];
  1132. } else {
  1133. $xfIndex = 15;
  1134. }
  1135. if (isset($col_array[4])) {
  1136. $grbit = $col_array[4];
  1137. } else {
  1138. $grbit = 0;
  1139. }
  1140. if (isset($col_array[5])) {
  1141. $level = $col_array[5];
  1142. } else {
  1143. $level = 0;
  1144. }
  1145. $record = 0x007D; // Record identifier
  1146. $length = 0x000C; // Number of bytes to follow
  1147. $coldx *= 256; // Convert to units of 1/256 of a char
  1148. $ixfe = $xfIndex;
  1149. $reserved = 0x0000; // Reserved
  1150. $level = max(0, min($level, 7));
  1151. $grbit |= $level << 8;
  1152. $header = pack('vv', $record, $length);
  1153. $data = pack('vvvvvv', $colFirst, $colLast, $coldx, $ixfe, $grbit, $reserved);
  1154. $this->append($header . $data);
  1155. }
  1156. /**
  1157. * Write BIFF record SELECTION.
  1158. */
  1159. private function writeSelection()
  1160. {
  1161. // look up the selected cell range
  1162. $selectedCells = Coordinate::splitRange($this->phpSheet->getSelectedCells());
  1163. $selectedCells = $selectedCells[0];
  1164. if (count($selectedCells) == 2) {
  1165. list($first, $last) = $selectedCells;
  1166. } else {
  1167. $first = $selectedCells[0];
  1168. $last = $selectedCells[0];
  1169. }
  1170. list($colFirst, $rwFirst) = Coordinate::coordinateFromString($first);
  1171. $colFirst = Coordinate::columnIndexFromString($colFirst) - 1; // base 0 column index
  1172. --$rwFirst; // base 0 row index
  1173. list($colLast, $rwLast) = Coordinate::coordinateFromString($last);
  1174. $colLast = Coordinate::columnIndexFromString($colLast) - 1; // base 0 column index
  1175. --$rwLast; // base 0 row index
  1176. // make sure we are not out of bounds
  1177. $colFirst = min($colFirst, 255);
  1178. $colLast = min($colLast, 255);
  1179. $rwFirst = min($rwFirst, 65535);
  1180. $rwLast = min($rwLast, 65535);
  1181. $record = 0x001D; // Record identifier
  1182. $length = 0x000F; // Number of bytes to follow
  1183. $pnn = $this->activePane; // Pane position
  1184. $rwAct = $rwFirst; // Active row
  1185. $colAct = $colFirst; // Active column
  1186. $irefAct = 0; // Active cell ref
  1187. $cref = 1; // Number of refs
  1188. if (!isset($rwLast)) {
  1189. $rwLast = $rwFirst; // Last row in reference
  1190. }
  1191. if (!isset($colLast)) {
  1192. $colLast = $colFirst; // Last col in reference
  1193. }
  1194. // Swap last row/col for first row/col as necessary
  1195. if ($rwFirst > $rwLast) {
  1196. list($rwFirst, $rwLast) = [$rwLast, $rwFirst];
  1197. }
  1198. if ($colFirst > $colLast) {
  1199. list($colFirst, $colLast) = [$colLast, $colFirst];
  1200. }
  1201. $header = pack('vv', $record, $length);
  1202. $data = pack('CvvvvvvCC', $pnn, $rwAct, $colAct, $irefAct, $cref, $rwFirst, $rwLast, $colFirst, $colLast);
  1203. $this->append($header . $data);
  1204. }
  1205. /**
  1206. * Store the MERGEDCELLS records for all ranges of merged cells.
  1207. */
  1208. private function writeMergedCells()
  1209. {
  1210. $mergeCells = $this->phpSheet->getMergeCells();
  1211. $countMergeCells = count($mergeCells);
  1212. if ($countMergeCells == 0) {
  1213. return;
  1214. }
  1215. // maximum allowed number of merged cells per record
  1216. $maxCountMergeCellsPerRecord = 1027;
  1217. // record identifier
  1218. $record = 0x00E5;
  1219. // counter for total number of merged cells treated so far by the writer
  1220. $i = 0;
  1221. // counter for number of merged cells written in record currently being written
  1222. $j = 0;
  1223. // initialize record data
  1224. $recordData = '';
  1225. // loop through the merged cells
  1226. foreach ($mergeCells as $mergeCell) {
  1227. ++$i;
  1228. ++$j;
  1229. // extract the row and column indexes
  1230. $range = Coordinate::splitRange($mergeCell);
  1231. list($first, $last) = $range[0];
  1232. list($firstColumn, $firstRow) = Coordinate::coordinateFromString($first);
  1233. list($lastColumn, $lastRow) = Coordinate::coordinateFromString($last);
  1234. $recordData .= pack('vvvv', $firstRow - 1, $lastRow - 1, Coordinate::columnIndexFromString($firstColumn) - 1, Coordinate::columnIndexFromString($lastColumn) - 1);
  1235. // flush record if we have reached limit for number of merged cells, or reached final merged cell
  1236. if ($j == $maxCountMergeCellsPerRecord or $i == $countMergeCells) {
  1237. $recordData = pack('v', $j) . $recordData;
  1238. $length = strlen($recordData);
  1239. $header = pack('vv', $record, $length);
  1240. $this->append($header . $recordData);
  1241. // initialize for next record, if any
  1242. $recordData = '';
  1243. $j = 0;
  1244. }
  1245. }
  1246. }
  1247. /**
  1248. * Write SHEETLAYOUT record.
  1249. */
  1250. private function writeSheetLayout()
  1251. {
  1252. if (!$this->phpSheet->isTabColorSet()) {
  1253. return;
  1254. }
  1255. $recordData = pack(
  1256. 'vvVVVvv',
  1257. 0x0862,
  1258. 0x0000, // unused
  1259. 0x00000000, // unused
  1260. 0x00000000, // unused
  1261. 0x00000014, // size of record data
  1262. $this->colors[$this->phpSheet->getTabColor()->getRGB()], // color index
  1263. 0x0000 // unused
  1264. );
  1265. $length = strlen($recordData);
  1266. $record = 0x0862; // Record identifier
  1267. $header = pack('vv', $record, $length);
  1268. $this->append($header . $recordData);
  1269. }
  1270. /**
  1271. * Write SHEETPROTECTION.
  1272. */
  1273. private function writeSheetProtection()
  1274. {
  1275. // record identifier
  1276. $record = 0x0867;
  1277. // prepare options
  1278. $options = (int) !$this->phpSheet->getProtection()->getObjects()
  1279. | (int) !$this->phpSheet->getProtection()->getScenarios() << 1
  1280. | (int) !$this->phpSheet->getProtection()->getFormatCells() << 2
  1281. | (int) !$this->phpSheet->getProtection()->getFormatColumns() << 3
  1282. | (int) !$this->phpSheet->getProtection()->getFormatRows() << 4
  1283. | (int) !$this->phpSheet->getProtection()->getInsertColumns() << 5
  1284. | (int) !$this->phpSheet->getProtection()->getInsertRows() << 6
  1285. | (int) !$this->phpSheet->getProtection()->getInsertHyperlinks() << 7
  1286. | (int) !$this->phpSheet->getProtection()->getDeleteColumns() << 8
  1287. | (int) !$this->phpSheet->getProtection()->getDeleteRows() << 9
  1288. | (int) !$this->phpSheet->getProtection()->getSelectLockedCells() << 10
  1289. | (int) !$this->phpSheet->getProtection()->getSort() << 11
  1290. | (int) !$this->phpSheet->getProtection()->getAutoFilter() << 12
  1291. | (int) !$this->phpSheet->getProtection()->getPivotTables() << 13
  1292. | (int) !$this->phpSheet->getProtection()->getSelectUnlockedCells() << 14;
  1293. // record data
  1294. $recordData = pack(
  1295. 'vVVCVVvv',
  1296. 0x0867, // repeated record identifier
  1297. 0x0000, // not used
  1298. 0x0000, // not used
  1299. 0x00, // not used
  1300. 0x01000200, // unknown data
  1301. 0xFFFFFFFF, // unknown data
  1302. $options, // options
  1303. 0x0000 // not used
  1304. );
  1305. $length = strlen($recordData);
  1306. $header = pack('vv', $record, $length);
  1307. $this->append($header . $recordData);
  1308. }
  1309. /**
  1310. * Write BIFF record RANGEPROTECTION.
  1311. *
  1312. * Openoffice.org's Documentaion of the Microsoft Excel File Format uses term RANGEPROTECTION for these records
  1313. * Microsoft Office Excel 97-2007 Binary File Format Specification uses term FEAT for these records
  1314. */
  1315. private function writeRangeProtection()
  1316. {
  1317. foreach ($this->phpSheet->getProtectedCells() as $range => $password) {
  1318. // number of ranges, e.g. 'A1:B3 C20:D25'
  1319. $cellRanges = explode(' ', $range);
  1320. $cref = count($cellRanges);
  1321. $recordData = pack(
  1322. 'vvVVvCVvVv',
  1323. 0x0868,
  1324. 0x00,
  1325. 0x0000,
  1326. 0x0000,
  1327. 0x02,
  1328. 0x0,
  1329. 0x0000,
  1330. $cref,
  1331. 0x0000,
  1332. 0x00
  1333. );
  1334. foreach ($cellRanges as $cellRange) {
  1335. $recordData .= $this->writeBIFF8CellRangeAddressFixed($cellRange);
  1336. }
  1337. // the rgbFeat structure
  1338. $recordData .= pack(
  1339. 'VV',
  1340. 0x0000,
  1341. hexdec($password)
  1342. );
  1343. $recordData .= StringHelper::UTF8toBIFF8UnicodeLong('p' . md5($recordData));
  1344. $length = strlen($recordData);
  1345. $record = 0x0868; // Record identifier
  1346. $header = pack('vv', $record, $length);
  1347. $this->append($header . $recordData);
  1348. }
  1349. }
  1350. /**
  1351. * Writes the Excel BIFF PANE record.
  1352. * The panes can either be frozen or thawed (unfrozen).
  1353. * Frozen panes are specified in terms of an integer number of rows and columns.
  1354. * Thawed panes are specified in terms of Excel's units for rows and columns.
  1355. */
  1356. private function writePanes()
  1357. {
  1358. $panes = [];
  1359. if ($this->phpSheet->getFreezePane()) {
  1360. list($column, $row) = Coordinate::coordinateFromString($this->phpSheet->getFreezePane());
  1361. $panes[0] = Coordinate::columnIndexFromString($column) - 1;
  1362. $panes[1] = $row - 1;
  1363. list($leftMostColumn, $topRow) = Coordinate::coordinateFromString($this->phpSheet->getTopLeftCell());
  1364. //Coordinates are zero-based in xls files
  1365. $panes[2] = $topRow - 1;
  1366. $panes[3] = Coordinate::columnIndexFromString($leftMostColumn) - 1;
  1367. } else {
  1368. // thaw panes
  1369. return;
  1370. }
  1371. $x = isset($panes[0]) ? $panes[0] : null;
  1372. $y = isset($panes[1]) ? $panes[1] : null;
  1373. $rwTop = isset($panes[2]) ? $panes[2] : null;
  1374. $colLeft = isset($panes[3]) ? $panes[3] : null;
  1375. if (count($panes) > 4) { // if Active pane was received
  1376. $pnnAct = $panes[4];
  1377. } else {
  1378. $pnnAct = null;
  1379. }
  1380. $record = 0x0041; // Record identifier
  1381. $length = 0x000A; // Number of bytes to follow
  1382. // Code specific to frozen or thawed panes.
  1383. if ($this->phpSheet->getFreezePane()) {
  1384. // Set default values for $rwTop and $colLeft
  1385. if (!isset($rwTop)) {
  1386. $rwTop = $y;
  1387. }
  1388. if (!isset($colLeft)) {
  1389. $colLeft = $x;
  1390. }
  1391. } else {
  1392. // Set default values for $rwTop and $colLeft
  1393. if (!isset($rwTop)) {
  1394. $rwTop = 0;
  1395. }
  1396. if (!isset($colLeft)) {
  1397. $colLeft = 0;
  1398. }
  1399. // Convert Excel's row and column units to the internal units.
  1400. // The default row height is 12.75
  1401. // The default column width is 8.43
  1402. // The following slope and intersection values were interpolated.
  1403. //
  1404. $y = 20 * $y + 255;
  1405. $x = 113.879 * $x + 390;
  1406. }
  1407. // Determine which pane should be active. There is also the undocumented
  1408. // option to override this should it be necessary: may be removed later.
  1409. //
  1410. if (!isset($pnnAct)) {
  1411. if ($x != 0 && $y != 0) {
  1412. $pnnAct = 0; // Bottom right
  1413. }
  1414. if ($x != 0 && $y == 0) {
  1415. $pnnAct = 1; // Top right
  1416. }
  1417. if ($x == 0 && $y != 0) {
  1418. $pnnAct = 2; // Bottom left
  1419. }
  1420. if ($x == 0 && $y == 0) {
  1421. $pnnAct = 3; // Top left
  1422. }
  1423. }
  1424. $this->activePane = $pnnAct; // Used in writeSelection
  1425. $header = pack('vv', $record, $length);
  1426. $data = pack('vvvvv', $x, $y, $rwTop, $colLeft, $pnnAct);
  1427. $this->append($header . $data);
  1428. }
  1429. /**
  1430. * Store the page setup SETUP BIFF record.
  1431. */
  1432. private function writeSetup()
  1433. {
  1434. $record = 0x00A1; // Record identifier
  1435. $length = 0x0022; // Number of bytes to follow
  1436. $iPaperSize = $this->phpSheet->getPageSetup()->getPaperSize(); // Paper size
  1437. $iScale = $this->phpSheet->getPageSetup()->getScale() ?
  1438. $this->phpSheet->getPageSetup()->getScale() : 100; // Print scaling factor
  1439. $iPageStart = 0x01; // Starting page number
  1440. $iFitWidth = (int) $this->phpSheet->getPageSetup()->getFitToWidth(); // Fit to number of pages wide
  1441. $iFitHeight = (int) $this->phpSheet->getPageSetup()->getFitToHeight(); // Fit to number of pages high
  1442. $grbit = 0x00; // Option flags
  1443. $iRes = 0x0258; // Print resolution
  1444. $iVRes = 0x0258; // Vertical print resolution
  1445. $numHdr = $this->phpSheet->getPageMargins()->getHeader(); // Header Margin
  1446. $numFtr = $this->phpSheet->getPageMargins()->getFooter(); // Footer Margin
  1447. $iCopies = 0x01; // Number of copies
  1448. $fLeftToRight = 0x0; // Print over then down
  1449. // Page orientation
  1450. $fLandscape = ($this->phpSheet->getPageSetup()->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE) ?
  1451. 0x0 : 0x1;
  1452. $fNoPls = 0x0; // Setup not read from printer
  1453. $fNoColor = 0x0; // Print black and white
  1454. $fDraft = 0x0; // Print draft quality
  1455. $fNotes = 0x0; // Print notes
  1456. $fNoOrient = 0x0; // Orientation not set
  1457. $fUsePage = 0x0; // Use custom starting page
  1458. $grbit = $fLeftToRight;
  1459. $grbit |= $fLandscape << 1;
  1460. $grbit |= $fNoPls << 2;
  1461. $grbit |= $fNoColor << 3;
  1462. $grbit |= $fDraft << 4;
  1463. $grbit |= $fNotes << 5;
  1464. $grbit |= $fNoOrient << 6;
  1465. $grbit |= $fUsePage << 7;
  1466. $numHdr = pack('d', $numHdr);
  1467. $numFtr = pack('d', $numFtr);
  1468. if (self::getByteOrder()) { // if it's Big Endian
  1469. $numHdr = strrev($numHdr);
  1470. $numFtr = strrev($numFtr);
  1471. }
  1472. $header = pack('vv', $record, $length);
  1473. $data1 = pack('vvvvvvvv', $iPaperSize, $iScale, $iPageStart, $iFitWidth, $iFitHeight, $grbit, $iRes, $iVRes);
  1474. $data2 = $numHdr . $numFtr;
  1475. $data3 = pack('v', $iCopies);
  1476. $this->append($header . $data1 . $data2 . $data3);
  1477. }
  1478. /**
  1479. * Store the header caption BIFF record.
  1480. */
  1481. private function writeHeader()
  1482. {
  1483. $record = 0x0014; // Record identifier
  1484. /* removing for now
  1485. // need to fix character count (multibyte!)
  1486. if (strlen($this->phpSheet->getHeaderFooter()->getOddHeader()) <= 255) {
  1487. $str = $this->phpSheet->getHeaderFooter()->getOddHeader(); // header string
  1488. } else {
  1489. $str = '';
  1490. }
  1491. */
  1492. $recordData = StringHelper::UTF8toBIFF8UnicodeLong($this->phpSheet->getHeaderFooter()->getOddHeader());
  1493. $length = strlen($recordData);
  1494. $header = pack('vv', $record, $length);
  1495. $this->append($header . $recordData);
  1496. }
  1497. /**
  1498. * Store the footer caption BIFF record.
  1499. */
  1500. private function writeFooter()
  1501. {
  1502. $record = 0x0015; // Record identifier
  1503. /* removing for now
  1504. // need to fix character count (multibyte!)
  1505. if (strlen($this->phpSheet->getHeaderFooter()->getOddFooter()) <= 255) {
  1506. $str = $this->phpSheet->getHeaderFooter()->getOddFooter();
  1507. } else {
  1508. $str = '';
  1509. }
  1510. */
  1511. $recordData = StringHelper::UTF8toBIFF8UnicodeLong($this->phpSheet->getHeaderFooter()->getOddFooter());
  1512. $length = strlen($recordData);
  1513. $header = pack('vv', $record, $length);
  1514. $this->append($header . $recordData);
  1515. }
  1516. /**
  1517. * Store the horizontal centering HCENTER BIFF record.
  1518. */
  1519. private function writeHcenter()
  1520. {
  1521. $record = 0x0083; // Record identifier
  1522. $length = 0x0002; // Bytes to follow
  1523. $fHCenter = $this->phpSheet->getPageSetup()->getHorizontalCentered() ? 1 : 0; // Horizontal centering
  1524. $header = pack('vv', $record, $length);
  1525. $data = pack('v', $fHCenter);
  1526. $this->append($header . $data);
  1527. }
  1528. /**
  1529. * Store the vertical centering VCENTER BIFF record.
  1530. */
  1531. private function writeVcenter()
  1532. {
  1533. $record = 0x0084; // Record identifier
  1534. $length = 0x0002; // Bytes to follow
  1535. $fVCenter = $this->phpSheet->getPageSetup()->getVerticalCentered() ? 1 : 0; // Horizontal centering
  1536. $header = pack('vv', $record, $length);
  1537. $data = pack('v', $fVCenter);
  1538. $this->append($header . $data);
  1539. }
  1540. /**
  1541. * Store the LEFTMARGIN BIFF record.
  1542. */
  1543. private function writeMarginLeft()
  1544. {
  1545. $record = 0x0026; // Record identifier
  1546. $length = 0x0008; // Bytes to follow
  1547. $margin = $this->phpSheet->getPageMargins()->getLeft(); // Margin in inches
  1548. $header = pack('vv', $record, $length);
  1549. $data = pack('d', $margin);
  1550. if (self::getByteOrder()) { // if it's Big Endian
  1551. $data = strrev($data);
  1552. }
  1553. $this->append($header . $data);
  1554. }
  1555. /**
  1556. * Store the RIGHTMARGIN BIFF record.
  1557. */
  1558. private function writeMarginRight()
  1559. {
  1560. $record = 0x0027; // Record identifier
  1561. $length = 0x0008; // Bytes to follow
  1562. $margin = $this->phpSheet->getPageMargins()->getRight(); // Margin in inches
  1563. $header = pack('vv', $record, $length);
  1564. $data = pack('d', $margin);
  1565. if (self::getByteOrder()) { // if it's Big Endian
  1566. $data = strrev($data);
  1567. }
  1568. $this->append($header . $data);
  1569. }
  1570. /**
  1571. * Store the TOPMARGIN BIFF record.
  1572. */
  1573. private function writeMarginTop()
  1574. {
  1575. $record = 0x0028; // Record identifier
  1576. $length = 0x0008; // Bytes to follow
  1577. $margin = $this->phpSheet->getPageMargins()->getTop(); // Margin in inches
  1578. $header = pack('vv', $record, $length);
  1579. $data = pack('d', $margin);
  1580. if (self::getByteOrder()) { // if it's Big Endian
  1581. $data = strrev($data);
  1582. }
  1583. $this->append($header . $data);
  1584. }
  1585. /**
  1586. * Store the BOTTOMMARGIN BIFF record.
  1587. */
  1588. private function writeMarginBottom()
  1589. {
  1590. $record = 0x0029; // Record identifier
  1591. $length = 0x0008; // Bytes to follow
  1592. $margin = $this->phpSheet->getPageMargins()->getBottom(); // Margin in inches
  1593. $header = pack('vv', $record, $length);
  1594. $data = pack('d', $margin);
  1595. if (self::getByteOrder()) { // if it's Big Endian
  1596. $data = strrev($data);
  1597. }
  1598. $this->append($header . $data);
  1599. }
  1600. /**
  1601. * Write the PRINTHEADERS BIFF record.
  1602. */
  1603. private function writePrintHeaders()
  1604. {
  1605. $record = 0x002a; // Record identifier
  1606. $length = 0x0002; // Bytes to follow
  1607. $fPrintRwCol = $this->printHeaders; // Boolean flag
  1608. $header = pack('vv', $record, $length);
  1609. $data = pack('v', $fPrintRwCol);
  1610. $this->append($header . $data);
  1611. }
  1612. /**
  1613. * Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
  1614. * GRIDSET record.
  1615. */
  1616. private function writePrintGridlines()
  1617. {
  1618. $record = 0x002b; // Record identifier
  1619. $length = 0x0002; // Bytes to follow
  1620. $fPrintGrid = $this->phpSheet->getPrintGridlines() ? 1 : 0; // Boolean flag
  1621. $header = pack('vv', $record, $length);
  1622. $data = pack('v', $fPrintGrid);
  1623. $this->append($header . $data);
  1624. }
  1625. /**
  1626. * Write the GRIDSET BIFF record. Must be used in conjunction with the
  1627. * PRINTGRIDLINES record.
  1628. */
  1629. private function writeGridset()
  1630. {
  1631. $record = 0x0082; // Record identifier
  1632. $length = 0x0002; // Bytes to follow
  1633. $fGridSet = !$this->phpSheet->getPrintGridlines(); // Boolean flag
  1634. $header = pack('vv', $record, $length);
  1635. $data = pack('v', $fGridSet);
  1636. $this->append($header . $data);
  1637. }
  1638. /**
  1639. * Write the AUTOFILTERINFO BIFF record. This is used to configure the number of autofilter select used in the sheet.
  1640. */
  1641. private function writeAutoFilterInfo()
  1642. {
  1643. $record = 0x009D; // Record identifier
  1644. $length = 0x0002; // Bytes to follow
  1645. $rangeBounds = Coordinate::rangeBoundaries($this->phpSheet->getAutoFilter()->getRange());
  1646. $iNumFilters = 1 + $rangeBounds[1][0] - $rangeBounds[0][0];
  1647. $header = pack('vv', $record, $length);
  1648. $data = pack('v', $iNumFilters);
  1649. $this->append($header . $data);
  1650. }
  1651. /**
  1652. * Write the GUTS BIFF record. This is used to configure the gutter margins
  1653. * where Excel outline symbols are displayed. The visibility of the gutters is
  1654. * controlled by a flag in WSBOOL.
  1655. *
  1656. * @see writeWsbool()
  1657. */
  1658. private function writeGuts()
  1659. {
  1660. $record = 0x0080; // Record identifier
  1661. $length = 0x0008; // Bytes to follow
  1662. $dxRwGut = 0x0000; // Size of row gutter
  1663. $dxColGut = 0x0000; // Size of col gutter
  1664. // determine maximum row outline level
  1665. $maxRowOutlineLevel = 0;
  1666. foreach ($this->phpSheet->getRowDimensions() as $rowDimension) {
  1667. $maxRowOutlineLevel = max($maxRowOutlineLevel, $rowDimension->getOutlineLevel());
  1668. }
  1669. $col_level = 0;
  1670. // Calculate the maximum column outline level. The equivalent calculation
  1671. // for the row outline level is carried out in writeRow().
  1672. $colcount = count($this->columnInfo);
  1673. for ($i = 0; $i < $colcount; ++$i) {
  1674. $col_level = max($this->columnInfo[$i][5], $col_level);
  1675. }
  1676. // Set the limits for the outline levels (0 <= x <= 7).
  1677. $col_level = max(0, min($col_level, 7));
  1678. // The displayed level is one greater than the max outline levels
  1679. if ($maxRowOutlineLevel) {
  1680. ++$maxRowOutlineLevel;
  1681. }
  1682. if ($col_level) {
  1683. ++$col_level;
  1684. }
  1685. $header = pack('vv', $record, $length);
  1686. $data = pack('vvvv', $dxRwGut, $dxColGut, $maxRowOutlineLevel, $col_level);
  1687. $this->append($header . $data);
  1688. }
  1689. /**
  1690. * Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
  1691. * with the SETUP record.
  1692. */
  1693. private function writeWsbool()
  1694. {
  1695. $record = 0x0081; // Record identifier
  1696. $length = 0x0002; // Bytes to follow
  1697. $grbit = 0x0000;
  1698. // The only option that is of interest is the flag for fit to page. So we
  1699. // set all the options in one go.
  1700. //
  1701. // Set the option flags
  1702. $grbit |= 0x0001; // Auto page breaks visible
  1703. if ($this->outlineStyle) {
  1704. $grbit |= 0x0020; // Auto outline styles
  1705. }
  1706. if ($this->phpSheet->getShowSummaryBelow()) {
  1707. $grbit |= 0x0040; // Outline summary below
  1708. }
  1709. if ($this->phpSheet->getShowSummaryRight()) {
  1710. $grbit |= 0x0080; // Outline summary right
  1711. }
  1712. if ($this->phpSheet->getPageSetup()->getFitToPage()) {
  1713. $grbit |= 0x0100; // Page setup fit to page
  1714. }
  1715. if ($this->outlineOn) {
  1716. $grbit |= 0x0400; // Outline symbols displayed
  1717. }
  1718. $header = pack('vv', $record, $length);
  1719. $data = pack('v', $grbit);
  1720. $this->append($header . $data);
  1721. }
  1722. /**
  1723. * Write the HORIZONTALPAGEBREAKS and VERTICALPAGEBREAKS BIFF records.
  1724. */
  1725. private function writeBreaks()
  1726. {
  1727. // initialize
  1728. $vbreaks = [];
  1729. $hbreaks = [];
  1730. foreach ($this->phpSheet->getBreaks() as $cell => $breakType) {
  1731. // Fetch coordinates
  1732. $coordinates = Coordinate::coordinateFromString($cell);
  1733. // Decide what to do by the type of break
  1734. switch ($breakType) {
  1735. case \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_COLUMN:
  1736. // Add to list of vertical breaks
  1737. $vbreaks[] = Coordinate::columnIndexFromString($coordinates[0]) - 1;
  1738. break;
  1739. case \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_ROW:
  1740. // Add to list of horizontal breaks
  1741. $hbreaks[] = $coordinates[1];
  1742. break;
  1743. case \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_NONE:
  1744. default:
  1745. // Nothing to do
  1746. break;
  1747. }
  1748. }
  1749. //horizontal page breaks
  1750. if (!empty($hbreaks)) {
  1751. // Sort and filter array of page breaks
  1752. sort($hbreaks, SORT_NUMERIC);
  1753. if ($hbreaks[0] == 0) { // don't use first break if it's 0
  1754. array_shift($hbreaks);
  1755. }
  1756. $record = 0x001b; // Record identifier
  1757. $cbrk = count($hbreaks); // Number of page breaks
  1758. $length = 2 + 6 * $cbrk; // Bytes to follow
  1759. $header = pack('vv', $record, $length);
  1760. $data = pack('v', $cbrk);
  1761. // Append each page break
  1762. foreach ($hbreaks as $hbreak) {
  1763. $data .= pack('vvv', $hbreak, 0x0000, 0x00ff);
  1764. }
  1765. $this->append($header . $data);
  1766. }
  1767. // vertical page breaks
  1768. if (!empty($vbreaks)) {
  1769. // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
  1770. // It is slightly higher in Excel 97/200, approx. 1026
  1771. $vbreaks = array_slice($vbreaks, 0, 1000);
  1772. // Sort and filter array of page breaks
  1773. sort($vbreaks, SORT_NUMERIC);
  1774. if ($vbreaks[0] == 0) { // don't use first break if it's 0
  1775. array_shift($vbreaks);
  1776. }
  1777. $record = 0x001a; // Record identifier
  1778. $cbrk = count($vbreaks); // Number of page breaks
  1779. $length = 2 + 6 * $cbrk; // Bytes to follow
  1780. $header = pack('vv', $record, $length);
  1781. $data = pack('v', $cbrk);
  1782. // Append each page break
  1783. foreach ($vbreaks as $vbreak) {
  1784. $data .= pack('vvv', $vbreak, 0x0000, 0xffff);
  1785. }
  1786. $this->append($header . $data);
  1787. }
  1788. }
  1789. /**
  1790. * Set the Biff PROTECT record to indicate that the worksheet is protected.
  1791. */
  1792. private function writeProtect()
  1793. {
  1794. // Exit unless sheet protection has been specified
  1795. if (!$this->phpSheet->getProtection()->getSheet()) {
  1796. return;
  1797. }
  1798. $record = 0x0012; // Record identifier
  1799. $length = 0x0002; // Bytes to follow
  1800. $fLock = 1; // Worksheet is protected
  1801. $header = pack('vv', $record, $length);
  1802. $data = pack('v', $fLock);
  1803. $this->append($header . $data);
  1804. }
  1805. /**
  1806. * Write SCENPROTECT.
  1807. */
  1808. private function writeScenProtect()
  1809. {
  1810. // Exit if sheet protection is not active
  1811. if (!$this->phpSheet->getProtection()->getSheet()) {
  1812. return;
  1813. }
  1814. // Exit if scenarios are not protected
  1815. if (!$this->phpSheet->getProtection()->getScenarios()) {
  1816. return;
  1817. }
  1818. $record = 0x00DD; // Record identifier
  1819. $length = 0x0002; // Bytes to follow
  1820. $header = pack('vv', $record, $length);
  1821. $data = pack('v', 1);
  1822. $this->append($header . $data);
  1823. }
  1824. /**
  1825. * Write OBJECTPROTECT.
  1826. */
  1827. private function writeObjectProtect()
  1828. {
  1829. // Exit if sheet protection is not active
  1830. if (!$this->phpSheet->getProtection()->getSheet()) {
  1831. return;
  1832. }
  1833. // Exit if objects are not protected
  1834. if (!$this->phpSheet->getProtection()->getObjects()) {
  1835. return;
  1836. }
  1837. $record = 0x0063; // Record identifier
  1838. $length = 0x0002; // Bytes to follow
  1839. $header = pack('vv', $record, $length);
  1840. $data = pack('v', 1);
  1841. $this->append($header . $data);
  1842. }
  1843. /**
  1844. * Write the worksheet PASSWORD record.
  1845. */
  1846. private function writePassword()
  1847. {
  1848. // Exit unless sheet protection and password have been specified
  1849. if (!$this->phpSheet->getProtection()->getSheet() || !$this->phpSheet->getProtection()->getPassword()) {
  1850. return;
  1851. }
  1852. $record = 0x0013; // Record identifier
  1853. $length = 0x0002; // Bytes to follow
  1854. $wPassword = hexdec($this->phpSheet->getProtection()->getPassword()); // Encoded password
  1855. $header = pack('vv', $record, $length);
  1856. $data = pack('v', $wPassword);
  1857. $this->append($header . $data);
  1858. }
  1859. /**
  1860. * Insert a 24bit bitmap image in a worksheet.
  1861. *
  1862. * @param int $row The row we are going to insert the bitmap into
  1863. * @param int $col The column we are going to insert the bitmap into
  1864. * @param mixed $bitmap The bitmap filename or GD-image resource
  1865. * @param int $x the horizontal position (offset) of the image inside the cell
  1866. * @param int $y the vertical position (offset) of the image inside the cell
  1867. * @param float $scale_x The horizontal scale
  1868. * @param float $scale_y The vertical scale
  1869. */
  1870. public function insertBitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1)
  1871. {
  1872. $bitmap_array = (is_resource($bitmap) ? $this->processBitmapGd($bitmap) : $this->processBitmap($bitmap));
  1873. list($width, $height, $size, $data) = $bitmap_array;
  1874. // Scale the frame of the image.
  1875. $width *= $scale_x;
  1876. $height *= $scale_y;
  1877. // Calculate the vertices of the image and write the OBJ record
  1878. $this->positionImage($col, $row, $x, $y, $width, $height);
  1879. // Write the IMDATA record to store the bitmap data
  1880. $record = 0x007f;
  1881. $length = 8 + $size;
  1882. $cf = 0x09;
  1883. $env = 0x01;
  1884. $lcb = $size;
  1885. $header = pack('vvvvV', $record, $length, $cf, $env, $lcb);
  1886. $this->append($header . $data);
  1887. }
  1888. /**
  1889. * Calculate the vertices that define the position of the image as required by
  1890. * the OBJ record.
  1891. *
  1892. * +------------+------------+
  1893. * | A | B |
  1894. * +-----+------------+------------+
  1895. * | |(x1,y1) | |
  1896. * | 1 |(A1)._______|______ |
  1897. * | | | | |
  1898. * | | | | |
  1899. * +-----+----| BITMAP |-----+
  1900. * | | | | |
  1901. * | 2 | |______________. |
  1902. * | | | (B2)|
  1903. * | | | (x2,y2)|
  1904. * +---- +------------+------------+
  1905. *
  1906. * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  1907. *
  1908. * Based on the width and height of the bitmap we need to calculate 8 vars:
  1909. * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  1910. * The width and height of the cells are also variable and have to be taken into
  1911. * account.
  1912. * The values of $col_start and $row_start are passed in from the calling
  1913. * function. The values of $col_end and $row_end are calculated by subtracting
  1914. * the width and height of the bitmap from the width and height of the
  1915. * underlying cells.
  1916. * The vertices are expressed as a percentage of the underlying cell width as
  1917. * follows (rhs values are in pixels):
  1918. *
  1919. * x1 = X / W *1024
  1920. * y1 = Y / H *256
  1921. * x2 = (X-1) / W *1024
  1922. * y2 = (Y-1) / H *256
  1923. *
  1924. * Where: X is distance from the left side of the underlying cell
  1925. * Y is distance from the top of the underlying cell
  1926. * W is the width of the cell
  1927. * H is the height of the cell
  1928. * The SDK incorrectly states that the height should be expressed as a
  1929. * percentage of 1024.
  1930. *
  1931. * @param int $col_start Col containing upper left corner of object
  1932. * @param int $row_start Row containing top left corner of object
  1933. * @param int $x1 Distance to left side of object
  1934. * @param int $y1 Distance to top of object
  1935. * @param int $width Width of image frame
  1936. * @param int $height Height of image frame
  1937. */
  1938. public function positionImage($col_start, $row_start, $x1, $y1, $width, $height)
  1939. {
  1940. // Initialise end cell to the same as the start cell
  1941. $col_end = $col_start; // Col containing lower right corner of object
  1942. $row_end = $row_start; // Row containing bottom right corner of object
  1943. // Zero the specified offset if greater than the cell dimensions
  1944. if ($x1 >= Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_start + 1))) {
  1945. $x1 = 0;
  1946. }
  1947. if ($y1 >= Xls::sizeRow($this->phpSheet, $row_start + 1)) {
  1948. $y1 = 0;
  1949. }
  1950. $width = $width + $x1 - 1;
  1951. $height = $height + $y1 - 1;
  1952. // Subtract the underlying cell widths to find the end cell of the image
  1953. while ($width >= Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1))) {
  1954. $width -= Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1));
  1955. ++$col_end;
  1956. }
  1957. // Subtract the underlying cell heights to find the end cell of the image
  1958. while ($height >= Xls::sizeRow($this->phpSheet, $row_end + 1)) {
  1959. $height -= Xls::sizeRow($this->phpSheet, $row_end + 1);
  1960. ++$row_end;
  1961. }
  1962. // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  1963. // with zero eight or width.
  1964. //
  1965. if (Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_start + 1)) == 0) {
  1966. return;
  1967. }
  1968. if (Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1)) == 0) {
  1969. return;
  1970. }
  1971. if (Xls::sizeRow($this->phpSheet, $row_start + 1) == 0) {
  1972. return;
  1973. }
  1974. if (Xls::sizeRow($this->phpSheet, $row_end + 1) == 0) {
  1975. return;
  1976. }
  1977. // Convert the pixel values to the percentage value expected by Excel
  1978. $x1 = $x1 / Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_start + 1)) * 1024;
  1979. $y1 = $y1 / Xls::sizeRow($this->phpSheet, $row_start + 1) * 256;
  1980. $x2 = $width / Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1)) * 1024; // Distance to right side of object
  1981. $y2 = $height / Xls::sizeRow($this->phpSheet, $row_end + 1) * 256; // Distance to bottom of object
  1982. $this->writeObjPicture($col_start, $x1, $row_start, $y1, $col_end, $x2, $row_end, $y2);
  1983. }
  1984. /**
  1985. * Store the OBJ record that precedes an IMDATA record. This could be generalise
  1986. * to support other Excel objects.
  1987. *
  1988. * @param int $colL Column containing upper left corner of object
  1989. * @param int $dxL Distance from left side of cell
  1990. * @param int $rwT Row containing top left corner of object
  1991. * @param int $dyT Distance from top of cell
  1992. * @param int $colR Column containing lower right corner of object
  1993. * @param int $dxR Distance from right of cell
  1994. * @param int $rwB Row containing bottom right corner of object
  1995. * @param int $dyB Distance from bottom of cell
  1996. */
  1997. private function writeObjPicture($colL, $dxL, $rwT, $dyT, $colR, $dxR, $rwB, $dyB)
  1998. {
  1999. $record = 0x005d; // Record identifier
  2000. $length = 0x003c; // Bytes to follow
  2001. $cObj = 0x0001; // Count of objects in file (set to 1)
  2002. $OT = 0x0008; // Object type. 8 = Picture
  2003. $id = 0x0001; // Object ID
  2004. $grbit = 0x0614; // Option flags
  2005. $cbMacro = 0x0000; // Length of FMLA structure
  2006. $Reserved1 = 0x0000; // Reserved
  2007. $Reserved2 = 0x0000; // Reserved
  2008. $icvBack = 0x09; // Background colour
  2009. $icvFore = 0x09; // Foreground colour
  2010. $fls = 0x00; // Fill pattern
  2011. $fAuto = 0x00; // Automatic fill
  2012. $icv = 0x08; // Line colour
  2013. $lns = 0xff; // Line style
  2014. $lnw = 0x01; // Line weight
  2015. $fAutoB = 0x00; // Automatic border
  2016. $frs = 0x0000; // Frame style
  2017. $cf = 0x0009; // Image format, 9 = bitmap
  2018. $Reserved3 = 0x0000; // Reserved
  2019. $cbPictFmla = 0x0000; // Length of FMLA structure
  2020. $Reserved4 = 0x0000; // Reserved
  2021. $grbit2 = 0x0001; // Option flags
  2022. $Reserved5 = 0x0000; // Reserved
  2023. $header = pack('vv', $record, $length);
  2024. $data = pack('V', $cObj);
  2025. $data .= pack('v', $OT);
  2026. $data .= pack('v', $id);
  2027. $data .= pack('v', $grbit);
  2028. $data .= pack('v', $colL);
  2029. $data .= pack('v', $dxL);
  2030. $data .= pack('v', $rwT);
  2031. $data .= pack('v', $dyT);
  2032. $data .= pack('v', $colR);
  2033. $data .= pack('v', $dxR);
  2034. $data .= pack('v', $rwB);
  2035. $data .= pack('v', $dyB);
  2036. $data .= pack('v', $cbMacro);
  2037. $data .= pack('V', $Reserved1);
  2038. $data .= pack('v', $Reserved2);
  2039. $data .= pack('C', $icvBack);
  2040. $data .= pack('C', $icvFore);
  2041. $data .= pack('C', $fls);
  2042. $data .= pack('C', $fAuto);
  2043. $data .= pack('C', $icv);
  2044. $data .= pack('C', $lns);
  2045. $data .= pack('C', $lnw);
  2046. $data .= pack('C', $fAutoB);
  2047. $data .= pack('v', $frs);
  2048. $data .= pack('V', $cf);
  2049. $data .= pack('v', $Reserved3);
  2050. $data .= pack('v', $cbPictFmla);
  2051. $data .= pack('v', $Reserved4);
  2052. $data .= pack('v', $grbit2);
  2053. $data .= pack('V', $Reserved5);
  2054. $this->append($header . $data);
  2055. }
  2056. /**
  2057. * Convert a GD-image into the internal format.
  2058. *
  2059. * @param resource $image The image to process
  2060. *
  2061. * @return array Array with data and properties of the bitmap
  2062. */
  2063. public function processBitmapGd($image)
  2064. {
  2065. $width = imagesx($image);
  2066. $height = imagesy($image);
  2067. $data = pack('Vvvvv', 0x000c, $width, $height, 0x01, 0x18);
  2068. for ($j = $height; --$j;) {
  2069. for ($i = 0; $i < $width; ++$i) {
  2070. $color = imagecolorsforindex($image, imagecolorat($image, $i, $j));
  2071. foreach (['red', 'green', 'blue'] as $key) {
  2072. $color[$key] = $color[$key] + round((255 - $color[$key]) * $color['alpha'] / 127);
  2073. }
  2074. $data .= chr($color['blue']) . chr($color['green']) . chr($color['red']);
  2075. }
  2076. if (3 * $width % 4) {
  2077. $data .= str_repeat("\x00", 4 - 3 * $width % 4);
  2078. }
  2079. }
  2080. return [$width, $height, strlen($data), $data];
  2081. }
  2082. /**
  2083. * Convert a 24 bit bitmap into the modified internal format used by Windows.
  2084. * This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
  2085. * MSDN library.
  2086. *
  2087. * @param string $bitmap The bitmap to process
  2088. *
  2089. * @return array Array with data and properties of the bitmap
  2090. */
  2091. public function processBitmap($bitmap)
  2092. {
  2093. // Open file.
  2094. $bmp_fd = @fopen($bitmap, 'rb');
  2095. if (!$bmp_fd) {
  2096. throw new WriterException("Couldn't import $bitmap");
  2097. }
  2098. // Slurp the file into a string.
  2099. $data = fread($bmp_fd, filesize($bitmap));
  2100. // Check that the file is big enough to be a bitmap.
  2101. if (strlen($data) <= 0x36) {
  2102. throw new WriterException("$bitmap doesn't contain enough data.\n");
  2103. }
  2104. // The first 2 bytes are used to identify the bitmap.
  2105. $identity = unpack('A2ident', $data);
  2106. if ($identity['ident'] != 'BM') {
  2107. throw new WriterException("$bitmap doesn't appear to be a valid bitmap image.\n");
  2108. }
  2109. // Remove bitmap data: ID.
  2110. $data = substr($data, 2);
  2111. // Read and remove the bitmap size. This is more reliable than reading
  2112. // the data size at offset 0x22.
  2113. //
  2114. $size_array = unpack('Vsa', substr($data, 0, 4));
  2115. $size = $size_array['sa'];
  2116. $data = substr($data, 4);
  2117. $size -= 0x36; // Subtract size of bitmap header.
  2118. $size += 0x0C; // Add size of BIFF header.
  2119. // Remove bitmap data: reserved, offset, header length.
  2120. $data = substr($data, 12);
  2121. // Read and remove the bitmap width and height. Verify the sizes.
  2122. $width_and_height = unpack('V2', substr($data, 0, 8));
  2123. $width = $width_and_height[1];
  2124. $height = $width_and_height[2];
  2125. $data = substr($data, 8);
  2126. if ($width > 0xFFFF) {
  2127. throw new WriterException("$bitmap: largest image width supported is 65k.\n");
  2128. }
  2129. if ($height > 0xFFFF) {
  2130. throw new WriterException("$bitmap: largest image height supported is 65k.\n");
  2131. }
  2132. // Read and remove the bitmap planes and bpp data. Verify them.
  2133. $planes_and_bitcount = unpack('v2', substr($data, 0, 4));
  2134. $data = substr($data, 4);
  2135. if ($planes_and_bitcount[2] != 24) { // Bitcount
  2136. throw new WriterException("$bitmap isn't a 24bit true color bitmap.\n");
  2137. }
  2138. if ($planes_and_bitcount[1] != 1) {
  2139. throw new WriterException("$bitmap: only 1 plane supported in bitmap image.\n");
  2140. }
  2141. // Read and remove the bitmap compression. Verify compression.
  2142. $compression = unpack('Vcomp', substr($data, 0, 4));
  2143. $data = substr($data, 4);
  2144. if ($compression['comp'] != 0) {
  2145. throw new WriterException("$bitmap: compression not supported in bitmap image.\n");
  2146. }
  2147. // Remove bitmap data: data size, hres, vres, colours, imp. colours.
  2148. $data = substr($data, 20);
  2149. // Add the BITMAPCOREHEADER data
  2150. $header = pack('Vvvvv', 0x000c, $width, $height, 0x01, 0x18);
  2151. $data = $header . $data;
  2152. return [$width, $height, $size, $data];
  2153. }
  2154. /**
  2155. * Store the window zoom factor. This should be a reduced fraction but for
  2156. * simplicity we will store all fractions with a numerator of 100.
  2157. */
  2158. private function writeZoom()
  2159. {
  2160. // If scale is 100 we don't need to write a record
  2161. if ($this->phpSheet->getSheetView()->getZoomScale() == 100) {
  2162. return;
  2163. }
  2164. $record = 0x00A0; // Record identifier
  2165. $length = 0x0004; // Bytes to follow
  2166. $header = pack('vv', $record, $length);
  2167. $data = pack('vv', $this->phpSheet->getSheetView()->getZoomScale(), 100);
  2168. $this->append($header . $data);
  2169. }
  2170. /**
  2171. * Get Escher object.
  2172. *
  2173. * @return \PhpOffice\PhpSpreadsheet\Shared\Escher
  2174. */
  2175. public function getEscher()
  2176. {
  2177. return $this->escher;
  2178. }
  2179. /**
  2180. * Set Escher object.
  2181. *
  2182. * @param \PhpOffice\PhpSpreadsheet\Shared\Escher $pValue
  2183. */
  2184. public function setEscher(\PhpOffice\PhpSpreadsheet\Shared\Escher $pValue = null)
  2185. {
  2186. $this->escher = $pValue;
  2187. }
  2188. /**
  2189. * Write MSODRAWING record.
  2190. */
  2191. private function writeMsoDrawing()
  2192. {
  2193. // write the Escher stream if necessary
  2194. if (isset($this->escher)) {
  2195. $writer = new Escher($this->escher);
  2196. $data = $writer->close();
  2197. $spOffsets = $writer->getSpOffsets();
  2198. $spTypes = $writer->getSpTypes();
  2199. // write the neccesary MSODRAWING, OBJ records
  2200. // split the Escher stream
  2201. $spOffsets[0] = 0;
  2202. $nm = count($spOffsets) - 1; // number of shapes excluding first shape
  2203. for ($i = 1; $i <= $nm; ++$i) {
  2204. // MSODRAWING record
  2205. $record = 0x00EC; // Record identifier
  2206. // chunk of Escher stream for one shape
  2207. $dataChunk = substr($data, $spOffsets[$i - 1], $spOffsets[$i] - $spOffsets[$i - 1]);
  2208. $length = strlen($dataChunk);
  2209. $header = pack('vv', $record, $length);
  2210. $this->append($header . $dataChunk);
  2211. // OBJ record
  2212. $record = 0x005D; // record identifier
  2213. $objData = '';
  2214. // ftCmo
  2215. if ($spTypes[$i] == 0x00C9) {
  2216. // Add ftCmo (common object data) subobject
  2217. $objData .=
  2218. pack(
  2219. 'vvvvvVVV',
  2220. 0x0015, // 0x0015 = ftCmo
  2221. 0x0012, // length of ftCmo data
  2222. 0x0014, // object type, 0x0014 = filter
  2223. $i, // object id number, Excel seems to use 1-based index, local for the sheet
  2224. 0x2101, // option flags, 0x2001 is what OpenOffice.org uses
  2225. 0, // reserved
  2226. 0, // reserved
  2227. 0 // reserved
  2228. );
  2229. // Add ftSbs Scroll bar subobject
  2230. $objData .= pack('vv', 0x00C, 0x0014);
  2231. $objData .= pack('H*', '0000000000000000640001000A00000010000100');
  2232. // Add ftLbsData (List box data) subobject
  2233. $objData .= pack('vv', 0x0013, 0x1FEE);
  2234. $objData .= pack('H*', '00000000010001030000020008005700');
  2235. } else {
  2236. // Add ftCmo (common object data) subobject
  2237. $objData .=
  2238. pack(
  2239. 'vvvvvVVV',
  2240. 0x0015, // 0x0015 = ftCmo
  2241. 0x0012, // length of ftCmo data
  2242. 0x0008, // object type, 0x0008 = picture
  2243. $i, // object id number, Excel seems to use 1-based index, local for the sheet
  2244. 0x6011, // option flags, 0x6011 is what OpenOffice.org uses
  2245. 0, // reserved
  2246. 0, // reserved
  2247. 0 // reserved
  2248. );
  2249. }
  2250. // ftEnd
  2251. $objData .=
  2252. pack(
  2253. 'vv',
  2254. 0x0000, // 0x0000 = ftEnd
  2255. 0x0000 // length of ftEnd data
  2256. );
  2257. $length = strlen($objData);
  2258. $header = pack('vv', $record, $length);
  2259. $this->append($header . $objData);
  2260. }
  2261. }
  2262. }
  2263. /**
  2264. * Store the DATAVALIDATIONS and DATAVALIDATION records.
  2265. */
  2266. private function writeDataValidity()
  2267. {
  2268. // Datavalidation collection
  2269. $dataValidationCollection = $this->phpSheet->getDataValidationCollection();
  2270. // Write data validations?
  2271. if (!empty($dataValidationCollection)) {
  2272. // DATAVALIDATIONS record
  2273. $record = 0x01B2; // Record identifier
  2274. $length = 0x0012; // Bytes to follow
  2275. $grbit = 0x0000; // Prompt box at cell, no cached validity data at DV records
  2276. $horPos = 0x00000000; // Horizontal position of prompt box, if fixed position
  2277. $verPos = 0x00000000; // Vertical position of prompt box, if fixed position
  2278. $objId = 0xFFFFFFFF; // Object identifier of drop down arrow object, or -1 if not visible
  2279. $header = pack('vv', $record, $length);
  2280. $data = pack('vVVVV', $grbit, $horPos, $verPos, $objId, count($dataValidationCollection));
  2281. $this->append($header . $data);
  2282. // DATAVALIDATION records
  2283. $record = 0x01BE; // Record identifier
  2284. foreach ($dataValidationCollection as $cellCoordinate => $dataValidation) {
  2285. // initialize record data
  2286. $data = '';
  2287. // options
  2288. $options = 0x00000000;
  2289. // data type
  2290. $type = 0x00;
  2291. switch ($dataValidation->getType()) {
  2292. case DataValidation::TYPE_NONE:
  2293. $type = 0x00;
  2294. break;
  2295. case DataValidation::TYPE_WHOLE:
  2296. $type = 0x01;
  2297. break;
  2298. case DataValidation::TYPE_DECIMAL:
  2299. $type = 0x02;
  2300. break;
  2301. case DataValidation::TYPE_LIST:
  2302. $type = 0x03;
  2303. break;
  2304. case DataValidation::TYPE_DATE:
  2305. $type = 0x04;
  2306. break;
  2307. case DataValidation::TYPE_TIME:
  2308. $type = 0x05;
  2309. break;
  2310. case DataValidation::TYPE_TEXTLENGTH:
  2311. $type = 0x06;
  2312. break;
  2313. case DataValidation::TYPE_CUSTOM:
  2314. $type = 0x07;
  2315. break;
  2316. }
  2317. $options |= $type << 0;
  2318. // error style
  2319. $errorStyle = 0x00;
  2320. switch ($dataValidation->getErrorStyle()) {
  2321. case DataValidation::STYLE_STOP:
  2322. $errorStyle = 0x00;
  2323. break;
  2324. case DataValidation::STYLE_WARNING:
  2325. $errorStyle = 0x01;
  2326. break;
  2327. case DataValidation::STYLE_INFORMATION:
  2328. $errorStyle = 0x02;
  2329. break;
  2330. }
  2331. $options |= $errorStyle << 4;
  2332. // explicit formula?
  2333. if ($type == 0x03 && preg_match('/^\".*\"$/', $dataValidation->getFormula1())) {
  2334. $options |= 0x01 << 7;
  2335. }
  2336. // empty cells allowed
  2337. $options |= $dataValidation->getAllowBlank() << 8;
  2338. // show drop down
  2339. $options |= (!$dataValidation->getShowDropDown()) << 9;
  2340. // show input message
  2341. $options |= $dataValidation->getShowInputMessage() << 18;
  2342. // show error message
  2343. $options |= $dataValidation->getShowErrorMessage() << 19;
  2344. // condition operator
  2345. $operator = 0x00;
  2346. switch ($dataValidation->getOperator()) {
  2347. case DataValidation::OPERATOR_BETWEEN:
  2348. $operator = 0x00;
  2349. break;
  2350. case DataValidation::OPERATOR_NOTBETWEEN:
  2351. $operator = 0x01;
  2352. break;
  2353. case DataValidation::OPERATOR_EQUAL:
  2354. $operator = 0x02;
  2355. break;
  2356. case DataValidation::OPERATOR_NOTEQUAL:
  2357. $operator = 0x03;
  2358. break;
  2359. case DataValidation::OPERATOR_GREATERTHAN:
  2360. $operator = 0x04;
  2361. break;
  2362. case DataValidation::OPERATOR_LESSTHAN:
  2363. $operator = 0x05;
  2364. break;
  2365. case DataValidation::OPERATOR_GREATERTHANOREQUAL:
  2366. $operator = 0x06;
  2367. break;
  2368. case DataValidation::OPERATOR_LESSTHANOREQUAL:
  2369. $operator = 0x07;
  2370. break;
  2371. }
  2372. $options |= $operator << 20;
  2373. $data = pack('V', $options);
  2374. // prompt title
  2375. $promptTitle = $dataValidation->getPromptTitle() !== '' ?
  2376. $dataValidation->getPromptTitle() : chr(0);
  2377. $data .= StringHelper::UTF8toBIFF8UnicodeLong($promptTitle);
  2378. // error title
  2379. $errorTitle = $dataValidation->getErrorTitle() !== '' ?
  2380. $dataValidation->getErrorTitle() : chr(0);
  2381. $data .= StringHelper::UTF8toBIFF8UnicodeLong($errorTitle);
  2382. // prompt text
  2383. $prompt = $dataValidation->getPrompt() !== '' ?
  2384. $dataValidation->getPrompt() : chr(0);
  2385. $data .= StringHelper::UTF8toBIFF8UnicodeLong($prompt);
  2386. // error text
  2387. $error = $dataValidation->getError() !== '' ?
  2388. $dataValidation->getError() : chr(0);
  2389. $data .= StringHelper::UTF8toBIFF8UnicodeLong($error);
  2390. // formula 1
  2391. try {
  2392. $formula1 = $dataValidation->getFormula1();
  2393. if ($type == 0x03) { // list type
  2394. $formula1 = str_replace(',', chr(0), $formula1);
  2395. }
  2396. $this->parser->parse($formula1);
  2397. $formula1 = $this->parser->toReversePolish();
  2398. $sz1 = strlen($formula1);
  2399. } catch (PhpSpreadsheetException $e) {
  2400. $sz1 = 0;
  2401. $formula1 = '';
  2402. }
  2403. $data .= pack('vv', $sz1, 0x0000);
  2404. $data .= $formula1;
  2405. // formula 2
  2406. try {
  2407. $formula2 = $dataValidation->getFormula2();
  2408. if ($formula2 === '') {
  2409. throw new WriterException('No formula2');
  2410. }
  2411. $this->parser->parse($formula2);
  2412. $formula2 = $this->parser->toReversePolish();
  2413. $sz2 = strlen($formula2);
  2414. } catch (PhpSpreadsheetException $e) {
  2415. $sz2 = 0;
  2416. $formula2 = '';
  2417. }
  2418. $data .= pack('vv', $sz2, 0x0000);
  2419. $data .= $formula2;
  2420. // cell range address list
  2421. $data .= pack('v', 0x0001);
  2422. $data .= $this->writeBIFF8CellRangeAddressFixed($cellCoordinate);
  2423. $length = strlen($data);
  2424. $header = pack('vv', $record, $length);
  2425. $this->append($header . $data);
  2426. }
  2427. }
  2428. }
  2429. /**
  2430. * Map Error code.
  2431. *
  2432. * @param string $errorCode
  2433. *
  2434. * @return int
  2435. */
  2436. private static function mapErrorCode($errorCode)
  2437. {
  2438. switch ($errorCode) {
  2439. case '#NULL!':
  2440. return 0x00;
  2441. case '#DIV/0!':
  2442. return 0x07;
  2443. case '#VALUE!':
  2444. return 0x0F;
  2445. case '#REF!':
  2446. return 0x17;
  2447. case '#NAME?':
  2448. return 0x1D;
  2449. case '#NUM!':
  2450. return 0x24;
  2451. case '#N/A':
  2452. return 0x2A;
  2453. }
  2454. return 0;
  2455. }
  2456. /**
  2457. * Write PLV Record.
  2458. */
  2459. private function writePageLayoutView()
  2460. {
  2461. $record = 0x088B; // Record identifier
  2462. $length = 0x0010; // Bytes to follow
  2463. $rt = 0x088B; // 2
  2464. $grbitFrt = 0x0000; // 2
  2465. $reserved = 0x0000000000000000; // 8
  2466. $wScalvePLV = $this->phpSheet->getSheetView()->getZoomScale(); // 2
  2467. // The options flags that comprise $grbit
  2468. if ($this->phpSheet->getSheetView()->getView() == SheetView::SHEETVIEW_PAGE_LAYOUT) {
  2469. $fPageLayoutView = 1;
  2470. } else {
  2471. $fPageLayoutView = 0;
  2472. }
  2473. $fRulerVisible = 0;
  2474. $fWhitespaceHidden = 0;
  2475. $grbit = $fPageLayoutView; // 2
  2476. $grbit |= $fRulerVisible << 1;
  2477. $grbit |= $fWhitespaceHidden << 3;
  2478. $header = pack('vv', $record, $length);
  2479. $data = pack('vvVVvv', $rt, $grbitFrt, 0x00000000, 0x00000000, $wScalvePLV, $grbit);
  2480. $this->append($header . $data);
  2481. }
  2482. /**
  2483. * Write CFRule Record.
  2484. *
  2485. * @param Conditional $conditional
  2486. */
  2487. private function writeCFRule(Conditional $conditional)
  2488. {
  2489. $record = 0x01B1; // Record identifier
  2490. // $type : Type of the CF
  2491. // $operatorType : Comparison operator
  2492. if ($conditional->getConditionType() == Conditional::CONDITION_EXPRESSION) {
  2493. $type = 0x02;
  2494. $operatorType = 0x00;
  2495. } elseif ($conditional->getConditionType() == Conditional::CONDITION_CELLIS) {
  2496. $type = 0x01;
  2497. switch ($conditional->getOperatorType()) {
  2498. case Conditional::OPERATOR_NONE:
  2499. $operatorType = 0x00;
  2500. break;
  2501. case Conditional::OPERATOR_EQUAL:
  2502. $operatorType = 0x03;
  2503. break;
  2504. case Conditional::OPERATOR_GREATERTHAN:
  2505. $operatorType = 0x05;
  2506. break;
  2507. case Conditional::OPERATOR_GREATERTHANOREQUAL:
  2508. $operatorType = 0x07;
  2509. break;
  2510. case Conditional::OPERATOR_LESSTHAN:
  2511. $operatorType = 0x06;
  2512. break;
  2513. case Conditional::OPERATOR_LESSTHANOREQUAL:
  2514. $operatorType = 0x08;
  2515. break;
  2516. case Conditional::OPERATOR_NOTEQUAL:
  2517. $operatorType = 0x04;
  2518. break;
  2519. case Conditional::OPERATOR_BETWEEN:
  2520. $operatorType = 0x01;
  2521. break;
  2522. // not OPERATOR_NOTBETWEEN 0x02
  2523. }
  2524. }
  2525. // $szValue1 : size of the formula data for first value or formula
  2526. // $szValue2 : size of the formula data for second value or formula
  2527. $arrConditions = $conditional->getConditions();
  2528. $numConditions = count($arrConditions);
  2529. if ($numConditions == 1) {
  2530. $szValue1 = ($arrConditions[0] <= 65535 ? 3 : 0x0000);
  2531. $szValue2 = 0x0000;
  2532. $operand1 = pack('Cv', 0x1E, $arrConditions[0]);
  2533. $operand2 = null;
  2534. } elseif ($numConditions == 2 && ($conditional->getOperatorType() == Conditional::OPERATOR_BETWEEN)) {
  2535. $szValue1 = ($arrConditions[0] <= 65535 ? 3 : 0x0000);
  2536. $szValue2 = ($arrConditions[1] <= 65535 ? 3 : 0x0000);
  2537. $operand1 = pack('Cv', 0x1E, $arrConditions[0]);
  2538. $operand2 = pack('Cv', 0x1E, $arrConditions[1]);
  2539. } else {
  2540. $szValue1 = 0x0000;
  2541. $szValue2 = 0x0000;
  2542. $operand1 = null;
  2543. $operand2 = null;
  2544. }
  2545. // $flags : Option flags
  2546. // Alignment
  2547. $bAlignHz = ($conditional->getStyle()->getAlignment()->getHorizontal() == null ? 1 : 0);
  2548. $bAlignVt = ($conditional->getStyle()->getAlignment()->getVertical() == null ? 1 : 0);
  2549. $bAlignWrapTx = ($conditional->getStyle()->getAlignment()->getWrapText() == false ? 1 : 0);
  2550. $bTxRotation = ($conditional->getStyle()->getAlignment()->getTextRotation() == null ? 1 : 0);
  2551. $bIndent = ($conditional->getStyle()->getAlignment()->getIndent() == 0 ? 1 : 0);
  2552. $bShrinkToFit = ($conditional->getStyle()->getAlignment()->getShrinkToFit() == false ? 1 : 0);
  2553. if ($bAlignHz == 0 || $bAlignVt == 0 || $bAlignWrapTx == 0 || $bTxRotation == 0 || $bIndent == 0 || $bShrinkToFit == 0) {
  2554. $bFormatAlign = 1;
  2555. } else {
  2556. $bFormatAlign = 0;
  2557. }
  2558. // Protection
  2559. $bProtLocked = ($conditional->getStyle()->getProtection()->getLocked() == null ? 1 : 0);
  2560. $bProtHidden = ($conditional->getStyle()->getProtection()->getHidden() == null ? 1 : 0);
  2561. if ($bProtLocked == 0 || $bProtHidden == 0) {
  2562. $bFormatProt = 1;
  2563. } else {
  2564. $bFormatProt = 0;
  2565. }
  2566. // Border
  2567. $bBorderLeft = ($conditional->getStyle()->getBorders()->getLeft()->getColor()->getARGB() == Color::COLOR_BLACK
  2568. && $conditional->getStyle()->getBorders()->getLeft()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
  2569. $bBorderRight = ($conditional->getStyle()->getBorders()->getRight()->getColor()->getARGB() == Color::COLOR_BLACK
  2570. && $conditional->getStyle()->getBorders()->getRight()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
  2571. $bBorderTop = ($conditional->getStyle()->getBorders()->getTop()->getColor()->getARGB() == Color::COLOR_BLACK
  2572. && $conditional->getStyle()->getBorders()->getTop()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
  2573. $bBorderBottom = ($conditional->getStyle()->getBorders()->getBottom()->getColor()->getARGB() == Color::COLOR_BLACK
  2574. && $conditional->getStyle()->getBorders()->getBottom()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
  2575. if ($bBorderLeft == 0 || $bBorderRight == 0 || $bBorderTop == 0 || $bBorderBottom == 0) {
  2576. $bFormatBorder = 1;
  2577. } else {
  2578. $bFormatBorder = 0;
  2579. }
  2580. // Pattern
  2581. $bFillStyle = ($conditional->getStyle()->getFill()->getFillType() == null ? 0 : 1);
  2582. $bFillColor = ($conditional->getStyle()->getFill()->getStartColor()->getARGB() == null ? 0 : 1);
  2583. $bFillColorBg = ($conditional->getStyle()->getFill()->getEndColor()->getARGB() == null ? 0 : 1);
  2584. if ($bFillStyle == 0 || $bFillColor == 0 || $bFillColorBg == 0) {
  2585. $bFormatFill = 1;
  2586. } else {
  2587. $bFormatFill = 0;
  2588. }
  2589. // Font
  2590. if ($conditional->getStyle()->getFont()->getName() != null
  2591. || $conditional->getStyle()->getFont()->getSize() != null
  2592. || $conditional->getStyle()->getFont()->getBold() != null
  2593. || $conditional->getStyle()->getFont()->getItalic() != null
  2594. || $conditional->getStyle()->getFont()->getSuperscript() != null
  2595. || $conditional->getStyle()->getFont()->getSubscript() != null
  2596. || $conditional->getStyle()->getFont()->getUnderline() != null
  2597. || $conditional->getStyle()->getFont()->getStrikethrough() != null
  2598. || $conditional->getStyle()->getFont()->getColor()->getARGB() != null) {
  2599. $bFormatFont = 1;
  2600. } else {
  2601. $bFormatFont = 0;
  2602. }
  2603. // Alignment
  2604. $flags = 0;
  2605. $flags |= (1 == $bAlignHz ? 0x00000001 : 0);
  2606. $flags |= (1 == $bAlignVt ? 0x00000002 : 0);
  2607. $flags |= (1 == $bAlignWrapTx ? 0x00000004 : 0);
  2608. $flags |= (1 == $bTxRotation ? 0x00000008 : 0);
  2609. // Justify last line flag
  2610. $flags |= (1 == 1 ? 0x00000010 : 0);
  2611. $flags |= (1 == $bIndent ? 0x00000020 : 0);
  2612. $flags |= (1 == $bShrinkToFit ? 0x00000040 : 0);
  2613. // Default
  2614. $flags |= (1 == 1 ? 0x00000080 : 0);
  2615. // Protection
  2616. $flags |= (1 == $bProtLocked ? 0x00000100 : 0);
  2617. $flags |= (1 == $bProtHidden ? 0x00000200 : 0);
  2618. // Border
  2619. $flags |= (1 == $bBorderLeft ? 0x00000400 : 0);
  2620. $flags |= (1 == $bBorderRight ? 0x00000800 : 0);
  2621. $flags |= (1 == $bBorderTop ? 0x00001000 : 0);
  2622. $flags |= (1 == $bBorderBottom ? 0x00002000 : 0);
  2623. $flags |= (1 == 1 ? 0x00004000 : 0); // Top left to Bottom right border
  2624. $flags |= (1 == 1 ? 0x00008000 : 0); // Bottom left to Top right border
  2625. // Pattern
  2626. $flags |= (1 == $bFillStyle ? 0x00010000 : 0);
  2627. $flags |= (1 == $bFillColor ? 0x00020000 : 0);
  2628. $flags |= (1 == $bFillColorBg ? 0x00040000 : 0);
  2629. $flags |= (1 == 1 ? 0x00380000 : 0);
  2630. // Font
  2631. $flags |= (1 == $bFormatFont ? 0x04000000 : 0);
  2632. // Alignment:
  2633. $flags |= (1 == $bFormatAlign ? 0x08000000 : 0);
  2634. // Border
  2635. $flags |= (1 == $bFormatBorder ? 0x10000000 : 0);
  2636. // Pattern
  2637. $flags |= (1 == $bFormatFill ? 0x20000000 : 0);
  2638. // Protection
  2639. $flags |= (1 == $bFormatProt ? 0x40000000 : 0);
  2640. // Text direction
  2641. $flags |= (1 == 0 ? 0x80000000 : 0);
  2642. // Data Blocks
  2643. if ($bFormatFont == 1) {
  2644. // Font Name
  2645. if ($conditional->getStyle()->getFont()->getName() == null) {
  2646. $dataBlockFont = pack('VVVVVVVV', 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000);
  2647. $dataBlockFont .= pack('VVVVVVVV', 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000);
  2648. } else {
  2649. $dataBlockFont = StringHelper::UTF8toBIFF8UnicodeLong($conditional->getStyle()->getFont()->getName());
  2650. }
  2651. // Font Size
  2652. if ($conditional->getStyle()->getFont()->getSize() == null) {
  2653. $dataBlockFont .= pack('V', 20 * 11);
  2654. } else {
  2655. $dataBlockFont .= pack('V', 20 * $conditional->getStyle()->getFont()->getSize());
  2656. }
  2657. // Font Options
  2658. $dataBlockFont .= pack('V', 0);
  2659. // Font weight
  2660. if ($conditional->getStyle()->getFont()->getBold() == true) {
  2661. $dataBlockFont .= pack('v', 0x02BC);
  2662. } else {
  2663. $dataBlockFont .= pack('v', 0x0190);
  2664. }
  2665. // Escapement type
  2666. if ($conditional->getStyle()->getFont()->getSubscript() == true) {
  2667. $dataBlockFont .= pack('v', 0x02);
  2668. $fontEscapement = 0;
  2669. } elseif ($conditional->getStyle()->getFont()->getSuperscript() == true) {
  2670. $dataBlockFont .= pack('v', 0x01);
  2671. $fontEscapement = 0;
  2672. } else {
  2673. $dataBlockFont .= pack('v', 0x00);
  2674. $fontEscapement = 1;
  2675. }
  2676. // Underline type
  2677. switch ($conditional->getStyle()->getFont()->getUnderline()) {
  2678. case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE:
  2679. $dataBlockFont .= pack('C', 0x00);
  2680. $fontUnderline = 0;
  2681. break;
  2682. case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE:
  2683. $dataBlockFont .= pack('C', 0x02);
  2684. $fontUnderline = 0;
  2685. break;
  2686. case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING:
  2687. $dataBlockFont .= pack('C', 0x22);
  2688. $fontUnderline = 0;
  2689. break;
  2690. case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE:
  2691. $dataBlockFont .= pack('C', 0x01);
  2692. $fontUnderline = 0;
  2693. break;
  2694. case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING:
  2695. $dataBlockFont .= pack('C', 0x21);
  2696. $fontUnderline = 0;
  2697. break;
  2698. default:
  2699. $dataBlockFont .= pack('C', 0x00);
  2700. $fontUnderline = 1;
  2701. break;
  2702. }
  2703. // Not used (3)
  2704. $dataBlockFont .= pack('vC', 0x0000, 0x00);
  2705. // Font color index
  2706. switch ($conditional->getStyle()->getFont()->getColor()->getRGB()) {
  2707. case '000000':
  2708. $colorIdx = 0x08;
  2709. break;
  2710. case 'FFFFFF':
  2711. $colorIdx = 0x09;
  2712. break;
  2713. case 'FF0000':
  2714. $colorIdx = 0x0A;
  2715. break;
  2716. case '00FF00':
  2717. $colorIdx = 0x0B;
  2718. break;
  2719. case '0000FF':
  2720. $colorIdx = 0x0C;
  2721. break;
  2722. case 'FFFF00':
  2723. $colorIdx = 0x0D;
  2724. break;
  2725. case 'FF00FF':
  2726. $colorIdx = 0x0E;
  2727. break;
  2728. case '00FFFF':
  2729. $colorIdx = 0x0F;
  2730. break;
  2731. case '800000':
  2732. $colorIdx = 0x10;
  2733. break;
  2734. case '008000':
  2735. $colorIdx = 0x11;
  2736. break;
  2737. case '000080':
  2738. $colorIdx = 0x12;
  2739. break;
  2740. case '808000':
  2741. $colorIdx = 0x13;
  2742. break;
  2743. case '800080':
  2744. $colorIdx = 0x14;
  2745. break;
  2746. case '008080':
  2747. $colorIdx = 0x15;
  2748. break;
  2749. case 'C0C0C0':
  2750. $colorIdx = 0x16;
  2751. break;
  2752. case '808080':
  2753. $colorIdx = 0x17;
  2754. break;
  2755. case '9999FF':
  2756. $colorIdx = 0x18;
  2757. break;
  2758. case '993366':
  2759. $colorIdx = 0x19;
  2760. break;
  2761. case 'FFFFCC':
  2762. $colorIdx = 0x1A;
  2763. break;
  2764. case 'CCFFFF':
  2765. $colorIdx = 0x1B;
  2766. break;
  2767. case '660066':
  2768. $colorIdx = 0x1C;
  2769. break;
  2770. case 'FF8080':
  2771. $colorIdx = 0x1D;
  2772. break;
  2773. case '0066CC':
  2774. $colorIdx = 0x1E;
  2775. break;
  2776. case 'CCCCFF':
  2777. $colorIdx = 0x1F;
  2778. break;
  2779. case '000080':
  2780. $colorIdx = 0x20;
  2781. break;
  2782. case 'FF00FF':
  2783. $colorIdx = 0x21;
  2784. break;
  2785. case 'FFFF00':
  2786. $colorIdx = 0x22;
  2787. break;
  2788. case '00FFFF':
  2789. $colorIdx = 0x23;
  2790. break;
  2791. case '800080':
  2792. $colorIdx = 0x24;
  2793. break;
  2794. case '800000':
  2795. $colorIdx = 0x25;
  2796. break;
  2797. case '008080':
  2798. $colorIdx = 0x26;
  2799. break;
  2800. case '0000FF':
  2801. $colorIdx = 0x27;
  2802. break;
  2803. case '00CCFF':
  2804. $colorIdx = 0x28;
  2805. break;
  2806. case 'CCFFFF':
  2807. $colorIdx = 0x29;
  2808. break;
  2809. case 'CCFFCC':
  2810. $colorIdx = 0x2A;
  2811. break;
  2812. case 'FFFF99':
  2813. $colorIdx = 0x2B;
  2814. break;
  2815. case '99CCFF':
  2816. $colorIdx = 0x2C;
  2817. break;
  2818. case 'FF99CC':
  2819. $colorIdx = 0x2D;
  2820. break;
  2821. case 'CC99FF':
  2822. $colorIdx = 0x2E;
  2823. break;
  2824. case 'FFCC99':
  2825. $colorIdx = 0x2F;
  2826. break;
  2827. case '3366FF':
  2828. $colorIdx = 0x30;
  2829. break;
  2830. case '33CCCC':
  2831. $colorIdx = 0x31;
  2832. break;
  2833. case '99CC00':
  2834. $colorIdx = 0x32;
  2835. break;
  2836. case 'FFCC00':
  2837. $colorIdx = 0x33;
  2838. break;
  2839. case 'FF9900':
  2840. $colorIdx = 0x34;
  2841. break;
  2842. case 'FF6600':
  2843. $colorIdx = 0x35;
  2844. break;
  2845. case '666699':
  2846. $colorIdx = 0x36;
  2847. break;
  2848. case '969696':
  2849. $colorIdx = 0x37;
  2850. break;
  2851. case '003366':
  2852. $colorIdx = 0x38;
  2853. break;
  2854. case '339966':
  2855. $colorIdx = 0x39;
  2856. break;
  2857. case '003300':
  2858. $colorIdx = 0x3A;
  2859. break;
  2860. case '333300':
  2861. $colorIdx = 0x3B;
  2862. break;
  2863. case '993300':
  2864. $colorIdx = 0x3C;
  2865. break;
  2866. case '993366':
  2867. $colorIdx = 0x3D;
  2868. break;
  2869. case '333399':
  2870. $colorIdx = 0x3E;
  2871. break;
  2872. case '333333':
  2873. $colorIdx = 0x3F;
  2874. break;
  2875. default:
  2876. $colorIdx = 0x00;
  2877. break;
  2878. }
  2879. $dataBlockFont .= pack('V', $colorIdx);
  2880. // Not used (4)
  2881. $dataBlockFont .= pack('V', 0x00000000);
  2882. // Options flags for modified font attributes
  2883. $optionsFlags = 0;
  2884. $optionsFlagsBold = ($conditional->getStyle()->getFont()->getBold() == null ? 1 : 0);
  2885. $optionsFlags |= (1 == $optionsFlagsBold ? 0x00000002 : 0);
  2886. $optionsFlags |= (1 == 1 ? 0x00000008 : 0);
  2887. $optionsFlags |= (1 == 1 ? 0x00000010 : 0);
  2888. $optionsFlags |= (1 == 0 ? 0x00000020 : 0);
  2889. $optionsFlags |= (1 == 1 ? 0x00000080 : 0);
  2890. $dataBlockFont .= pack('V', $optionsFlags);
  2891. // Escapement type
  2892. $dataBlockFont .= pack('V', $fontEscapement);
  2893. // Underline type
  2894. $dataBlockFont .= pack('V', $fontUnderline);
  2895. // Always
  2896. $dataBlockFont .= pack('V', 0x00000000);
  2897. // Always
  2898. $dataBlockFont .= pack('V', 0x00000000);
  2899. // Not used (8)
  2900. $dataBlockFont .= pack('VV', 0x00000000, 0x00000000);
  2901. // Always
  2902. $dataBlockFont .= pack('v', 0x0001);
  2903. }
  2904. if ($bFormatAlign == 1) {
  2905. $blockAlign = 0;
  2906. // Alignment and text break
  2907. switch ($conditional->getStyle()->getAlignment()->getHorizontal()) {
  2908. case Alignment::HORIZONTAL_GENERAL:
  2909. $blockAlign = 0;
  2910. break;
  2911. case Alignment::HORIZONTAL_LEFT:
  2912. $blockAlign = 1;
  2913. break;
  2914. case Alignment::HORIZONTAL_RIGHT:
  2915. $blockAlign = 3;
  2916. break;
  2917. case Alignment::HORIZONTAL_CENTER:
  2918. $blockAlign = 2;
  2919. break;
  2920. case Alignment::HORIZONTAL_CENTER_CONTINUOUS:
  2921. $blockAlign = 6;
  2922. break;
  2923. case Alignment::HORIZONTAL_JUSTIFY:
  2924. $blockAlign = 5;
  2925. break;
  2926. }
  2927. if ($conditional->getStyle()->getAlignment()->getWrapText() == true) {
  2928. $blockAlign |= 1 << 3;
  2929. } else {
  2930. $blockAlign |= 0 << 3;
  2931. }
  2932. switch ($conditional->getStyle()->getAlignment()->getVertical()) {
  2933. case Alignment::VERTICAL_BOTTOM:
  2934. $blockAlign = 2 << 4;
  2935. break;
  2936. case Alignment::VERTICAL_TOP:
  2937. $blockAlign = 0 << 4;
  2938. break;
  2939. case Alignment::VERTICAL_CENTER:
  2940. $blockAlign = 1 << 4;
  2941. break;
  2942. case Alignment::VERTICAL_JUSTIFY:
  2943. $blockAlign = 3 << 4;
  2944. break;
  2945. }
  2946. $blockAlign |= 0 << 7;
  2947. // Text rotation angle
  2948. $blockRotation = $conditional->getStyle()->getAlignment()->getTextRotation();
  2949. // Indentation
  2950. $blockIndent = $conditional->getStyle()->getAlignment()->getIndent();
  2951. if ($conditional->getStyle()->getAlignment()->getShrinkToFit() == true) {
  2952. $blockIndent |= 1 << 4;
  2953. } else {
  2954. $blockIndent |= 0 << 4;
  2955. }
  2956. $blockIndent |= 0 << 6;
  2957. // Relative indentation
  2958. $blockIndentRelative = 255;
  2959. $dataBlockAlign = pack('CCvvv', $blockAlign, $blockRotation, $blockIndent, $blockIndentRelative, 0x0000);
  2960. }
  2961. if ($bFormatBorder == 1) {
  2962. $blockLineStyle = 0;
  2963. switch ($conditional->getStyle()->getBorders()->getLeft()->getBorderStyle()) {
  2964. case Border::BORDER_NONE:
  2965. $blockLineStyle |= 0x00;
  2966. break;
  2967. case Border::BORDER_THIN:
  2968. $blockLineStyle |= 0x01;
  2969. break;
  2970. case Border::BORDER_MEDIUM:
  2971. $blockLineStyle |= 0x02;
  2972. break;
  2973. case Border::BORDER_DASHED:
  2974. $blockLineStyle |= 0x03;
  2975. break;
  2976. case Border::BORDER_DOTTED:
  2977. $blockLineStyle |= 0x04;
  2978. break;
  2979. case Border::BORDER_THICK:
  2980. $blockLineStyle |= 0x05;
  2981. break;
  2982. case Border::BORDER_DOUBLE:
  2983. $blockLineStyle |= 0x06;
  2984. break;
  2985. case Border::BORDER_HAIR:
  2986. $blockLineStyle |= 0x07;
  2987. break;
  2988. case Border::BORDER_MEDIUMDASHED:
  2989. $blockLineStyle |= 0x08;
  2990. break;
  2991. case Border::BORDER_DASHDOT:
  2992. $blockLineStyle |= 0x09;
  2993. break;
  2994. case Border::BORDER_MEDIUMDASHDOT:
  2995. $blockLineStyle |= 0x0A;
  2996. break;
  2997. case Border::BORDER_DASHDOTDOT:
  2998. $blockLineStyle |= 0x0B;
  2999. break;
  3000. case Border::BORDER_MEDIUMDASHDOTDOT:
  3001. $blockLineStyle |= 0x0C;
  3002. break;
  3003. case Border::BORDER_SLANTDASHDOT:
  3004. $blockLineStyle |= 0x0D;
  3005. break;
  3006. }
  3007. switch ($conditional->getStyle()->getBorders()->getRight()->getBorderStyle()) {
  3008. case Border::BORDER_NONE:
  3009. $blockLineStyle |= 0x00 << 4;
  3010. break;
  3011. case Border::BORDER_THIN:
  3012. $blockLineStyle |= 0x01 << 4;
  3013. break;
  3014. case Border::BORDER_MEDIUM:
  3015. $blockLineStyle |= 0x02 << 4;
  3016. break;
  3017. case Border::BORDER_DASHED:
  3018. $blockLineStyle |= 0x03 << 4;
  3019. break;
  3020. case Border::BORDER_DOTTED:
  3021. $blockLineStyle |= 0x04 << 4;
  3022. break;
  3023. case Border::BORDER_THICK:
  3024. $blockLineStyle |= 0x05 << 4;
  3025. break;
  3026. case Border::BORDER_DOUBLE:
  3027. $blockLineStyle |= 0x06 << 4;
  3028. break;
  3029. case Border::BORDER_HAIR:
  3030. $blockLineStyle |= 0x07 << 4;
  3031. break;
  3032. case Border::BORDER_MEDIUMDASHED:
  3033. $blockLineStyle |= 0x08 << 4;
  3034. break;
  3035. case Border::BORDER_DASHDOT:
  3036. $blockLineStyle |= 0x09 << 4;
  3037. break;
  3038. case Border::BORDER_MEDIUMDASHDOT:
  3039. $blockLineStyle |= 0x0A << 4;
  3040. break;
  3041. case Border::BORDER_DASHDOTDOT:
  3042. $blockLineStyle |= 0x0B << 4;
  3043. break;
  3044. case Border::BORDER_MEDIUMDASHDOTDOT:
  3045. $blockLineStyle |= 0x0C << 4;
  3046. break;
  3047. case Border::BORDER_SLANTDASHDOT:
  3048. $blockLineStyle |= 0x0D << 4;
  3049. break;
  3050. }
  3051. switch ($conditional->getStyle()->getBorders()->getTop()->getBorderStyle()) {
  3052. case Border::BORDER_NONE:
  3053. $blockLineStyle |= 0x00 << 8;
  3054. break;
  3055. case Border::BORDER_THIN:
  3056. $blockLineStyle |= 0x01 << 8;
  3057. break;
  3058. case Border::BORDER_MEDIUM:
  3059. $blockLineStyle |= 0x02 << 8;
  3060. break;
  3061. case Border::BORDER_DASHED:
  3062. $blockLineStyle |= 0x03 << 8;
  3063. break;
  3064. case Border::BORDER_DOTTED:
  3065. $blockLineStyle |= 0x04 << 8;
  3066. break;
  3067. case Border::BORDER_THICK:
  3068. $blockLineStyle |= 0x05 << 8;
  3069. break;
  3070. case Border::BORDER_DOUBLE:
  3071. $blockLineStyle |= 0x06 << 8;
  3072. break;
  3073. case Border::BORDER_HAIR:
  3074. $blockLineStyle |= 0x07 << 8;
  3075. break;
  3076. case Border::BORDER_MEDIUMDASHED:
  3077. $blockLineStyle |= 0x08 << 8;
  3078. break;
  3079. case Border::BORDER_DASHDOT:
  3080. $blockLineStyle |= 0x09 << 8;
  3081. break;
  3082. case Border::BORDER_MEDIUMDASHDOT:
  3083. $blockLineStyle |= 0x0A << 8;
  3084. break;
  3085. case Border::BORDER_DASHDOTDOT:
  3086. $blockLineStyle |= 0x0B << 8;
  3087. break;
  3088. case Border::BORDER_MEDIUMDASHDOTDOT:
  3089. $blockLineStyle |= 0x0C << 8;
  3090. break;
  3091. case Border::BORDER_SLANTDASHDOT:
  3092. $blockLineStyle |= 0x0D << 8;
  3093. break;
  3094. }
  3095. switch ($conditional->getStyle()->getBorders()->getBottom()->getBorderStyle()) {
  3096. case Border::BORDER_NONE:
  3097. $blockLineStyle |= 0x00 << 12;
  3098. break;
  3099. case Border::BORDER_THIN:
  3100. $blockLineStyle |= 0x01 << 12;
  3101. break;
  3102. case Border::BORDER_MEDIUM:
  3103. $blockLineStyle |= 0x02 << 12;
  3104. break;
  3105. case Border::BORDER_DASHED:
  3106. $blockLineStyle |= 0x03 << 12;
  3107. break;
  3108. case Border::BORDER_DOTTED:
  3109. $blockLineStyle |= 0x04 << 12;
  3110. break;
  3111. case Border::BORDER_THICK:
  3112. $blockLineStyle |= 0x05 << 12;
  3113. break;
  3114. case Border::BORDER_DOUBLE:
  3115. $blockLineStyle |= 0x06 << 12;
  3116. break;
  3117. case Border::BORDER_HAIR:
  3118. $blockLineStyle |= 0x07 << 12;
  3119. break;
  3120. case Border::BORDER_MEDIUMDASHED:
  3121. $blockLineStyle |= 0x08 << 12;
  3122. break;
  3123. case Border::BORDER_DASHDOT:
  3124. $blockLineStyle |= 0x09 << 12;
  3125. break;
  3126. case Border::BORDER_MEDIUMDASHDOT:
  3127. $blockLineStyle |= 0x0A << 12;
  3128. break;
  3129. case Border::BORDER_DASHDOTDOT:
  3130. $blockLineStyle |= 0x0B << 12;
  3131. break;
  3132. case Border::BORDER_MEDIUMDASHDOTDOT:
  3133. $blockLineStyle |= 0x0C << 12;
  3134. break;
  3135. case Border::BORDER_SLANTDASHDOT:
  3136. $blockLineStyle |= 0x0D << 12;
  3137. break;
  3138. }
  3139. //@todo writeCFRule() => $blockLineStyle => Index Color for left line
  3140. //@todo writeCFRule() => $blockLineStyle => Index Color for right line
  3141. //@todo writeCFRule() => $blockLineStyle => Top-left to bottom-right on/off
  3142. //@todo writeCFRule() => $blockLineStyle => Bottom-left to top-right on/off
  3143. $blockColor = 0;
  3144. //@todo writeCFRule() => $blockColor => Index Color for top line
  3145. //@todo writeCFRule() => $blockColor => Index Color for bottom line
  3146. //@todo writeCFRule() => $blockColor => Index Color for diagonal line
  3147. switch ($conditional->getStyle()->getBorders()->getDiagonal()->getBorderStyle()) {
  3148. case Border::BORDER_NONE:
  3149. $blockColor |= 0x00 << 21;
  3150. break;
  3151. case Border::BORDER_THIN:
  3152. $blockColor |= 0x01 << 21;
  3153. break;
  3154. case Border::BORDER_MEDIUM:
  3155. $blockColor |= 0x02 << 21;
  3156. break;
  3157. case Border::BORDER_DASHED:
  3158. $blockColor |= 0x03 << 21;
  3159. break;
  3160. case Border::BORDER_DOTTED:
  3161. $blockColor |= 0x04 << 21;
  3162. break;
  3163. case Border::BORDER_THICK:
  3164. $blockColor |= 0x05 << 21;
  3165. break;
  3166. case Border::BORDER_DOUBLE:
  3167. $blockColor |= 0x06 << 21;
  3168. break;
  3169. case Border::BORDER_HAIR:
  3170. $blockColor |= 0x07 << 21;
  3171. break;
  3172. case Border::BORDER_MEDIUMDASHED:
  3173. $blockColor |= 0x08 << 21;
  3174. break;
  3175. case Border::BORDER_DASHDOT:
  3176. $blockColor |= 0x09 << 21;
  3177. break;
  3178. case Border::BORDER_MEDIUMDASHDOT:
  3179. $blockColor |= 0x0A << 21;
  3180. break;
  3181. case Border::BORDER_DASHDOTDOT:
  3182. $blockColor |= 0x0B << 21;
  3183. break;
  3184. case Border::BORDER_MEDIUMDASHDOTDOT:
  3185. $blockColor |= 0x0C << 21;
  3186. break;
  3187. case Border::BORDER_SLANTDASHDOT:
  3188. $blockColor |= 0x0D << 21;
  3189. break;
  3190. }
  3191. $dataBlockBorder = pack('vv', $blockLineStyle, $blockColor);
  3192. }
  3193. if ($bFormatFill == 1) {
  3194. // Fill Patern Style
  3195. $blockFillPatternStyle = 0;
  3196. switch ($conditional->getStyle()->getFill()->getFillType()) {
  3197. case Fill::FILL_NONE:
  3198. $blockFillPatternStyle = 0x00;
  3199. break;
  3200. case Fill::FILL_SOLID:
  3201. $blockFillPatternStyle = 0x01;
  3202. break;
  3203. case Fill::FILL_PATTERN_MEDIUMGRAY:
  3204. $blockFillPatternStyle = 0x02;
  3205. break;
  3206. case Fill::FILL_PATTERN_DARKGRAY:
  3207. $blockFillPatternStyle = 0x03;
  3208. break;
  3209. case Fill::FILL_PATTERN_LIGHTGRAY:
  3210. $blockFillPatternStyle = 0x04;
  3211. break;
  3212. case Fill::FILL_PATTERN_DARKHORIZONTAL:
  3213. $blockFillPatternStyle = 0x05;
  3214. break;
  3215. case Fill::FILL_PATTERN_DARKVERTICAL:
  3216. $blockFillPatternStyle = 0x06;
  3217. break;
  3218. case Fill::FILL_PATTERN_DARKDOWN:
  3219. $blockFillPatternStyle = 0x07;
  3220. break;
  3221. case Fill::FILL_PATTERN_DARKUP:
  3222. $blockFillPatternStyle = 0x08;
  3223. break;
  3224. case Fill::FILL_PATTERN_DARKGRID:
  3225. $blockFillPatternStyle = 0x09;
  3226. break;
  3227. case Fill::FILL_PATTERN_DARKTRELLIS:
  3228. $blockFillPatternStyle = 0x0A;
  3229. break;
  3230. case Fill::FILL_PATTERN_LIGHTHORIZONTAL:
  3231. $blockFillPatternStyle = 0x0B;
  3232. break;
  3233. case Fill::FILL_PATTERN_LIGHTVERTICAL:
  3234. $blockFillPatternStyle = 0x0C;
  3235. break;
  3236. case Fill::FILL_PATTERN_LIGHTDOWN:
  3237. $blockFillPatternStyle = 0x0D;
  3238. break;
  3239. case Fill::FILL_PATTERN_LIGHTUP:
  3240. $blockFillPatternStyle = 0x0E;
  3241. break;
  3242. case Fill::FILL_PATTERN_LIGHTGRID:
  3243. $blockFillPatternStyle = 0x0F;
  3244. break;
  3245. case Fill::FILL_PATTERN_LIGHTTRELLIS:
  3246. $blockFillPatternStyle = 0x10;
  3247. break;
  3248. case Fill::FILL_PATTERN_GRAY125:
  3249. $blockFillPatternStyle = 0x11;
  3250. break;
  3251. case Fill::FILL_PATTERN_GRAY0625:
  3252. $blockFillPatternStyle = 0x12;
  3253. break;
  3254. case Fill::FILL_GRADIENT_LINEAR:
  3255. $blockFillPatternStyle = 0x00;
  3256. break; // does not exist in BIFF8
  3257. case Fill::FILL_GRADIENT_PATH:
  3258. $blockFillPatternStyle = 0x00;
  3259. break; // does not exist in BIFF8
  3260. default:
  3261. $blockFillPatternStyle = 0x00;
  3262. break;
  3263. }
  3264. // Color
  3265. switch ($conditional->getStyle()->getFill()->getStartColor()->getRGB()) {
  3266. case '000000':
  3267. $colorIdxBg = 0x08;
  3268. break;
  3269. case 'FFFFFF':
  3270. $colorIdxBg = 0x09;
  3271. break;
  3272. case 'FF0000':
  3273. $colorIdxBg = 0x0A;
  3274. break;
  3275. case '00FF00':
  3276. $colorIdxBg = 0x0B;
  3277. break;
  3278. case '0000FF':
  3279. $colorIdxBg = 0x0C;
  3280. break;
  3281. case 'FFFF00':
  3282. $colorIdxBg = 0x0D;
  3283. break;
  3284. case 'FF00FF':
  3285. $colorIdxBg = 0x0E;
  3286. break;
  3287. case '00FFFF':
  3288. $colorIdxBg = 0x0F;
  3289. break;
  3290. case '800000':
  3291. $colorIdxBg = 0x10;
  3292. break;
  3293. case '008000':
  3294. $colorIdxBg = 0x11;
  3295. break;
  3296. case '000080':
  3297. $colorIdxBg = 0x12;
  3298. break;
  3299. case '808000':
  3300. $colorIdxBg = 0x13;
  3301. break;
  3302. case '800080':
  3303. $colorIdxBg = 0x14;
  3304. break;
  3305. case '008080':
  3306. $colorIdxBg = 0x15;
  3307. break;
  3308. case 'C0C0C0':
  3309. $colorIdxBg = 0x16;
  3310. break;
  3311. case '808080':
  3312. $colorIdxBg = 0x17;
  3313. break;
  3314. case '9999FF':
  3315. $colorIdxBg = 0x18;
  3316. break;
  3317. case '993366':
  3318. $colorIdxBg = 0x19;
  3319. break;
  3320. case 'FFFFCC':
  3321. $colorIdxBg = 0x1A;
  3322. break;
  3323. case 'CCFFFF':
  3324. $colorIdxBg = 0x1B;
  3325. break;
  3326. case '660066':
  3327. $colorIdxBg = 0x1C;
  3328. break;
  3329. case 'FF8080':
  3330. $colorIdxBg = 0x1D;
  3331. break;
  3332. case '0066CC':
  3333. $colorIdxBg = 0x1E;
  3334. break;
  3335. case 'CCCCFF':
  3336. $colorIdxBg = 0x1F;
  3337. break;
  3338. case '000080':
  3339. $colorIdxBg = 0x20;
  3340. break;
  3341. case 'FF00FF':
  3342. $colorIdxBg = 0x21;
  3343. break;
  3344. case 'FFFF00':
  3345. $colorIdxBg = 0x22;
  3346. break;
  3347. case '00FFFF':
  3348. $colorIdxBg = 0x23;
  3349. break;
  3350. case '800080':
  3351. $colorIdxBg = 0x24;
  3352. break;
  3353. case '800000':
  3354. $colorIdxBg = 0x25;
  3355. break;
  3356. case '008080':
  3357. $colorIdxBg = 0x26;
  3358. break;
  3359. case '0000FF':
  3360. $colorIdxBg = 0x27;
  3361. break;
  3362. case '00CCFF':
  3363. $colorIdxBg = 0x28;
  3364. break;
  3365. case 'CCFFFF':
  3366. $colorIdxBg = 0x29;
  3367. break;
  3368. case 'CCFFCC':
  3369. $colorIdxBg = 0x2A;
  3370. break;
  3371. case 'FFFF99':
  3372. $colorIdxBg = 0x2B;
  3373. break;
  3374. case '99CCFF':
  3375. $colorIdxBg = 0x2C;
  3376. break;
  3377. case 'FF99CC':
  3378. $colorIdxBg = 0x2D;
  3379. break;
  3380. case 'CC99FF':
  3381. $colorIdxBg = 0x2E;
  3382. break;
  3383. case 'FFCC99':
  3384. $colorIdxBg = 0x2F;
  3385. break;
  3386. case '3366FF':
  3387. $colorIdxBg = 0x30;
  3388. break;
  3389. case '33CCCC':
  3390. $colorIdxBg = 0x31;
  3391. break;
  3392. case '99CC00':
  3393. $colorIdxBg = 0x32;
  3394. break;
  3395. case 'FFCC00':
  3396. $colorIdxBg = 0x33;
  3397. break;
  3398. case 'FF9900':
  3399. $colorIdxBg = 0x34;
  3400. break;
  3401. case 'FF6600':
  3402. $colorIdxBg = 0x35;
  3403. break;
  3404. case '666699':
  3405. $colorIdxBg = 0x36;
  3406. break;
  3407. case '969696':
  3408. $colorIdxBg = 0x37;
  3409. break;
  3410. case '003366':
  3411. $colorIdxBg = 0x38;
  3412. break;
  3413. case '339966':
  3414. $colorIdxBg = 0x39;
  3415. break;
  3416. case '003300':
  3417. $colorIdxBg = 0x3A;
  3418. break;
  3419. case '333300':
  3420. $colorIdxBg = 0x3B;
  3421. break;
  3422. case '993300':
  3423. $colorIdxBg = 0x3C;
  3424. break;
  3425. case '993366':
  3426. $colorIdxBg = 0x3D;
  3427. break;
  3428. case '333399':
  3429. $colorIdxBg = 0x3E;
  3430. break;
  3431. case '333333':
  3432. $colorIdxBg = 0x3F;
  3433. break;
  3434. default:
  3435. $colorIdxBg = 0x41;
  3436. break;
  3437. }
  3438. // Fg Color
  3439. switch ($conditional->getStyle()->getFill()->getEndColor()->getRGB()) {
  3440. case '000000':
  3441. $colorIdxFg = 0x08;
  3442. break;
  3443. case 'FFFFFF':
  3444. $colorIdxFg = 0x09;
  3445. break;
  3446. case 'FF0000':
  3447. $colorIdxFg = 0x0A;
  3448. break;
  3449. case '00FF00':
  3450. $colorIdxFg = 0x0B;
  3451. break;
  3452. case '0000FF':
  3453. $colorIdxFg = 0x0C;
  3454. break;
  3455. case 'FFFF00':
  3456. $colorIdxFg = 0x0D;
  3457. break;
  3458. case 'FF00FF':
  3459. $colorIdxFg = 0x0E;
  3460. break;
  3461. case '00FFFF':
  3462. $colorIdxFg = 0x0F;
  3463. break;
  3464. case '800000':
  3465. $colorIdxFg = 0x10;
  3466. break;
  3467. case '008000':
  3468. $colorIdxFg = 0x11;
  3469. break;
  3470. case '000080':
  3471. $colorIdxFg = 0x12;
  3472. break;
  3473. case '808000':
  3474. $colorIdxFg = 0x13;
  3475. break;
  3476. case '800080':
  3477. $colorIdxFg = 0x14;
  3478. break;
  3479. case '008080':
  3480. $colorIdxFg = 0x15;
  3481. break;
  3482. case 'C0C0C0':
  3483. $colorIdxFg = 0x16;
  3484. break;
  3485. case '808080':
  3486. $colorIdxFg = 0x17;
  3487. break;
  3488. case '9999FF':
  3489. $colorIdxFg = 0x18;
  3490. break;
  3491. case '993366':
  3492. $colorIdxFg = 0x19;
  3493. break;
  3494. case 'FFFFCC':
  3495. $colorIdxFg = 0x1A;
  3496. break;
  3497. case 'CCFFFF':
  3498. $colorIdxFg = 0x1B;
  3499. break;
  3500. case '660066':
  3501. $colorIdxFg = 0x1C;
  3502. break;
  3503. case 'FF8080':
  3504. $colorIdxFg = 0x1D;
  3505. break;
  3506. case '0066CC':
  3507. $colorIdxFg = 0x1E;
  3508. break;
  3509. case 'CCCCFF':
  3510. $colorIdxFg = 0x1F;
  3511. break;
  3512. case '000080':
  3513. $colorIdxFg = 0x20;
  3514. break;
  3515. case 'FF00FF':
  3516. $colorIdxFg = 0x21;
  3517. break;
  3518. case 'FFFF00':
  3519. $colorIdxFg = 0x22;
  3520. break;
  3521. case '00FFFF':
  3522. $colorIdxFg = 0x23;
  3523. break;
  3524. case '800080':
  3525. $colorIdxFg = 0x24;
  3526. break;
  3527. case '800000':
  3528. $colorIdxFg = 0x25;
  3529. break;
  3530. case '008080':
  3531. $colorIdxFg = 0x26;
  3532. break;
  3533. case '0000FF':
  3534. $colorIdxFg = 0x27;
  3535. break;
  3536. case '00CCFF':
  3537. $colorIdxFg = 0x28;
  3538. break;
  3539. case 'CCFFFF':
  3540. $colorIdxFg = 0x29;
  3541. break;
  3542. case 'CCFFCC':
  3543. $colorIdxFg = 0x2A;
  3544. break;
  3545. case 'FFFF99':
  3546. $colorIdxFg = 0x2B;
  3547. break;
  3548. case '99CCFF':
  3549. $colorIdxFg = 0x2C;
  3550. break;
  3551. case 'FF99CC':
  3552. $colorIdxFg = 0x2D;
  3553. break;
  3554. case 'CC99FF':
  3555. $colorIdxFg = 0x2E;
  3556. break;
  3557. case 'FFCC99':
  3558. $colorIdxFg = 0x2F;
  3559. break;
  3560. case '3366FF':
  3561. $colorIdxFg = 0x30;
  3562. break;
  3563. case '33CCCC':
  3564. $colorIdxFg = 0x31;
  3565. break;
  3566. case '99CC00':
  3567. $colorIdxFg = 0x32;
  3568. break;
  3569. case 'FFCC00':
  3570. $colorIdxFg = 0x33;
  3571. break;
  3572. case 'FF9900':
  3573. $colorIdxFg = 0x34;
  3574. break;
  3575. case 'FF6600':
  3576. $colorIdxFg = 0x35;
  3577. break;
  3578. case '666699':
  3579. $colorIdxFg = 0x36;
  3580. break;
  3581. case '969696':
  3582. $colorIdxFg = 0x37;
  3583. break;
  3584. case '003366':
  3585. $colorIdxFg = 0x38;
  3586. break;
  3587. case '339966':
  3588. $colorIdxFg = 0x39;
  3589. break;
  3590. case '003300':
  3591. $colorIdxFg = 0x3A;
  3592. break;
  3593. case '333300':
  3594. $colorIdxFg = 0x3B;
  3595. break;
  3596. case '993300':
  3597. $colorIdxFg = 0x3C;
  3598. break;
  3599. case '993366':
  3600. $colorIdxFg = 0x3D;
  3601. break;
  3602. case '333399':
  3603. $colorIdxFg = 0x3E;
  3604. break;
  3605. case '333333':
  3606. $colorIdxFg = 0x3F;
  3607. break;
  3608. default:
  3609. $colorIdxFg = 0x40;
  3610. break;
  3611. }
  3612. $dataBlockFill = pack('v', $blockFillPatternStyle);
  3613. $dataBlockFill .= pack('v', $colorIdxFg | ($colorIdxBg << 7));
  3614. }
  3615. if ($bFormatProt == 1) {
  3616. $dataBlockProtection = 0;
  3617. if ($conditional->getStyle()->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED) {
  3618. $dataBlockProtection = 1;
  3619. }
  3620. if ($conditional->getStyle()->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED) {
  3621. $dataBlockProtection = 1 << 1;
  3622. }
  3623. }
  3624. $data = pack('CCvvVv', $type, $operatorType, $szValue1, $szValue2, $flags, 0x0000);
  3625. if ($bFormatFont == 1) { // Block Formatting : OK
  3626. $data .= $dataBlockFont;
  3627. }
  3628. if ($bFormatAlign == 1) {
  3629. $data .= $dataBlockAlign;
  3630. }
  3631. if ($bFormatBorder == 1) {
  3632. $data .= $dataBlockBorder;
  3633. }
  3634. if ($bFormatFill == 1) { // Block Formatting : OK
  3635. $data .= $dataBlockFill;
  3636. }
  3637. if ($bFormatProt == 1) {
  3638. $data .= $dataBlockProtection;
  3639. }
  3640. if ($operand1 !== null) {
  3641. $data .= $operand1;
  3642. }
  3643. if ($operand2 !== null) {
  3644. $data .= $operand2;
  3645. }
  3646. $header = pack('vv', $record, strlen($data));
  3647. $this->append($header . $data);
  3648. }
  3649. /**
  3650. * Write CFHeader record.
  3651. */
  3652. private function writeCFHeader()
  3653. {
  3654. $record = 0x01B0; // Record identifier
  3655. $length = 0x0016; // Bytes to follow
  3656. $numColumnMin = null;
  3657. $numColumnMax = null;
  3658. $numRowMin = null;
  3659. $numRowMax = null;
  3660. $arrConditional = [];
  3661. foreach ($this->phpSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
  3662. foreach ($conditionalStyles as $conditional) {
  3663. if ($conditional->getConditionType() == Conditional::CONDITION_EXPRESSION
  3664. || $conditional->getConditionType() == Conditional::CONDITION_CELLIS) {
  3665. if (!in_array($conditional->getHashCode(), $arrConditional)) {
  3666. $arrConditional[] = $conditional->getHashCode();
  3667. }
  3668. // Cells
  3669. $arrCoord = Coordinate::coordinateFromString($cellCoordinate);
  3670. if (!is_numeric($arrCoord[0])) {
  3671. $arrCoord[0] = Coordinate::columnIndexFromString($arrCoord[0]);
  3672. }
  3673. if ($numColumnMin === null || ($numColumnMin > $arrCoord[0])) {
  3674. $numColumnMin = $arrCoord[0];
  3675. }
  3676. if ($numColumnMax === null || ($numColumnMax < $arrCoord[0])) {
  3677. $numColumnMax = $arrCoord[0];
  3678. }
  3679. if ($numRowMin === null || ($numRowMin > $arrCoord[1])) {
  3680. $numRowMin = $arrCoord[1];
  3681. }
  3682. if ($numRowMax === null || ($numRowMax < $arrCoord[1])) {
  3683. $numRowMax = $arrCoord[1];
  3684. }
  3685. }
  3686. }
  3687. }
  3688. $needRedraw = 1;
  3689. $cellRange = pack('vvvv', $numRowMin - 1, $numRowMax - 1, $numColumnMin - 1, $numColumnMax - 1);
  3690. $header = pack('vv', $record, $length);
  3691. $data = pack('vv', count($arrConditional), $needRedraw);
  3692. $data .= $cellRange;
  3693. $data .= pack('v', 0x0001);
  3694. $data .= $cellRange;
  3695. $this->append($header . $data);
  3696. }
  3697. }