Worksheet.php 160 KB

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