Worksheet.php 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534
  1. <?php
  2. /*
  3. * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  4. *
  5. * The majority of this is _NOT_ my code. I simply ported it from the
  6. * PERL Spreadsheet::WriteExcel module.
  7. *
  8. * The author of the Spreadsheet::WriteExcel module is John McNamara
  9. * <jmcnamara@cpan.org>
  10. *
  11. * I _DO_ maintain this code, and John McNamara has nothing to do with the
  12. * porting of this code to PHP. Any questions directly related to this
  13. * class library should be directed to me.
  14. *
  15. * License Information:
  16. *
  17. * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  18. * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  19. *
  20. * This library is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU Lesser General Public
  22. * License as published by the Free Software Foundation; either
  23. * version 2.1 of the License, or (at your option) any later version.
  24. *
  25. * This library is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * Lesser General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Lesser General Public
  31. * License along with this library; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. */
  34. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer/Parser.php';
  35. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer/BIFFwriter.php';
  36. /**
  37. * Class for generating Excel Spreadsheets
  38. *
  39. * @author Xavier Noguer <xnoguer@rezebra.com>
  40. * @category FileFormats
  41. * @package Spreadsheet_Excel_Writer
  42. */
  43. class Spreadsheet_Excel_Writer_Worksheet extends Spreadsheet_Excel_Writer_BIFFwriter
  44. {
  45. /**
  46. * Name of the Worksheet
  47. * @var string
  48. */
  49. var $name;
  50. /**
  51. * Index for the Worksheet
  52. * @var integer
  53. */
  54. var $index;
  55. /**
  56. * Reference to the (default) Format object for URLs
  57. * @var object Format
  58. */
  59. var $_url_format;
  60. /**
  61. * Reference to the parser used for parsing formulas
  62. * @var object Format
  63. */
  64. var $_parser;
  65. /**
  66. * Filehandle to the temporary file for storing data
  67. * @var resource
  68. */
  69. var $_filehandle;
  70. /**
  71. * Boolean indicating if we are using a temporary file for storing data
  72. * @var bool
  73. */
  74. var $_using_tmpfile;
  75. /**
  76. * Maximum number of rows for an Excel spreadsheet (BIFF5)
  77. * @var integer
  78. */
  79. var $_xls_rowmax;
  80. /**
  81. * Maximum number of columns for an Excel spreadsheet (BIFF5)
  82. * @var integer
  83. */
  84. var $_xls_colmax;
  85. /**
  86. * Maximum number of characters for a string (LABEL record in BIFF5)
  87. * @var integer
  88. */
  89. var $_xls_strmax;
  90. /**
  91. * First row for the DIMENSIONS record
  92. * @var integer
  93. * @see _storeDimensions()
  94. */
  95. var $_dim_rowmin;
  96. /**
  97. * Last row for the DIMENSIONS record
  98. * @var integer
  99. * @see _storeDimensions()
  100. */
  101. var $_dim_rowmax;
  102. /**
  103. * First column for the DIMENSIONS record
  104. * @var integer
  105. * @see _storeDimensions()
  106. */
  107. var $_dim_colmin;
  108. /**
  109. * Last column for the DIMENSIONS record
  110. * @var integer
  111. * @see _storeDimensions()
  112. */
  113. var $_dim_colmax;
  114. /**
  115. * Array containing format information for columns
  116. * @var array
  117. */
  118. var $_colinfo;
  119. /**
  120. * Array containing the selected area for the worksheet
  121. * @var array
  122. */
  123. var $_selection;
  124. /**
  125. * Array containing the panes for the worksheet
  126. * @var array
  127. */
  128. var $_panes;
  129. /**
  130. * The active pane for the worksheet
  131. * @var integer
  132. */
  133. var $_active_pane;
  134. /**
  135. * Bit specifying if panes are frozen
  136. * @var integer
  137. */
  138. var $_frozen;
  139. /**
  140. * Bit specifying if the worksheet is selected
  141. * @var integer
  142. */
  143. var $selected;
  144. /**
  145. * The paper size (for printing) (DOCUMENT!!!)
  146. * @var integer
  147. */
  148. var $_paper_size;
  149. /**
  150. * Bit specifying paper orientation (for printing). 0 => landscape, 1 => portrait
  151. * @var integer
  152. */
  153. var $_orientation;
  154. /**
  155. * The page header caption
  156. * @var string
  157. */
  158. var $_header;
  159. /**
  160. * The page footer caption
  161. * @var string
  162. */
  163. var $_footer;
  164. /**
  165. * The horizontal centering value for the page
  166. * @var integer
  167. */
  168. var $_hcenter;
  169. /**
  170. * The vertical centering value for the page
  171. * @var integer
  172. */
  173. var $_vcenter;
  174. /**
  175. * The margin for the header
  176. * @var float
  177. */
  178. var $_margin_head;
  179. /**
  180. * The margin for the footer
  181. * @var float
  182. */
  183. var $_margin_foot;
  184. /**
  185. * The left margin for the worksheet in inches
  186. * @var float
  187. */
  188. var $_margin_left;
  189. /**
  190. * The right margin for the worksheet in inches
  191. * @var float
  192. */
  193. var $_margin_right;
  194. /**
  195. * The top margin for the worksheet in inches
  196. * @var float
  197. */
  198. var $_margin_top;
  199. /**
  200. * The bottom margin for the worksheet in inches
  201. * @var float
  202. */
  203. var $_margin_bottom;
  204. /**
  205. * First row to reapeat on each printed page
  206. * @var integer
  207. */
  208. var $title_rowmin;
  209. /**
  210. * Last row to reapeat on each printed page
  211. * @var integer
  212. */
  213. var $title_rowmax;
  214. /**
  215. * First column to reapeat on each printed page
  216. * @var integer
  217. */
  218. var $title_colmin;
  219. /**
  220. * First row of the area to print
  221. * @var integer
  222. */
  223. var $print_rowmin;
  224. /**
  225. * Last row to of the area to print
  226. * @var integer
  227. */
  228. var $print_rowmax;
  229. /**
  230. * First column of the area to print
  231. * @var integer
  232. */
  233. var $print_colmin;
  234. /**
  235. * Last column of the area to print
  236. * @var integer
  237. */
  238. var $print_colmax;
  239. /**
  240. * Whether to use outline.
  241. * @var integer
  242. */
  243. var $_outline_on;
  244. /**
  245. * Auto outline styles.
  246. * @var bool
  247. */
  248. var $_outline_style;
  249. /**
  250. * Whether to have outline summary below.
  251. * @var bool
  252. */
  253. var $_outline_below;
  254. /**
  255. * Whether to have outline summary at the right.
  256. * @var bool
  257. */
  258. var $_outline_right;
  259. /**
  260. * Outline row level.
  261. * @var integer
  262. */
  263. var $_outline_row_level;
  264. /**
  265. * Whether to fit to page when printing or not.
  266. * @var bool
  267. */
  268. var $_fit_page;
  269. /**
  270. * Number of pages to fit wide
  271. * @var integer
  272. */
  273. var $_fit_width;
  274. /**
  275. * Number of pages to fit high
  276. * @var integer
  277. */
  278. var $_fit_height;
  279. /**
  280. * Reference to the total number of strings in the workbook
  281. * @var integer
  282. */
  283. var $_str_total;
  284. /**
  285. * Reference to the number of unique strings in the workbook
  286. * @var integer
  287. */
  288. var $_str_unique;
  289. /**
  290. * Reference to the array containing all the unique strings in the workbook
  291. * @var array
  292. */
  293. var $_str_table;
  294. /**
  295. * Merged cell ranges
  296. * @var array
  297. */
  298. var $_merged_ranges;
  299. /**
  300. * Charset encoding currently used when calling writeString()
  301. * @var string
  302. */
  303. var $_input_encoding;
  304. /**
  305. * Constructor
  306. *
  307. * @param string $name The name of the new worksheet
  308. * @param integer $index The index of the new worksheet
  309. * @param mixed &$activesheet The current activesheet of the workbook we belong to
  310. * @param mixed &$firstsheet The first worksheet in the workbook we belong to
  311. * @param mixed &$url_format The default format for hyperlinks
  312. * @param mixed &$parser The formula parser created for the Workbook
  313. * @param string $tmp_dir The path to the directory for temporary files
  314. * @access private
  315. */
  316. function Spreadsheet_Excel_Writer_Worksheet($BIFF_version, $name,
  317. $index, &$activesheet,
  318. &$firstsheet, &$str_total,
  319. &$str_unique, &$str_table,
  320. &$url_format, &$parser,
  321. $tmp_dir)
  322. {
  323. // It needs to call its parent's constructor explicitly
  324. $this->Spreadsheet_Excel_Writer_BIFFwriter();
  325. $this->_BIFF_version = $BIFF_version;
  326. $rowmax = 65536; // 16384 in Excel 5
  327. $colmax = 256;
  328. $this->name = $name;
  329. $this->index = $index;
  330. $this->activesheet = &$activesheet;
  331. $this->firstsheet = &$firstsheet;
  332. $this->_str_total = &$str_total;
  333. $this->_str_unique = &$str_unique;
  334. $this->_str_table = &$str_table;
  335. $this->_url_format = &$url_format;
  336. $this->_parser = &$parser;
  337. //$this->ext_sheets = array();
  338. $this->_filehandle = '';
  339. $this->_using_tmpfile = true;
  340. //$this->fileclosed = 0;
  341. //$this->offset = 0;
  342. $this->_xls_rowmax = $rowmax;
  343. $this->_xls_colmax = $colmax;
  344. $this->_xls_strmax = 255;
  345. $this->_dim_rowmin = $rowmax + 1;
  346. $this->_dim_rowmax = 0;
  347. $this->_dim_colmin = $colmax + 1;
  348. $this->_dim_colmax = 0;
  349. $this->_colinfo = array();
  350. $this->_selection = array(0,0,0,0);
  351. $this->_panes = array();
  352. $this->_active_pane = 3;
  353. $this->_frozen = 0;
  354. $this->selected = 0;
  355. $this->_paper_size = 0x0;
  356. $this->_orientation = 0x1;
  357. $this->_header = '';
  358. $this->_footer = '';
  359. $this->_hcenter = 0;
  360. $this->_vcenter = 0;
  361. $this->_margin_head = 0.50;
  362. $this->_margin_foot = 0.50;
  363. $this->_margin_left = 0.75;
  364. $this->_margin_right = 0.75;
  365. $this->_margin_top = 1.00;
  366. $this->_margin_bottom = 1.00;
  367. $this->title_rowmin = null;
  368. $this->title_rowmax = null;
  369. $this->title_colmin = null;
  370. $this->title_colmax = null;
  371. $this->print_rowmin = null;
  372. $this->print_rowmax = null;
  373. $this->print_colmin = null;
  374. $this->print_colmax = null;
  375. $this->_print_gridlines = 1;
  376. $this->_screen_gridlines = 1;
  377. $this->_print_headers = 0;
  378. $this->_fit_page = 0;
  379. $this->_fit_width = 0;
  380. $this->_fit_height = 0;
  381. $this->_hbreaks = array();
  382. $this->_vbreaks = array();
  383. $this->_protect = 0;
  384. $this->_password = null;
  385. $this->col_sizes = array();
  386. $this->_row_sizes = array();
  387. $this->_zoom = 100;
  388. $this->_print_scale = 100;
  389. $this->_outline_row_level = 0;
  390. $this->_outline_style = 0;
  391. $this->_outline_below = 1;
  392. $this->_outline_right = 1;
  393. $this->_outline_on = 1;
  394. $this->_merged_ranges = array();
  395. $this->_input_encoding = '';
  396. $this->_dv = array();
  397. $this->_tmp_dir = $tmp_dir;
  398. $this->_initialize();
  399. }
  400. /**
  401. * Open a tmp file to store the majority of the Worksheet data. If this fails,
  402. * for example due to write permissions, store the data in memory. This can be
  403. * slow for large files.
  404. *
  405. * @access private
  406. */
  407. function _initialize()
  408. {
  409. if ($this->_using_tmpfile == false) {
  410. return;
  411. }
  412. if ($this->_tmp_dir === '' && ini_get('open_basedir') === false) {
  413. // open_basedir restriction in effect - store data in memory
  414. // ToDo: Let the error actually have an effect somewhere
  415. $this->_using_tmpfile = false;
  416. return new PEAR_Error('Temp file could not be opened since open_basedir restriction in effect - please use setTmpDir() - using memory storage instead');
  417. }
  418. // Open tmp file for storing Worksheet data
  419. if ($this->_tmp_dir === '') {
  420. $fh = tmpfile();
  421. } else {
  422. // For people with open base dir restriction
  423. $tmpfilename = tempnam($this->_tmp_dir, "Spreadsheet_Excel_Writer");
  424. $fh = @fopen($tmpfilename, "w+b");
  425. }
  426. if ($fh === false) {
  427. // If tmpfile() fails store data in memory
  428. $this->_using_tmpfile = false;
  429. } else {
  430. // Store filehandle
  431. $this->_filehandle = $fh;
  432. }
  433. }
  434. /**
  435. * Add data to the beginning of the workbook (note the reverse order)
  436. * and to the end of the workbook.
  437. *
  438. * @access public
  439. * @see Spreadsheet_Excel_Writer_Workbook::storeWorkbook()
  440. * @param array $sheetnames The array of sheetnames from the Workbook this
  441. * worksheet belongs to
  442. */
  443. function close($sheetnames)
  444. {
  445. $num_sheets = count($sheetnames);
  446. /***********************************************
  447. * Prepend in reverse order!!
  448. */
  449. // Prepend the sheet dimensions
  450. $this->_storeDimensions();
  451. // Prepend the sheet password
  452. $this->_storePassword();
  453. // Prepend the sheet protection
  454. $this->_storeProtect();
  455. // Prepend the page setup
  456. $this->_storeSetup();
  457. /* FIXME: margins are actually appended */
  458. // Prepend the bottom margin
  459. $this->_storeMarginBottom();
  460. // Prepend the top margin
  461. $this->_storeMarginTop();
  462. // Prepend the right margin
  463. $this->_storeMarginRight();
  464. // Prepend the left margin
  465. $this->_storeMarginLeft();
  466. // Prepend the page vertical centering
  467. $this->_storeVcenter();
  468. // Prepend the page horizontal centering
  469. $this->_storeHcenter();
  470. // Prepend the page footer
  471. $this->_storeFooter();
  472. // Prepend the page header
  473. $this->_storeHeader();
  474. // Prepend the vertical page breaks
  475. $this->_storeVbreak();
  476. // Prepend the horizontal page breaks
  477. $this->_storeHbreak();
  478. // Prepend WSBOOL
  479. $this->_storeWsbool();
  480. // Prepend GRIDSET
  481. $this->_storeGridset();
  482. // Prepend GUTS
  483. if ($this->_BIFF_version == 0x0500) {
  484. $this->_storeGuts();
  485. }
  486. // Prepend PRINTGRIDLINES
  487. $this->_storePrintGridlines();
  488. // Prepend PRINTHEADERS
  489. $this->_storePrintHeaders();
  490. // Prepend EXTERNSHEET references
  491. if ($this->_BIFF_version == 0x0500) {
  492. for ($i = $num_sheets; $i > 0; $i--) {
  493. $sheetname = $sheetnames[$i-1];
  494. $this->_storeExternsheet($sheetname);
  495. }
  496. }
  497. // Prepend the EXTERNCOUNT of external references.
  498. if ($this->_BIFF_version == 0x0500) {
  499. $this->_storeExterncount($num_sheets);
  500. }
  501. // Prepend the COLINFO records if they exist
  502. if (!empty($this->_colinfo)) {
  503. $colcount = count($this->_colinfo);
  504. for ($i = 0; $i < $colcount; $i++) {
  505. $this->_storeColinfo($this->_colinfo[$i]);
  506. }
  507. $this->_storeDefcol();
  508. }
  509. // Prepend the BOF record
  510. $this->_storeBof(0x0010);
  511. /*
  512. * End of prepend. Read upwards from here.
  513. ***********************************************/
  514. // Append
  515. $this->_storeWindow2();
  516. $this->_storeZoom();
  517. if (!empty($this->_panes)) {
  518. $this->_storePanes($this->_panes);
  519. }
  520. $this->_storeSelection($this->_selection);
  521. $this->_storeMergedCells();
  522. /* TODO: add data validity */
  523. /*if ($this->_BIFF_version == 0x0600) {
  524. $this->_storeDataValidity();
  525. }*/
  526. $this->_storeEof();
  527. }
  528. /**
  529. * Retrieve the worksheet name.
  530. * This is usefull when creating worksheets without a name.
  531. *
  532. * @access public
  533. * @return string The worksheet's name
  534. */
  535. function getName()
  536. {
  537. return $this->name;
  538. }
  539. /**
  540. * Retrieves data from memory in one chunk, or from disk in $buffer
  541. * sized chunks.
  542. *
  543. * @return string The data
  544. */
  545. function getData()
  546. {
  547. $buffer = 4096;
  548. // Return data stored in memory
  549. if (isset($this->_data)) {
  550. $tmp = $this->_data;
  551. unset($this->_data);
  552. $fh = $this->_filehandle;
  553. if ($this->_using_tmpfile) {
  554. fseek($fh, 0);
  555. }
  556. return $tmp;
  557. }
  558. // Return data stored on disk
  559. if ($this->_using_tmpfile) {
  560. if ($tmp = fread($this->_filehandle, $buffer)) {
  561. return $tmp;
  562. }
  563. }
  564. // No data to return
  565. return '';
  566. }
  567. /**
  568. * Sets a merged cell range
  569. *
  570. * @access public
  571. * @param integer $first_row First row of the area to merge
  572. * @param integer $first_col First column of the area to merge
  573. * @param integer $last_row Last row of the area to merge
  574. * @param integer $last_col Last column of the area to merge
  575. */
  576. function setMerge($first_row, $first_col, $last_row, $last_col)
  577. {
  578. if (($last_row < $first_row) || ($last_col < $first_col)) {
  579. return;
  580. }
  581. // don't check rowmin, rowmax, etc... because we don't know when this
  582. // is going to be called
  583. $this->_merged_ranges[] = array($first_row, $first_col, $last_row, $last_col);
  584. }
  585. /**
  586. * Set this worksheet as a selected worksheet,
  587. * i.e. the worksheet has its tab highlighted.
  588. *
  589. * @access public
  590. */
  591. function select()
  592. {
  593. $this->selected = 1;
  594. }
  595. /**
  596. * Set this worksheet as the active worksheet,
  597. * i.e. the worksheet that is displayed when the workbook is opened.
  598. * Also set it as selected.
  599. *
  600. * @access public
  601. */
  602. function activate()
  603. {
  604. $this->selected = 1;
  605. $this->activesheet = $this->index;
  606. }
  607. /**
  608. * Set this worksheet as the first visible sheet.
  609. * This is necessary when there are a large number of worksheets and the
  610. * activated worksheet is not visible on the screen.
  611. *
  612. * @access public
  613. */
  614. function setFirstSheet()
  615. {
  616. $this->firstsheet = $this->index;
  617. }
  618. /**
  619. * Set the worksheet protection flag
  620. * to prevent accidental modification and to
  621. * hide formulas if the locked and hidden format properties have been set.
  622. *
  623. * @access public
  624. * @param string $password The password to use for protecting the sheet.
  625. */
  626. function protect($password)
  627. {
  628. $this->_protect = 1;
  629. $this->_password = $this->_encodePassword($password);
  630. }
  631. /**
  632. * Set the width of a single column or a range of columns.
  633. *
  634. * @access public
  635. * @param integer $firstcol first column on the range
  636. * @param integer $lastcol last column on the range
  637. * @param integer $width width to set
  638. * @param mixed $format The optional XF format to apply to the columns
  639. * @param integer $hidden The optional hidden atribute
  640. * @param integer $level The optional outline level
  641. */
  642. function setColumn($firstcol, $lastcol, $width, $format = null, $hidden = 0, $level = 0)
  643. {
  644. $this->_colinfo[] = array($firstcol, $lastcol, $width, &$format, $hidden, $level);
  645. // Set width to zero if column is hidden
  646. $width = ($hidden) ? 0 : $width;
  647. for ($col = $firstcol; $col <= $lastcol; $col++) {
  648. $this->col_sizes[$col] = $width;
  649. }
  650. }
  651. /**
  652. * Set which cell or cells are selected in a worksheet
  653. *
  654. * @access public
  655. * @param integer $first_row first row in the selected quadrant
  656. * @param integer $first_column first column in the selected quadrant
  657. * @param integer $last_row last row in the selected quadrant
  658. * @param integer $last_column last column in the selected quadrant
  659. */
  660. function setSelection($first_row,$first_column,$last_row,$last_column)
  661. {
  662. $this->_selection = array($first_row,$first_column,$last_row,$last_column);
  663. }
  664. /**
  665. * Set panes and mark them as frozen.
  666. *
  667. * @access public
  668. * @param array $panes This is the only parameter received and is composed of the following:
  669. * 0 => Vertical split position,
  670. * 1 => Horizontal split position
  671. * 2 => Top row visible
  672. * 3 => Leftmost column visible
  673. * 4 => Active pane
  674. */
  675. function freezePanes($panes)
  676. {
  677. $this->_frozen = 1;
  678. $this->_panes = $panes;
  679. }
  680. /**
  681. * Set panes and mark them as unfrozen.
  682. *
  683. * @access public
  684. * @param array $panes This is the only parameter received and is composed of the following:
  685. * 0 => Vertical split position,
  686. * 1 => Horizontal split position
  687. * 2 => Top row visible
  688. * 3 => Leftmost column visible
  689. * 4 => Active pane
  690. */
  691. function thawPanes($panes)
  692. {
  693. $this->_frozen = 0;
  694. $this->_panes = $panes;
  695. }
  696. /**
  697. * Set the page orientation as portrait.
  698. *
  699. * @access public
  700. */
  701. function setPortrait()
  702. {
  703. $this->_orientation = 1;
  704. }
  705. /**
  706. * Set the page orientation as landscape.
  707. *
  708. * @access public
  709. */
  710. function setLandscape()
  711. {
  712. $this->_orientation = 0;
  713. }
  714. /**
  715. * Set the paper type. Ex. 1 = US Letter, 9 = A4
  716. *
  717. * @access public
  718. * @param integer $size The type of paper size to use
  719. */
  720. function setPaper($size = 0)
  721. {
  722. $this->_paper_size = $size;
  723. }
  724. /**
  725. * Set the page header caption and optional margin.
  726. *
  727. * @access public
  728. * @param string $string The header text
  729. * @param float $margin optional head margin in inches.
  730. */
  731. function setHeader($string,$margin = 0.50)
  732. {
  733. if (strlen($string) >= 255) {
  734. //carp 'Header string must be less than 255 characters';
  735. return;
  736. }
  737. $this->_header = $string;
  738. $this->_margin_head = $margin;
  739. }
  740. /**
  741. * Set the page footer caption and optional margin.
  742. *
  743. * @access public
  744. * @param string $string The footer text
  745. * @param float $margin optional foot margin in inches.
  746. */
  747. function setFooter($string,$margin = 0.50)
  748. {
  749. if (strlen($string) >= 255) {
  750. //carp 'Footer string must be less than 255 characters';
  751. return;
  752. }
  753. $this->_footer = $string;
  754. $this->_margin_foot = $margin;
  755. }
  756. /**
  757. * Center the page horinzontally.
  758. *
  759. * @access public
  760. * @param integer $center the optional value for centering. Defaults to 1 (center).
  761. */
  762. function centerHorizontally($center = 1)
  763. {
  764. $this->_hcenter = $center;
  765. }
  766. /**
  767. * Center the page vertically.
  768. *
  769. * @access public
  770. * @param integer $center the optional value for centering. Defaults to 1 (center).
  771. */
  772. function centerVertically($center = 1)
  773. {
  774. $this->_vcenter = $center;
  775. }
  776. /**
  777. * Set all the page margins to the same value in inches.
  778. *
  779. * @access public
  780. * @param float $margin The margin to set in inches
  781. */
  782. function setMargins($margin)
  783. {
  784. $this->setMarginLeft($margin);
  785. $this->setMarginRight($margin);
  786. $this->setMarginTop($margin);
  787. $this->setMarginBottom($margin);
  788. }
  789. /**
  790. * Set the left and right margins to the same value in inches.
  791. *
  792. * @access public
  793. * @param float $margin The margin to set in inches
  794. */
  795. function setMargins_LR($margin)
  796. {
  797. $this->setMarginLeft($margin);
  798. $this->setMarginRight($margin);
  799. }
  800. /**
  801. * Set the top and bottom margins to the same value in inches.
  802. *
  803. * @access public
  804. * @param float $margin The margin to set in inches
  805. */
  806. function setMargins_TB($margin)
  807. {
  808. $this->setMarginTop($margin);
  809. $this->setMarginBottom($margin);
  810. }
  811. /**
  812. * Set the left margin in inches.
  813. *
  814. * @access public
  815. * @param float $margin The margin to set in inches
  816. */
  817. function setMarginLeft($margin = 0.75)
  818. {
  819. $this->_margin_left = $margin;
  820. }
  821. /**
  822. * Set the right margin in inches.
  823. *
  824. * @access public
  825. * @param float $margin The margin to set in inches
  826. */
  827. function setMarginRight($margin = 0.75)
  828. {
  829. $this->_margin_right = $margin;
  830. }
  831. /**
  832. * Set the top margin in inches.
  833. *
  834. * @access public
  835. * @param float $margin The margin to set in inches
  836. */
  837. function setMarginTop($margin = 1.00)
  838. {
  839. $this->_margin_top = $margin;
  840. }
  841. /**
  842. * Set the bottom margin in inches.
  843. *
  844. * @access public
  845. * @param float $margin The margin to set in inches
  846. */
  847. function setMarginBottom($margin = 1.00)
  848. {
  849. $this->_margin_bottom = $margin;
  850. }
  851. /**
  852. * Set the rows to repeat at the top of each printed page.
  853. *
  854. * @access public
  855. * @param integer $first_row First row to repeat
  856. * @param integer $last_row Last row to repeat. Optional.
  857. */
  858. function repeatRows($first_row, $last_row = null)
  859. {
  860. $this->title_rowmin = $first_row;
  861. if (isset($last_row)) { //Second row is optional
  862. $this->title_rowmax = $last_row;
  863. } else {
  864. $this->title_rowmax = $first_row;
  865. }
  866. }
  867. /**
  868. * Set the columns to repeat at the left hand side of each printed page.
  869. *
  870. * @access public
  871. * @param integer $first_col First column to repeat
  872. * @param integer $last_col Last column to repeat. Optional.
  873. */
  874. function repeatColumns($first_col, $last_col = null)
  875. {
  876. $this->title_colmin = $first_col;
  877. if (isset($last_col)) { // Second col is optional
  878. $this->title_colmax = $last_col;
  879. } else {
  880. $this->title_colmax = $first_col;
  881. }
  882. }
  883. /**
  884. * Set the area of each worksheet that will be printed.
  885. *
  886. * @access public
  887. * @param integer $first_row First row of the area to print
  888. * @param integer $first_col First column of the area to print
  889. * @param integer $last_row Last row of the area to print
  890. * @param integer $last_col Last column of the area to print
  891. */
  892. function printArea($first_row, $first_col, $last_row, $last_col)
  893. {
  894. $this->print_rowmin = $first_row;
  895. $this->print_colmin = $first_col;
  896. $this->print_rowmax = $last_row;
  897. $this->print_colmax = $last_col;
  898. }
  899. /**
  900. * Set the option to hide gridlines on the printed page.
  901. *
  902. * @access public
  903. */
  904. function hideGridlines()
  905. {
  906. $this->_print_gridlines = 0;
  907. }
  908. /**
  909. * Set the option to hide gridlines on the worksheet (as seen on the screen).
  910. *
  911. * @access public
  912. */
  913. function hideScreenGridlines()
  914. {
  915. $this->_screen_gridlines = 0;
  916. }
  917. /**
  918. * Set the option to print the row and column headers on the printed page.
  919. *
  920. * @access public
  921. * @param integer $print Whether to print the headers or not. Defaults to 1 (print).
  922. */
  923. function printRowColHeaders($print = 1)
  924. {
  925. $this->_print_headers = $print;
  926. }
  927. /**
  928. * Set the vertical and horizontal number of pages that will define the maximum area printed.
  929. * It doesn't seem to work with OpenOffice.
  930. *
  931. * @access public
  932. * @param integer $width Maximun width of printed area in pages
  933. * @param integer $height Maximun heigth of printed area in pages
  934. * @see setPrintScale()
  935. */
  936. function fitToPages($width, $height)
  937. {
  938. $this->_fit_page = 1;
  939. $this->_fit_width = $width;
  940. $this->_fit_height = $height;
  941. }
  942. /**
  943. * Store the horizontal page breaks on a worksheet (for printing).
  944. * The breaks represent the row after which the break is inserted.
  945. *
  946. * @access public
  947. * @param array $breaks Array containing the horizontal page breaks
  948. */
  949. function setHPagebreaks($breaks)
  950. {
  951. foreach ($breaks as $break) {
  952. array_push($this->_hbreaks, $break);
  953. }
  954. }
  955. /**
  956. * Store the vertical page breaks on a worksheet (for printing).
  957. * The breaks represent the column after which the break is inserted.
  958. *
  959. * @access public
  960. * @param array $breaks Array containing the vertical page breaks
  961. */
  962. function setVPagebreaks($breaks)
  963. {
  964. foreach ($breaks as $break) {
  965. array_push($this->_vbreaks, $break);
  966. }
  967. }
  968. /**
  969. * Set the worksheet zoom factor.
  970. *
  971. * @access public
  972. * @param integer $scale The zoom factor
  973. */
  974. function setZoom($scale = 100)
  975. {
  976. // Confine the scale to Excel's range
  977. if ($scale < 10 || $scale > 400) {
  978. $this->raiseError("Zoom factor $scale outside range: 10 <= zoom <= 400");
  979. $scale = 100;
  980. }
  981. $this->_zoom = floor($scale);
  982. }
  983. /**
  984. * Set the scale factor for the printed page.
  985. * It turns off the "fit to page" option
  986. *
  987. * @access public
  988. * @param integer $scale The optional scale factor. Defaults to 100
  989. */
  990. function setPrintScale($scale = 100)
  991. {
  992. // Confine the scale to Excel's range
  993. if ($scale < 10 || $scale > 400) {
  994. $this->raiseError("Print scale $scale outside range: 10 <= zoom <= 400");
  995. $scale = 100;
  996. }
  997. // Turn off "fit to page" option
  998. $this->_fit_page = 0;
  999. $this->_print_scale = floor($scale);
  1000. }
  1001. /**
  1002. * Map to the appropriate write method acording to the token recieved.
  1003. *
  1004. * @access public
  1005. * @param integer $row The row of the cell we are writing to
  1006. * @param integer $col The column of the cell we are writing to
  1007. * @param mixed $token What we are writing
  1008. * @param mixed $format The optional format to apply to the cell
  1009. */
  1010. function write($row, $col, $token, $format = null)
  1011. {
  1012. // Check for a cell reference in A1 notation and substitute row and column
  1013. /*if ($_[0] =~ /^\D/) {
  1014. @_ = $this->_substituteCellref(@_);
  1015. }*/
  1016. if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
  1017. // Match number
  1018. return $this->writeNumber($row, $col, $token, $format);
  1019. } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
  1020. // Match http or ftp URL
  1021. return $this->writeUrl($row, $col, $token, '', $format);
  1022. } elseif (preg_match("/^mailto:/", $token)) {
  1023. // Match mailto:
  1024. return $this->writeUrl($row, $col, $token, '', $format);
  1025. } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
  1026. // Match internal or external sheet link
  1027. return $this->writeUrl($row, $col, $token, '', $format);
  1028. } elseif (preg_match("/^=/", $token)) {
  1029. // Match formula
  1030. return $this->writeFormula($row, $col, $token, $format);
  1031. } elseif ($token == '') {
  1032. // Match blank
  1033. return $this->writeBlank($row, $col, $format);
  1034. } else {
  1035. // Default: match string
  1036. return $this->writeString($row, $col, $token, $format);
  1037. }
  1038. }
  1039. /**
  1040. * Write an array of values as a row
  1041. *
  1042. * @access public
  1043. * @param integer $row The row we are writing to
  1044. * @param integer $col The first col (leftmost col) we are writing to
  1045. * @param array $val The array of values to write
  1046. * @param mixed $format The optional format to apply to the cell
  1047. * @return mixed PEAR_Error on failure
  1048. */
  1049. function writeRow($row, $col, $val, $format = null)
  1050. {
  1051. $retval = '';
  1052. if (is_array($val)) {
  1053. foreach ($val as $v) {
  1054. if (is_array($v)) {
  1055. $this->writeCol($row, $col, $v, $format);
  1056. } else {
  1057. $this->write($row, $col, $v, $format);
  1058. }
  1059. $col++;
  1060. }
  1061. } else {
  1062. $retval = new PEAR_Error('$val needs to be an array');
  1063. }
  1064. return($retval);
  1065. }
  1066. /**
  1067. * Write an array of values as a column
  1068. *
  1069. * @access public
  1070. * @param integer $row The first row (uppermost row) we are writing to
  1071. * @param integer $col The col we are writing to
  1072. * @param array $val The array of values to write
  1073. * @param mixed $format The optional format to apply to the cell
  1074. * @return mixed PEAR_Error on failure
  1075. */
  1076. function writeCol($row, $col, $val, $format = null)
  1077. {
  1078. $retval = '';
  1079. if (is_array($val)) {
  1080. foreach ($val as $v) {
  1081. $this->write($row, $col, $v, $format);
  1082. $row++;
  1083. }
  1084. } else {
  1085. $retval = new PEAR_Error('$val needs to be an array');
  1086. }
  1087. return($retval);
  1088. }
  1089. /**
  1090. * Returns an index to the XF record in the workbook
  1091. *
  1092. * @access private
  1093. * @param mixed &$format The optional XF format
  1094. * @return integer The XF record index
  1095. */
  1096. function _XF(&$format)
  1097. {
  1098. if ($format) {
  1099. return($format->getXfIndex());
  1100. } else {
  1101. return(0x0F);
  1102. }
  1103. }
  1104. /******************************************************************************
  1105. *******************************************************************************
  1106. *
  1107. * Internal methods
  1108. */
  1109. /**
  1110. * Store Worksheet data in memory using the parent's class append() or to a
  1111. * temporary file, the default.
  1112. *
  1113. * @access private
  1114. * @param string $data The binary data to append
  1115. */
  1116. function _append($data)
  1117. {
  1118. if ($this->_using_tmpfile) {
  1119. // Add CONTINUE records if necessary
  1120. if (strlen($data) > $this->_limit) {
  1121. $data = $this->_addContinue($data);
  1122. }
  1123. fwrite($this->_filehandle, $data);
  1124. $this->_datasize += strlen($data);
  1125. } else {
  1126. parent::_append($data);
  1127. }
  1128. }
  1129. /**
  1130. * Substitute an Excel cell reference in A1 notation for zero based row and
  1131. * column values in an argument list.
  1132. *
  1133. * Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
  1134. *
  1135. * @access private
  1136. * @param string $cell The cell reference. Or range of cells.
  1137. * @return array
  1138. */
  1139. function _substituteCellref($cell)
  1140. {
  1141. $cell = strtoupper($cell);
  1142. // Convert a column range: 'A:A' or 'B:G'
  1143. if (preg_match("/([A-I]?[A-Z]):([A-I]?[A-Z])/", $cell, $match)) {
  1144. list($no_use, $col1) = $this->_cellToRowcol($match[1] .'1'); // Add a dummy row
  1145. list($no_use, $col2) = $this->_cellToRowcol($match[2] .'1'); // Add a dummy row
  1146. return(array($col1, $col2));
  1147. }
  1148. // Convert a cell range: 'A1:B7'
  1149. if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/", $cell, $match)) {
  1150. list($row1, $col1) = $this->_cellToRowcol($match[1]);
  1151. list($row2, $col2) = $this->_cellToRowcol($match[2]);
  1152. return(array($row1, $col1, $row2, $col2));
  1153. }
  1154. // Convert a cell reference: 'A1' or 'AD2000'
  1155. if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) {
  1156. list($row1, $col1) = $this->_cellToRowcol($match[1]);
  1157. return(array($row1, $col1));
  1158. }
  1159. // TODO use real error codes
  1160. $this->raiseError("Unknown cell reference $cell", 0, PEAR_ERROR_DIE);
  1161. }
  1162. /**
  1163. * Convert an Excel cell reference in A1 notation to a zero based row and column
  1164. * reference; converts C1 to (0, 2).
  1165. *
  1166. * @access private
  1167. * @param string $cell The cell reference.
  1168. * @return array containing (row, column)
  1169. */
  1170. function _cellToRowcol($cell)
  1171. {
  1172. preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
  1173. $col = $match[1];
  1174. $row = $match[2];
  1175. // Convert base26 column string to number
  1176. $chars = split('', $col);
  1177. $expn = 0;
  1178. $col = 0;
  1179. while ($chars) {
  1180. $char = array_pop($chars); // LS char first
  1181. $col += (ord($char) -ord('A') +1) * pow(26,$expn);
  1182. $expn++;
  1183. }
  1184. // Convert 1-index to zero-index
  1185. $row--;
  1186. $col--;
  1187. return(array($row, $col));
  1188. }
  1189. /**
  1190. * Based on the algorithm provided by Daniel Rentz of OpenOffice.
  1191. *
  1192. * @access private
  1193. * @param string $plaintext The password to be encoded in plaintext.
  1194. * @return string The encoded password
  1195. */
  1196. function _encodePassword($plaintext)
  1197. {
  1198. $password = 0x0000;
  1199. $i = 1; // char position
  1200. // split the plain text password in its component characters
  1201. $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY);
  1202. foreach ($chars as $char) {
  1203. $value = ord($char) << $i; // shifted ASCII value
  1204. $rotated_bits = $value >> 15; // rotated bits beyond bit 15
  1205. $value &= 0x7fff; // first 15 bits
  1206. $password ^= ($value | $rotated_bits);
  1207. $i++;
  1208. }
  1209. $password ^= strlen($plaintext);
  1210. $password ^= 0xCE4B;
  1211. return($password);
  1212. }
  1213. /**
  1214. * This method sets the properties for outlining and grouping. The defaults
  1215. * correspond to Excel's defaults.
  1216. *
  1217. * @param bool $visible
  1218. * @param bool $symbols_below
  1219. * @param bool $symbols_right
  1220. * @param bool $auto_style
  1221. */
  1222. function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
  1223. {
  1224. $this->_outline_on = $visible;
  1225. $this->_outline_below = $symbols_below;
  1226. $this->_outline_right = $symbols_right;
  1227. $this->_outline_style = $auto_style;
  1228. // Ensure this is a boolean vale for Window2
  1229. if ($this->_outline_on) {
  1230. $this->_outline_on = 1;
  1231. }
  1232. }
  1233. /******************************************************************************
  1234. *******************************************************************************
  1235. *
  1236. * BIFF RECORDS
  1237. */
  1238. /**
  1239. * Write a double to the specified row and column (zero indexed).
  1240. * An integer can be written as a double. Excel will display an
  1241. * integer. $format is optional.
  1242. *
  1243. * Returns 0 : normal termination
  1244. * -2 : row or column out of range
  1245. *
  1246. * @access public
  1247. * @param integer $row Zero indexed row
  1248. * @param integer $col Zero indexed column
  1249. * @param float $num The number to write
  1250. * @param mixed $format The optional XF format
  1251. * @return integer
  1252. */
  1253. function writeNumber($row, $col, $num, $format = null)
  1254. {
  1255. $record = 0x0203; // Record identifier
  1256. $length = 0x000E; // Number of bytes to follow
  1257. $xf = $this->_XF($format); // The cell format
  1258. // Check that row and col are valid and store max and min values
  1259. if ($row >= $this->_xls_rowmax) {
  1260. return(-2);
  1261. }
  1262. if ($col >= $this->_xls_colmax) {
  1263. return(-2);
  1264. }
  1265. if ($row < $this->_dim_rowmin) {
  1266. $this->_dim_rowmin = $row;
  1267. }
  1268. if ($row > $this->_dim_rowmax) {
  1269. $this->_dim_rowmax = $row;
  1270. }
  1271. if ($col < $this->_dim_colmin) {
  1272. $this->_dim_colmin = $col;
  1273. }
  1274. if ($col > $this->_dim_colmax) {
  1275. $this->_dim_colmax = $col;
  1276. }
  1277. $header = pack("vv", $record, $length);
  1278. $data = pack("vvv", $row, $col, $xf);
  1279. $xl_double = pack("d", $num);
  1280. if ($this->_byte_order) { // if it's Big Endian
  1281. $xl_double = strrev($xl_double);
  1282. }
  1283. $this->_append($header.$data.$xl_double);
  1284. return(0);
  1285. }
  1286. /**
  1287. * Write a string to the specified row and column (zero indexed).
  1288. * NOTE: there is an Excel 5 defined limit of 255 characters.
  1289. * $format is optional.
  1290. * Returns 0 : normal termination
  1291. * -2 : row or column out of range
  1292. * -3 : long string truncated to 255 chars
  1293. *
  1294. * @access public
  1295. * @param integer $row Zero indexed row
  1296. * @param integer $col Zero indexed column
  1297. * @param string $str The string to write
  1298. * @param mixed $format The XF format for the cell
  1299. * @return integer
  1300. */
  1301. function writeString($row, $col, $str, $format = null)
  1302. {
  1303. if ($this->_BIFF_version == 0x0600) {
  1304. return $this->writeStringBIFF8($row, $col, $str, $format);
  1305. }
  1306. $strlen = strlen($str);
  1307. $record = 0x0204; // Record identifier
  1308. $length = 0x0008 + $strlen; // Bytes to follow
  1309. $xf = $this->_XF($format); // The cell format
  1310. $str_error = 0;
  1311. // Check that row and col are valid and store max and min values
  1312. if ($row >= $this->_xls_rowmax) {
  1313. return(-2);
  1314. }
  1315. if ($col >= $this->_xls_colmax) {
  1316. return(-2);
  1317. }
  1318. if ($row < $this->_dim_rowmin) {
  1319. $this->_dim_rowmin = $row;
  1320. }
  1321. if ($row > $this->_dim_rowmax) {
  1322. $this->_dim_rowmax = $row;
  1323. }
  1324. if ($col < $this->_dim_colmin) {
  1325. $this->_dim_colmin = $col;
  1326. }
  1327. if ($col > $this->_dim_colmax) {
  1328. $this->_dim_colmax = $col;
  1329. }
  1330. if ($strlen > $this->_xls_strmax) { // LABEL must be < 255 chars
  1331. $str = substr($str, 0, $this->_xls_strmax);
  1332. $length = 0x0008 + $this->_xls_strmax;
  1333. $strlen = $this->_xls_strmax;
  1334. $str_error = -3;
  1335. }
  1336. $header = pack("vv", $record, $length);
  1337. $data = pack("vvvv", $row, $col, $xf, $strlen);
  1338. $this->_append($header . $data . $str);
  1339. return($str_error);
  1340. }
  1341. /**
  1342. * Sets Input Encoding for writing strings
  1343. *
  1344. * @access public
  1345. * @param string $encoding The encoding. Ex: 'UTF-16LE', 'utf-8', 'ISO-859-7'
  1346. */
  1347. function setInputEncoding($encoding)
  1348. {
  1349. // Modified by Ivan Tcholakov, 06-AUG-2010.
  1350. //if ($encoding != 'UTF-16LE' && !function_exists('iconv')) {
  1351. if (!api_equal_encodings($encoding, 'UTF-16LE') && !api_is_encoding_supported($encoding)) {
  1352. //
  1353. $this->raiseError("Using an input encoding other than UTF-16LE requires PHP support for iconv");
  1354. }
  1355. $this->_input_encoding = $encoding;
  1356. }
  1357. /**
  1358. * Write a string to the specified row and column (zero indexed).
  1359. * This is the BIFF8 version (no 255 chars limit).
  1360. * $format is optional.
  1361. * Returns 0 : normal termination
  1362. * -2 : row or column out of range
  1363. * -3 : long string truncated to 255 chars
  1364. *
  1365. * @access public
  1366. * @param integer $row Zero indexed row
  1367. * @param integer $col Zero indexed column
  1368. * @param string $str The string to write
  1369. * @param mixed $format The XF format for the cell
  1370. * @return integer
  1371. */
  1372. function writeStringBIFF8($row, $col, $str, $format = null)
  1373. {
  1374. // Modified by Ivan Tcholakov, 06-AUG-2010.
  1375. //if ($this->_input_encoding == 'UTF-16LE')
  1376. if (api_equal_encodings($this->_input_encoding, 'UTF-16LE'))
  1377. //
  1378. {
  1379. // Modified by Ivan Tcholakov, 06-AUG-2010.
  1380. //$strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1381. $strlen = api_is_encoding_supported('UTF-16LE') ? api_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1382. //
  1383. $encoding = 0x1;
  1384. }
  1385. elseif ($this->_input_encoding != '')
  1386. {
  1387. // Modified by Ivan Tcholakov, 06-AUG-2010.
  1388. //$str = iconv($this->_input_encoding, 'UTF-16LE', $str);
  1389. //$strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1390. $str = api_convert_encoding($str, 'UTF-16LE', $this->_input_encoding);
  1391. $strlen = api_is_encoding_supported('UTF-16LE') ? api_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1392. //
  1393. $encoding = 0x1;
  1394. }
  1395. else
  1396. {
  1397. $strlen = strlen($str);
  1398. $encoding = 0x0;
  1399. }
  1400. $record = 0x00FD; // Record identifier
  1401. $length = 0x000A; // Bytes to follow
  1402. $xf = $this->_XF($format); // The cell format
  1403. $str_error = 0;
  1404. // Check that row and col are valid and store max and min values
  1405. if ($this->_checkRowCol($row, $col) == false) {
  1406. return -2;
  1407. }
  1408. $str = pack('vC', $strlen, $encoding).$str;
  1409. /* check if string is already present */
  1410. if (!isset($this->_str_table[$str])) {
  1411. $this->_str_table[$str] = $this->_str_unique++;
  1412. }
  1413. $this->_str_total++;
  1414. $header = pack('vv', $record, $length);
  1415. $data = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]);
  1416. $this->_append($header.$data);
  1417. return $str_error;
  1418. }
  1419. /**
  1420. * Check row and col before writing to a cell, and update the sheet's
  1421. * dimensions accordingly
  1422. *
  1423. * @access private
  1424. * @param integer $row Zero indexed row
  1425. * @param integer $col Zero indexed column
  1426. * @return boolean true for success, false if row and/or col are grester
  1427. * then maximums allowed.
  1428. */
  1429. function _checkRowCol($row, $col)
  1430. {
  1431. if ($row >= $this->_xls_rowmax) {
  1432. return false;
  1433. }
  1434. if ($col >= $this->_xls_colmax) {
  1435. return false;
  1436. }
  1437. if ($row < $this->_dim_rowmin) {
  1438. $this->_dim_rowmin = $row;
  1439. }
  1440. if ($row > $this->_dim_rowmax) {
  1441. $this->_dim_rowmax = $row;
  1442. }
  1443. if ($col < $this->_dim_colmin) {
  1444. $this->_dim_colmin = $col;
  1445. }
  1446. if ($col > $this->_dim_colmax) {
  1447. $this->_dim_colmax = $col;
  1448. }
  1449. return true;
  1450. }
  1451. /**
  1452. * Writes a note associated with the cell given by the row and column.
  1453. * NOTE records don't have a length limit.
  1454. *
  1455. * @access public
  1456. * @param integer $row Zero indexed row
  1457. * @param integer $col Zero indexed column
  1458. * @param string $note The note to write
  1459. */
  1460. function writeNote($row, $col, $note)
  1461. {
  1462. $note_length = strlen($note);
  1463. $record = 0x001C; // Record identifier
  1464. $max_length = 2048; // Maximun length for a NOTE record
  1465. //$length = 0x0006 + $note_length; // Bytes to follow
  1466. // Check that row and col are valid and store max and min values
  1467. if ($row >= $this->_xls_rowmax) {
  1468. return(-2);
  1469. }
  1470. if ($col >= $this->_xls_colmax) {
  1471. return(-2);
  1472. }
  1473. if ($row < $this->_dim_rowmin) {
  1474. $this->_dim_rowmin = $row;
  1475. }
  1476. if ($row > $this->_dim_rowmax) {
  1477. $this->_dim_rowmax = $row;
  1478. }
  1479. if ($col < $this->_dim_colmin) {
  1480. $this->_dim_colmin = $col;
  1481. }
  1482. if ($col > $this->_dim_colmax) {
  1483. $this->_dim_colmax = $col;
  1484. }
  1485. // Length for this record is no more than 2048 + 6
  1486. $length = 0x0006 + min($note_length, 2048);
  1487. $header = pack("vv", $record, $length);
  1488. $data = pack("vvv", $row, $col, $note_length);
  1489. $this->_append($header . $data . substr($note, 0, 2048));
  1490. for ($i = $max_length; $i < $note_length; $i += $max_length) {
  1491. $chunk = substr($note, $i, $max_length);
  1492. $length = 0x0006 + strlen($chunk);
  1493. $header = pack("vv", $record, $length);
  1494. $data = pack("vvv", -1, 0, strlen($chunk));
  1495. $this->_append($header.$data.$chunk);
  1496. }
  1497. return(0);
  1498. }
  1499. /**
  1500. * Write a blank cell to the specified row and column (zero indexed).
  1501. * A blank cell is used to specify formatting without adding a string
  1502. * or a number.
  1503. *
  1504. * A blank cell without a format serves no purpose. Therefore, we don't write
  1505. * a BLANK record unless a format is specified.
  1506. *
  1507. * Returns 0 : normal termination (including no format)
  1508. * -1 : insufficient number of arguments
  1509. * -2 : row or column out of range
  1510. *
  1511. * @access public
  1512. * @param integer $row Zero indexed row
  1513. * @param integer $col Zero indexed column
  1514. * @param mixed $format The XF format
  1515. */
  1516. function writeBlank($row, $col, $format)
  1517. {
  1518. // Don't write a blank cell unless it has a format
  1519. if (!$format) {
  1520. return(0);
  1521. }
  1522. $record = 0x0201; // Record identifier
  1523. $length = 0x0006; // Number of bytes to follow
  1524. $xf = $this->_XF($format); // The cell format
  1525. // Check that row and col are valid and store max and min values
  1526. if ($row >= $this->_xls_rowmax) {
  1527. return(-2);
  1528. }
  1529. if ($col >= $this->_xls_colmax) {
  1530. return(-2);
  1531. }
  1532. if ($row < $this->_dim_rowmin) {
  1533. $this->_dim_rowmin = $row;
  1534. }
  1535. if ($row > $this->_dim_rowmax) {
  1536. $this->_dim_rowmax = $row;
  1537. }
  1538. if ($col < $this->_dim_colmin) {
  1539. $this->_dim_colmin = $col;
  1540. }
  1541. if ($col > $this->_dim_colmax) {
  1542. $this->_dim_colmax = $col;
  1543. }
  1544. $header = pack("vv", $record, $length);
  1545. $data = pack("vvv", $row, $col, $xf);
  1546. $this->_append($header . $data);
  1547. return 0;
  1548. }
  1549. /**
  1550. * Write a formula to the specified row and column (zero indexed).
  1551. * The textual representation of the formula is passed to the parser in
  1552. * Parser.php which returns a packed binary string.
  1553. *
  1554. * Returns 0 : normal termination
  1555. * -1 : formula errors (bad formula)
  1556. * -2 : row or column out of range
  1557. *
  1558. * @access public
  1559. * @param integer $row Zero indexed row
  1560. * @param integer $col Zero indexed column
  1561. * @param string $formula The formula text string
  1562. * @param mixed $format The optional XF format
  1563. * @return integer
  1564. */
  1565. function writeFormula($row, $col, $formula, $format = null)
  1566. {
  1567. $record = 0x0006; // Record identifier
  1568. // Excel normally stores the last calculated value of the formula in $num.
  1569. // Clearly we are not in a position to calculate this a priori. Instead
  1570. // we set $num to zero and set the option flags in $grbit to ensure
  1571. // automatic calculation of the formula when the file is opened.
  1572. //
  1573. $xf = $this->_XF($format); // The cell format
  1574. $num = 0x00; // Current value of formula
  1575. $grbit = 0x03; // Option flags
  1576. $unknown = 0x0000; // Must be zero
  1577. // Check that row and col are valid and store max and min values
  1578. if ($this->_checkRowCol($row, $col) == false) {
  1579. return -2;
  1580. }
  1581. // Strip the '=' or '@' sign at the beginning of the formula string
  1582. if (preg_match("/^=/", $formula)) {
  1583. $formula = preg_replace("/(^=)/", "", $formula);
  1584. } elseif (preg_match("/^@/", $formula)) {
  1585. $formula = preg_replace("/(^@)/", "", $formula);
  1586. } else {
  1587. // Error handling
  1588. $this->writeString($row, $col, 'Unrecognised character for formula');
  1589. return -1;
  1590. }
  1591. // Parse the formula using the parser in Parser.php
  1592. $error = $this->_parser->parse($formula);
  1593. if ($this->isError($error)) {
  1594. $this->writeString($row, $col, $error->getMessage());
  1595. return -1;
  1596. }
  1597. $formula = $this->_parser->toReversePolish();
  1598. if ($this->isError($formula)) {
  1599. $this->writeString($row, $col, $formula->getMessage());
  1600. return -1;
  1601. }
  1602. $formlen = strlen($formula); // Length of the binary string
  1603. $length = 0x16 + $formlen; // Length of the record data
  1604. $header = pack("vv", $record, $length);
  1605. $data = pack("vvvdvVv", $row, $col, $xf, $num,
  1606. $grbit, $unknown, $formlen);
  1607. $this->_append($header . $data . $formula);
  1608. return 0;
  1609. }
  1610. /**
  1611. * Write a hyperlink.
  1612. * This is comprised of two elements: the visible label and
  1613. * the invisible link. The visible label is the same as the link unless an
  1614. * alternative string is specified. The label is written using the
  1615. * writeString() method. Therefore the 255 characters string limit applies.
  1616. * $string and $format are optional.
  1617. *
  1618. * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
  1619. * directory url.
  1620. *
  1621. * Returns 0 : normal termination
  1622. * -2 : row or column out of range
  1623. * -3 : long string truncated to 255 chars
  1624. *
  1625. * @access public
  1626. * @param integer $row Row
  1627. * @param integer $col Column
  1628. * @param string $url URL string
  1629. * @param string $string Alternative label
  1630. * @param mixed $format The cell format
  1631. * @return integer
  1632. */
  1633. function writeUrl($row, $col, $url, $string = '', $format = null)
  1634. {
  1635. // Add start row and col to arg list
  1636. return($this->_writeUrlRange($row, $col, $row, $col, $url, $string, $format));
  1637. }
  1638. /**
  1639. * This is the more general form of writeUrl(). It allows a hyperlink to be
  1640. * written to a range of cells. This function also decides the type of hyperlink
  1641. * to be written. These are either, Web (http, ftp, mailto), Internal
  1642. * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
  1643. *
  1644. * @access private
  1645. * @see writeUrl()
  1646. * @param integer $row1 Start row
  1647. * @param integer $col1 Start column
  1648. * @param integer $row2 End row
  1649. * @param integer $col2 End column
  1650. * @param string $url URL string
  1651. * @param string $string Alternative label
  1652. * @param mixed $format The cell format
  1653. * @return integer
  1654. */
  1655. function _writeUrlRange($row1, $col1, $row2, $col2, $url, $string = '', $format = null)
  1656. {
  1657. // Check for internal/external sheet links or default to web link
  1658. if (preg_match('[^internal:]', $url)) {
  1659. return($this->_writeUrlInternal($row1, $col1, $row2, $col2, $url, $string, $format));
  1660. }
  1661. if (preg_match('[^external:]', $url)) {
  1662. return($this->_writeUrlExternal($row1, $col1, $row2, $col2, $url, $string, $format));
  1663. }
  1664. return($this->_writeUrlWeb($row1, $col1, $row2, $col2, $url, $string, $format));
  1665. }
  1666. /**
  1667. * Used to write http, ftp and mailto hyperlinks.
  1668. * The link type ($options) is 0x03 is the same as absolute dir ref without
  1669. * sheet. However it is differentiated by the $unknown2 data stream.
  1670. *
  1671. * @access private
  1672. * @see writeUrl()
  1673. * @param integer $row1 Start row
  1674. * @param integer $col1 Start column
  1675. * @param integer $row2 End row
  1676. * @param integer $col2 End column
  1677. * @param string $url URL string
  1678. * @param string $str Alternative label
  1679. * @param mixed $format The cell format
  1680. * @return integer
  1681. */
  1682. function _writeUrlWeb($row1, $col1, $row2, $col2, $url, $str, $format = null)
  1683. {
  1684. $record = 0x01B8; // Record identifier
  1685. $length = 0x00000; // Bytes to follow
  1686. if (!$format) {
  1687. $format = $this->_url_format;
  1688. }
  1689. // Write the visible label using the writeString() method.
  1690. if ($str == '') {
  1691. $str = $url;
  1692. }
  1693. $str_error = $this->writeString($row1, $col1, $str, $format);
  1694. if (($str_error == -2) || ($str_error == -3)) {
  1695. return $str_error;
  1696. }
  1697. // Pack the undocumented parts of the hyperlink stream
  1698. $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
  1699. $unknown2 = pack("H*", "E0C9EA79F9BACE118C8200AA004BA90B");
  1700. // Pack the option flags
  1701. $options = pack("V", 0x03);
  1702. // Convert URL to a null terminated wchar string
  1703. $url = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
  1704. $url = $url . "\0\0\0";
  1705. // Pack the length of the URL
  1706. $url_len = pack("V", strlen($url));
  1707. // Calculate the data length
  1708. $length = 0x34 + strlen($url);
  1709. // Pack the header data
  1710. $header = pack("vv", $record, $length);
  1711. $data = pack("vvvv", $row1, $row2, $col1, $col2);
  1712. // Write the packed data
  1713. $this->_append($header . $data .
  1714. $unknown1 . $options .
  1715. $unknown2 . $url_len . $url);
  1716. return($str_error);
  1717. }
  1718. /**
  1719. * Used to write internal reference hyperlinks such as "Sheet1!A1".
  1720. *
  1721. * @access private
  1722. * @see writeUrl()
  1723. * @param integer $row1 Start row
  1724. * @param integer $col1 Start column
  1725. * @param integer $row2 End row
  1726. * @param integer $col2 End column
  1727. * @param string $url URL string
  1728. * @param string $str Alternative label
  1729. * @param mixed $format The cell format
  1730. * @return integer
  1731. */
  1732. function _writeUrlInternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
  1733. {
  1734. $record = 0x01B8; // Record identifier
  1735. $length = 0x00000; // Bytes to follow
  1736. if (!$format) {
  1737. $format = $this->_url_format;
  1738. }
  1739. // Strip URL type
  1740. $url = preg_replace('/^internal:/', '', $url);
  1741. // Write the visible label
  1742. if ($str == '') {
  1743. $str = $url;
  1744. }
  1745. $str_error = $this->writeString($row1, $col1, $str, $format);
  1746. if (($str_error == -2) || ($str_error == -3)) {
  1747. return $str_error;
  1748. }
  1749. // Pack the undocumented parts of the hyperlink stream
  1750. $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
  1751. // Pack the option flags
  1752. $options = pack("V", 0x08);
  1753. // Convert the URL type and to a null terminated wchar string
  1754. $url = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
  1755. $url = $url . "\0\0\0";
  1756. // Pack the length of the URL as chars (not wchars)
  1757. $url_len = pack("V", floor(strlen($url)/2));
  1758. // Calculate the data length
  1759. $length = 0x24 + strlen($url);
  1760. // Pack the header data
  1761. $header = pack("vv", $record, $length);
  1762. $data = pack("vvvv", $row1, $row2, $col1, $col2);
  1763. // Write the packed data
  1764. $this->_append($header . $data .
  1765. $unknown1 . $options .
  1766. $url_len . $url);
  1767. return($str_error);
  1768. }
  1769. /**
  1770. * Write links to external directory names such as 'c:\foo.xls',
  1771. * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
  1772. *
  1773. * Note: Excel writes some relative links with the $dir_long string. We ignore
  1774. * these cases for the sake of simpler code.
  1775. *
  1776. * @access private
  1777. * @see writeUrl()
  1778. * @param integer $row1 Start row
  1779. * @param integer $col1 Start column
  1780. * @param integer $row2 End row
  1781. * @param integer $col2 End column
  1782. * @param string $url URL string
  1783. * @param string $str Alternative label
  1784. * @param mixed $format The cell format
  1785. * @return integer
  1786. */
  1787. function _writeUrlExternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
  1788. {
  1789. // Network drives are different. We will handle them separately
  1790. // MS/Novell network drives and shares start with \\
  1791. if (preg_match('[^external:\\\\]', $url)) {
  1792. return; //($this->_writeUrlExternal_net($row1, $col1, $row2, $col2, $url, $str, $format));
  1793. }
  1794. $record = 0x01B8; // Record identifier
  1795. $length = 0x00000; // Bytes to follow
  1796. if (!$format) {
  1797. $format = $this->_url_format;
  1798. }
  1799. // Strip URL type and change Unix dir separator to Dos style (if needed)
  1800. //
  1801. $url = preg_replace('/^external:/', '', $url);
  1802. $url = preg_replace('/\//', "\\", $url);
  1803. // Write the visible label
  1804. if ($str == '') {
  1805. $str = preg_replace('/\#/', ' - ', $url);
  1806. }
  1807. $str_error = $this->writeString($row1, $col1, $str, $format);
  1808. if (($str_error == -2) or ($str_error == -3)) {
  1809. return $str_error;
  1810. }
  1811. // Determine if the link is relative or absolute:
  1812. // relative if link contains no dir separator, "somefile.xls"
  1813. // relative if link starts with up-dir, "..\..\somefile.xls"
  1814. // otherwise, absolute
  1815. $absolute = 0x02; // Bit mask
  1816. if (!preg_match("/\\\/", $url)) {
  1817. $absolute = 0x00;
  1818. }
  1819. if (preg_match("/^\.\.\\\/", $url)) {
  1820. $absolute = 0x00;
  1821. }
  1822. $link_type = 0x01 | $absolute;
  1823. // Determine if the link contains a sheet reference and change some of the
  1824. // parameters accordingly.
  1825. // Split the dir name and sheet name (if it exists)
  1826. /*if (preg_match("/\#/", $url)) {
  1827. list($dir_long, $sheet) = split("\#", $url);
  1828. } else {
  1829. $dir_long = $url;
  1830. }
  1831. if (isset($sheet)) {
  1832. $link_type |= 0x08;
  1833. $sheet_len = pack("V", strlen($sheet) + 0x01);
  1834. $sheet = join("\0", split('', $sheet));
  1835. $sheet .= "\0\0\0";
  1836. } else {
  1837. $sheet_len = '';
  1838. $sheet = '';
  1839. }*/
  1840. $dir_long = $url;
  1841. if (preg_match("/\#/", $url)) {
  1842. $link_type |= 0x08;
  1843. }
  1844. // Pack the link type
  1845. $link_type = pack("V", $link_type);
  1846. // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
  1847. $up_count = preg_match_all("/\.\.\\\/", $dir_long, $useless);
  1848. $up_count = pack("v", $up_count);
  1849. // Store the short dos dir name (null terminated)
  1850. $dir_short = preg_replace("/\.\.\\\/", '', $dir_long) . "\0";
  1851. // Store the long dir name as a wchar string (non-null terminated)
  1852. //$dir_long = join("\0", split('', $dir_long));
  1853. $dir_long = $dir_long . "\0";
  1854. // Pack the lengths of the dir strings
  1855. $dir_short_len = pack("V", strlen($dir_short) );
  1856. $dir_long_len = pack("V", strlen($dir_long) );
  1857. $stream_len = pack("V", 0);//strlen($dir_long) + 0x06);
  1858. // Pack the undocumented parts of the hyperlink stream
  1859. $unknown1 = pack("H*",'D0C9EA79F9BACE118C8200AA004BA90B02000000' );
  1860. $unknown2 = pack("H*",'0303000000000000C000000000000046' );
  1861. $unknown3 = pack("H*",'FFFFADDE000000000000000000000000000000000000000');
  1862. $unknown4 = pack("v", 0x03 );
  1863. // Pack the main data stream
  1864. $data = pack("vvvv", $row1, $row2, $col1, $col2) .
  1865. $unknown1 .
  1866. $link_type .
  1867. $unknown2 .
  1868. $up_count .
  1869. $dir_short_len.
  1870. $dir_short .
  1871. $unknown3 .
  1872. $stream_len ;/*.
  1873. $dir_long_len .
  1874. $unknown4 .
  1875. $dir_long .
  1876. $sheet_len .
  1877. $sheet ;*/
  1878. // Pack the header data
  1879. $length = strlen($data);
  1880. $header = pack("vv", $record, $length);
  1881. // Write the packed data
  1882. $this->_append($header. $data);
  1883. return($str_error);
  1884. }
  1885. /**
  1886. * This method is used to set the height and format for a row.
  1887. *
  1888. * @access public
  1889. * @param integer $row The row to set
  1890. * @param integer $height Height we are giving to the row.
  1891. * Use null to set XF without setting height
  1892. * @param mixed $format XF format we are giving to the row
  1893. * @param bool $hidden The optional hidden attribute
  1894. * @param integer $level The optional outline level for row, in range [0,7]
  1895. */
  1896. function setRow($row, $height, $format = null, $hidden = false, $level = 0)
  1897. {
  1898. $record = 0x0208; // Record identifier
  1899. $length = 0x0010; // Number of bytes to follow
  1900. $colMic = 0x0000; // First defined column
  1901. $colMac = 0x0000; // Last defined column
  1902. $irwMac = 0x0000; // Used by Excel to optimise loading
  1903. $reserved = 0x0000; // Reserved
  1904. $grbit = 0x0000; // Option flags
  1905. $ixfe = $this->_XF($format); // XF index
  1906. // set _row_sizes so _sizeRow() can use it
  1907. $this->_row_sizes[$row] = $height;
  1908. // Use setRow($row, null, $XF) to set XF format without setting height
  1909. if ($height != null) {
  1910. $miyRw = $height * 20; // row height
  1911. } else {
  1912. $miyRw = 0xff; // default row height is 256
  1913. }
  1914. $level = max(0, min($level, 7)); // level should be between 0 and 7
  1915. $this->_outline_row_level = max($level, $this->_outline_row_level);
  1916. // Set the options flags. fUnsynced is used to show that the font and row
  1917. // heights are not compatible. This is usually the case for WriteExcel.
  1918. // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
  1919. // is collapsed. Instead it is used to indicate that the previous row is
  1920. // collapsed. The zero height flag, 0x20, is used to collapse a row.
  1921. $grbit |= $level;
  1922. if ($hidden) {
  1923. $grbit |= 0x0020;
  1924. }
  1925. $grbit |= 0x0040; // fUnsynced
  1926. if ($format) {
  1927. $grbit |= 0x0080;
  1928. }
  1929. $grbit |= 0x0100;
  1930. $header = pack("vv", $record, $length);
  1931. $data = pack("vvvvvvvv", $row, $colMic, $colMac, $miyRw,
  1932. $irwMac,$reserved, $grbit, $ixfe);
  1933. $this->_append($header.$data);
  1934. }
  1935. /**
  1936. * Writes Excel DIMENSIONS to define the area in which there is data.
  1937. *
  1938. * @access private
  1939. */
  1940. function _storeDimensions()
  1941. {
  1942. $record = 0x0200; // Record identifier
  1943. $row_min = $this->_dim_rowmin; // First row
  1944. $row_max = $this->_dim_rowmax + 1; // Last row plus 1
  1945. $col_min = $this->_dim_colmin; // First column
  1946. $col_max = $this->_dim_colmax + 1; // Last column plus 1
  1947. $reserved = 0x0000; // Reserved by Excel
  1948. if ($this->_BIFF_version == 0x0500) {
  1949. $length = 0x000A; // Number of bytes to follow
  1950. $data = pack("vvvvv", $row_min, $row_max,
  1951. $col_min, $col_max, $reserved);
  1952. } elseif ($this->_BIFF_version == 0x0600) {
  1953. $length = 0x000E;
  1954. $data = pack("VVvvv", $row_min, $row_max,
  1955. $col_min, $col_max, $reserved);
  1956. }
  1957. $header = pack("vv", $record, $length);
  1958. $this->_prepend($header.$data);
  1959. }
  1960. /**
  1961. * Write BIFF record Window2.
  1962. *
  1963. * @access private
  1964. */
  1965. function _storeWindow2()
  1966. {
  1967. $record = 0x023E; // Record identifier
  1968. if ($this->_BIFF_version == 0x0500) {
  1969. $length = 0x000A; // Number of bytes to follow
  1970. } elseif ($this->_BIFF_version == 0x0600) {
  1971. $length = 0x0012;
  1972. }
  1973. $grbit = 0x00B6; // Option flags
  1974. $rwTop = 0x0000; // Top row visible in window
  1975. $colLeft = 0x0000; // Leftmost column visible in window
  1976. // The options flags that comprise $grbit
  1977. $fDspFmla = 0; // 0 - bit
  1978. $fDspGrid = $this->_screen_gridlines; // 1
  1979. $fDspRwCol = 1; // 2
  1980. $fFrozen = $this->_frozen; // 3
  1981. $fDspZeros = 1; // 4
  1982. $fDefaultHdr = 1; // 5
  1983. $fArabic = 0; // 6
  1984. $fDspGuts = $this->_outline_on; // 7
  1985. $fFrozenNoSplit = 0; // 0 - bit
  1986. $fSelected = $this->selected; // 1
  1987. $fPaged = 1; // 2
  1988. $grbit = $fDspFmla;
  1989. $grbit |= $fDspGrid << 1;
  1990. $grbit |= $fDspRwCol << 2;
  1991. $grbit |= $fFrozen << 3;
  1992. $grbit |= $fDspZeros << 4;
  1993. $grbit |= $fDefaultHdr << 5;
  1994. $grbit |= $fArabic << 6;
  1995. $grbit |= $fDspGuts << 7;
  1996. $grbit |= $fFrozenNoSplit << 8;
  1997. $grbit |= $fSelected << 9;
  1998. $grbit |= $fPaged << 10;
  1999. $header = pack("vv", $record, $length);
  2000. $data = pack("vvv", $grbit, $rwTop, $colLeft);
  2001. // FIXME !!!
  2002. if ($this->_BIFF_version == 0x0500) {
  2003. $rgbHdr = 0x00000000; // Row/column heading and gridline color
  2004. $data .= pack("V", $rgbHdr);
  2005. } elseif ($this->_BIFF_version == 0x0600) {
  2006. $rgbHdr = 0x0040; // Row/column heading and gridline color index
  2007. $zoom_factor_page_break = 0x0000;
  2008. $zoom_factor_normal = 0x0000;
  2009. $data .= pack("vvvvV", $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
  2010. }
  2011. $this->_append($header.$data);
  2012. }
  2013. /**
  2014. * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
  2015. *
  2016. * @access private
  2017. */
  2018. function _storeDefcol()
  2019. {
  2020. $record = 0x0055; // Record identifier
  2021. $length = 0x0002; // Number of bytes to follow
  2022. $colwidth = 0x0008; // Default column width
  2023. $header = pack("vv", $record, $length);
  2024. $data = pack("v", $colwidth);
  2025. $this->_prepend($header . $data);
  2026. }
  2027. /**
  2028. * Write BIFF record COLINFO to define column widths
  2029. *
  2030. * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
  2031. * length record.
  2032. *
  2033. * @access private
  2034. * @param array $col_array This is the only parameter received and is composed of the following:
  2035. * 0 => First formatted column,
  2036. * 1 => Last formatted column,
  2037. * 2 => Col width (8.43 is Excel default),
  2038. * 3 => The optional XF format of the column,
  2039. * 4 => Option flags.
  2040. * 5 => Optional outline level
  2041. */
  2042. function _storeColinfo($col_array)
  2043. {
  2044. if (isset($col_array[0])) {
  2045. $colFirst = $col_array[0];
  2046. }
  2047. if (isset($col_array[1])) {
  2048. $colLast = $col_array[1];
  2049. }
  2050. if (isset($col_array[2])) {
  2051. $coldx = $col_array[2];
  2052. } else {
  2053. $coldx = 8.43;
  2054. }
  2055. if (isset($col_array[3])) {
  2056. $format = $col_array[3];
  2057. } else {
  2058. $format = 0;
  2059. }
  2060. if (isset($col_array[4])) {
  2061. $grbit = $col_array[4];
  2062. } else {
  2063. $grbit = 0;
  2064. }
  2065. if (isset($col_array[5])) {
  2066. $level = $col_array[5];
  2067. } else {
  2068. $level = 0;
  2069. }
  2070. $record = 0x007D; // Record identifier
  2071. $length = 0x000B; // Number of bytes to follow
  2072. $coldx += 0.72; // Fudge. Excel subtracts 0.72 !?
  2073. $coldx *= 256; // Convert to units of 1/256 of a char
  2074. $ixfe = $this->_XF($format);
  2075. $reserved = 0x00; // Reserved
  2076. $level = max(0, min($level, 7));
  2077. $grbit |= $level << 8;
  2078. $header = pack("vv", $record, $length);
  2079. $data = pack("vvvvvC", $colFirst, $colLast, $coldx,
  2080. $ixfe, $grbit, $reserved);
  2081. $this->_prepend($header.$data);
  2082. }
  2083. /**
  2084. * Write BIFF record SELECTION.
  2085. *
  2086. * @access private
  2087. * @param array $array array containing ($rwFirst,$colFirst,$rwLast,$colLast)
  2088. * @see setSelection()
  2089. */
  2090. function _storeSelection($array)
  2091. {
  2092. list($rwFirst,$colFirst,$rwLast,$colLast) = $array;
  2093. $record = 0x001D; // Record identifier
  2094. $length = 0x000F; // Number of bytes to follow
  2095. $pnn = $this->_active_pane; // Pane position
  2096. $rwAct = $rwFirst; // Active row
  2097. $colAct = $colFirst; // Active column
  2098. $irefAct = 0; // Active cell ref
  2099. $cref = 1; // Number of refs
  2100. if (!isset($rwLast)) {
  2101. $rwLast = $rwFirst; // Last row in reference
  2102. }
  2103. if (!isset($colLast)) {
  2104. $colLast = $colFirst; // Last col in reference
  2105. }
  2106. // Swap last row/col for first row/col as necessary
  2107. if ($rwFirst > $rwLast) {
  2108. list($rwFirst, $rwLast) = array($rwLast, $rwFirst);
  2109. }
  2110. if ($colFirst > $colLast) {
  2111. list($colFirst, $colLast) = array($colLast, $colFirst);
  2112. }
  2113. $header = pack("vv", $record, $length);
  2114. $data = pack("CvvvvvvCC", $pnn, $rwAct, $colAct,
  2115. $irefAct, $cref,
  2116. $rwFirst, $rwLast,
  2117. $colFirst, $colLast);
  2118. $this->_append($header . $data);
  2119. }
  2120. /**
  2121. * Store the MERGEDCELLS record for all ranges of merged cells
  2122. *
  2123. * @access private
  2124. */
  2125. function _storeMergedCells()
  2126. {
  2127. // if there are no merged cell ranges set, return
  2128. if (count($this->_merged_ranges) == 0) {
  2129. return;
  2130. }
  2131. $record = 0x00E5;
  2132. $length = 2 + count($this->_merged_ranges) * 8;
  2133. $header = pack('vv', $record, $length);
  2134. $data = pack('v', count($this->_merged_ranges));
  2135. foreach ($this->_merged_ranges as $range) {
  2136. $data .= pack('vvvv', $range[0], $range[2], $range[1], $range[3]);
  2137. }
  2138. $this->_append($header . $data);
  2139. }
  2140. /**
  2141. * Write BIFF record EXTERNCOUNT to indicate the number of external sheet
  2142. * references in a worksheet.
  2143. *
  2144. * Excel only stores references to external sheets that are used in formulas.
  2145. * For simplicity we store references to all the sheets in the workbook
  2146. * regardless of whether they are used or not. This reduces the overall
  2147. * complexity and eliminates the need for a two way dialogue between the formula
  2148. * parser the worksheet objects.
  2149. *
  2150. * @access private
  2151. * @param integer $count The number of external sheet references in this worksheet
  2152. */
  2153. function _storeExterncount($count)
  2154. {
  2155. $record = 0x0016; // Record identifier
  2156. $length = 0x0002; // Number of bytes to follow
  2157. $header = pack("vv", $record, $length);
  2158. $data = pack("v", $count);
  2159. $this->_prepend($header . $data);
  2160. }
  2161. /**
  2162. * Writes the Excel BIFF EXTERNSHEET record. These references are used by
  2163. * formulas. A formula references a sheet name via an index. Since we store a
  2164. * reference to all of the external worksheets the EXTERNSHEET index is the same
  2165. * as the worksheet index.
  2166. *
  2167. * @access private
  2168. * @param string $sheetname The name of a external worksheet
  2169. */
  2170. function _storeExternsheet($sheetname)
  2171. {
  2172. $record = 0x0017; // Record identifier
  2173. // References to the current sheet are encoded differently to references to
  2174. // external sheets.
  2175. //
  2176. if ($this->name == $sheetname) {
  2177. $sheetname = '';
  2178. $length = 0x02; // The following 2 bytes
  2179. $cch = 1; // The following byte
  2180. $rgch = 0x02; // Self reference
  2181. } else {
  2182. $length = 0x02 + strlen($sheetname);
  2183. $cch = strlen($sheetname);
  2184. $rgch = 0x03; // Reference to a sheet in the current workbook
  2185. }
  2186. $header = pack("vv", $record, $length);
  2187. $data = pack("CC", $cch, $rgch);
  2188. $this->_prepend($header . $data . $sheetname);
  2189. }
  2190. /**
  2191. * Writes the Excel BIFF PANE record.
  2192. * The panes can either be frozen or thawed (unfrozen).
  2193. * Frozen panes are specified in terms of an integer number of rows and columns.
  2194. * Thawed panes are specified in terms of Excel's units for rows and columns.
  2195. *
  2196. * @access private
  2197. * @param array $panes This is the only parameter received and is composed of the following:
  2198. * 0 => Vertical split position,
  2199. * 1 => Horizontal split position
  2200. * 2 => Top row visible
  2201. * 3 => Leftmost column visible
  2202. * 4 => Active pane
  2203. */
  2204. function _storePanes($panes)
  2205. {
  2206. $y = $panes[0];
  2207. $x = $panes[1];
  2208. $rwTop = $panes[2];
  2209. $colLeft = $panes[3];
  2210. if (count($panes) > 4) { // if Active pane was received
  2211. $pnnAct = $panes[4];
  2212. } else {
  2213. $pnnAct = null;
  2214. }
  2215. $record = 0x0041; // Record identifier
  2216. $length = 0x000A; // Number of bytes to follow
  2217. // Code specific to frozen or thawed panes.
  2218. if ($this->_frozen) {
  2219. // Set default values for $rwTop and $colLeft
  2220. if (!isset($rwTop)) {
  2221. $rwTop = $y;
  2222. }
  2223. if (!isset($colLeft)) {
  2224. $colLeft = $x;
  2225. }
  2226. } else {
  2227. // Set default values for $rwTop and $colLeft
  2228. if (!isset($rwTop)) {
  2229. $rwTop = 0;
  2230. }
  2231. if (!isset($colLeft)) {
  2232. $colLeft = 0;
  2233. }
  2234. // Convert Excel's row and column units to the internal units.
  2235. // The default row height is 12.75
  2236. // The default column width is 8.43
  2237. // The following slope and intersection values were interpolated.
  2238. //
  2239. $y = 20*$y + 255;
  2240. $x = 113.879*$x + 390;
  2241. }
  2242. // Determine which pane should be active. There is also the undocumented
  2243. // option to override this should it be necessary: may be removed later.
  2244. //
  2245. if (!isset($pnnAct)) {
  2246. if ($x != 0 && $y != 0) {
  2247. $pnnAct = 0; // Bottom right
  2248. }
  2249. if ($x != 0 && $y == 0) {
  2250. $pnnAct = 1; // Top right
  2251. }
  2252. if ($x == 0 && $y != 0) {
  2253. $pnnAct = 2; // Bottom left
  2254. }
  2255. if ($x == 0 && $y == 0) {
  2256. $pnnAct = 3; // Top left
  2257. }
  2258. }
  2259. $this->_active_pane = $pnnAct; // Used in _storeSelection
  2260. $header = pack("vv", $record, $length);
  2261. $data = pack("vvvvv", $x, $y, $rwTop, $colLeft, $pnnAct);
  2262. $this->_append($header . $data);
  2263. }
  2264. /**
  2265. * Store the page setup SETUP BIFF record.
  2266. *
  2267. * @access private
  2268. */
  2269. function _storeSetup()
  2270. {
  2271. $record = 0x00A1; // Record identifier
  2272. $length = 0x0022; // Number of bytes to follow
  2273. $iPaperSize = $this->_paper_size; // Paper size
  2274. $iScale = $this->_print_scale; // Print scaling factor
  2275. $iPageStart = 0x01; // Starting page number
  2276. $iFitWidth = $this->_fit_width; // Fit to number of pages wide
  2277. $iFitHeight = $this->_fit_height; // Fit to number of pages high
  2278. $grbit = 0x00; // Option flags
  2279. $iRes = 0x0258; // Print resolution
  2280. $iVRes = 0x0258; // Vertical print resolution
  2281. $numHdr = $this->_margin_head; // Header Margin
  2282. $numFtr = $this->_margin_foot; // Footer Margin
  2283. $iCopies = 0x01; // Number of copies
  2284. $fLeftToRight = 0x0; // Print over then down
  2285. $fLandscape = $this->_orientation; // Page orientation
  2286. $fNoPls = 0x0; // Setup not read from printer
  2287. $fNoColor = 0x0; // Print black and white
  2288. $fDraft = 0x0; // Print draft quality
  2289. $fNotes = 0x0; // Print notes
  2290. $fNoOrient = 0x0; // Orientation not set
  2291. $fUsePage = 0x0; // Use custom starting page
  2292. $grbit = $fLeftToRight;
  2293. $grbit |= $fLandscape << 1;
  2294. $grbit |= $fNoPls << 2;
  2295. $grbit |= $fNoColor << 3;
  2296. $grbit |= $fDraft << 4;
  2297. $grbit |= $fNotes << 5;
  2298. $grbit |= $fNoOrient << 6;
  2299. $grbit |= $fUsePage << 7;
  2300. $numHdr = pack("d", $numHdr);
  2301. $numFtr = pack("d", $numFtr);
  2302. if ($this->_byte_order) { // if it's Big Endian
  2303. $numHdr = strrev($numHdr);
  2304. $numFtr = strrev($numFtr);
  2305. }
  2306. $header = pack("vv", $record, $length);
  2307. $data1 = pack("vvvvvvvv", $iPaperSize,
  2308. $iScale,
  2309. $iPageStart,
  2310. $iFitWidth,
  2311. $iFitHeight,
  2312. $grbit,
  2313. $iRes,
  2314. $iVRes);
  2315. $data2 = $numHdr.$numFtr;
  2316. $data3 = pack("v", $iCopies);
  2317. $this->_prepend($header . $data1 . $data2 . $data3);
  2318. }
  2319. /**
  2320. * Store the header caption BIFF record.
  2321. *
  2322. * @access private
  2323. */
  2324. function _storeHeader()
  2325. {
  2326. $record = 0x0014; // Record identifier
  2327. $str = $this->_header; // header string
  2328. $cch = strlen($str); // Length of header string
  2329. if ($this->_BIFF_version == 0x0600) {
  2330. $encoding = 0x0; // TODO: Unicode support
  2331. $length = 3 + $cch; // Bytes to follow
  2332. } else {
  2333. $length = 1 + $cch; // Bytes to follow
  2334. }
  2335. $header = pack("vv", $record, $length);
  2336. if ($this->_BIFF_version == 0x0600) {
  2337. $data = pack("vC", $cch, $encoding);
  2338. } else {
  2339. $data = pack("C", $cch);
  2340. }
  2341. $this->_prepend($header.$data.$str);
  2342. }
  2343. /**
  2344. * Store the footer caption BIFF record.
  2345. *
  2346. * @access private
  2347. */
  2348. function _storeFooter()
  2349. {
  2350. $record = 0x0015; // Record identifier
  2351. $str = $this->_footer; // Footer string
  2352. $cch = strlen($str); // Length of footer string
  2353. if ($this->_BIFF_version == 0x0600) {
  2354. $encoding = 0x0; // TODO: Unicode support
  2355. $length = 3 + $cch; // Bytes to follow
  2356. } else {
  2357. $length = 1 + $cch;
  2358. }
  2359. $header = pack("vv", $record, $length);
  2360. if ($this->_BIFF_version == 0x0600) {
  2361. $data = pack("vC", $cch, $encoding);
  2362. } else {
  2363. $data = pack("C", $cch);
  2364. }
  2365. $this->_prepend($header . $data . $str);
  2366. }
  2367. /**
  2368. * Store the horizontal centering HCENTER BIFF record.
  2369. *
  2370. * @access private
  2371. */
  2372. function _storeHcenter()
  2373. {
  2374. $record = 0x0083; // Record identifier
  2375. $length = 0x0002; // Bytes to follow
  2376. $fHCenter = $this->_hcenter; // Horizontal centering
  2377. $header = pack("vv", $record, $length);
  2378. $data = pack("v", $fHCenter);
  2379. $this->_prepend($header.$data);
  2380. }
  2381. /**
  2382. * Store the vertical centering VCENTER BIFF record.
  2383. *
  2384. * @access private
  2385. */
  2386. function _storeVcenter()
  2387. {
  2388. $record = 0x0084; // Record identifier
  2389. $length = 0x0002; // Bytes to follow
  2390. $fVCenter = $this->_vcenter; // Horizontal centering
  2391. $header = pack("vv", $record, $length);
  2392. $data = pack("v", $fVCenter);
  2393. $this->_prepend($header . $data);
  2394. }
  2395. /**
  2396. * Store the LEFTMARGIN BIFF record.
  2397. *
  2398. * @access private
  2399. */
  2400. function _storeMarginLeft()
  2401. {
  2402. $record = 0x0026; // Record identifier
  2403. $length = 0x0008; // Bytes to follow
  2404. $margin = $this->_margin_left; // Margin in inches
  2405. $header = pack("vv", $record, $length);
  2406. $data = pack("d", $margin);
  2407. if ($this->_byte_order) { // if it's Big Endian
  2408. $data = strrev($data);
  2409. }
  2410. $this->_prepend($header . $data);
  2411. }
  2412. /**
  2413. * Store the RIGHTMARGIN BIFF record.
  2414. *
  2415. * @access private
  2416. */
  2417. function _storeMarginRight()
  2418. {
  2419. $record = 0x0027; // Record identifier
  2420. $length = 0x0008; // Bytes to follow
  2421. $margin = $this->_margin_right; // Margin in inches
  2422. $header = pack("vv", $record, $length);
  2423. $data = pack("d", $margin);
  2424. if ($this->_byte_order) { // if it's Big Endian
  2425. $data = strrev($data);
  2426. }
  2427. $this->_prepend($header . $data);
  2428. }
  2429. /**
  2430. * Store the TOPMARGIN BIFF record.
  2431. *
  2432. * @access private
  2433. */
  2434. function _storeMarginTop()
  2435. {
  2436. $record = 0x0028; // Record identifier
  2437. $length = 0x0008; // Bytes to follow
  2438. $margin = $this->_margin_top; // Margin in inches
  2439. $header = pack("vv", $record, $length);
  2440. $data = pack("d", $margin);
  2441. if ($this->_byte_order) { // if it's Big Endian
  2442. $data = strrev($data);
  2443. }
  2444. $this->_prepend($header . $data);
  2445. }
  2446. /**
  2447. * Store the BOTTOMMARGIN BIFF record.
  2448. *
  2449. * @access private
  2450. */
  2451. function _storeMarginBottom()
  2452. {
  2453. $record = 0x0029; // Record identifier
  2454. $length = 0x0008; // Bytes to follow
  2455. $margin = $this->_margin_bottom; // Margin in inches
  2456. $header = pack("vv", $record, $length);
  2457. $data = pack("d", $margin);
  2458. if ($this->_byte_order) { // if it's Big Endian
  2459. $data = strrev($data);
  2460. }
  2461. $this->_prepend($header . $data);
  2462. }
  2463. /**
  2464. * Merges the area given by its arguments.
  2465. * This is an Excel97/2000 method. It is required to perform more complicated
  2466. * merging than the normal setAlign('merge').
  2467. *
  2468. * @access public
  2469. * @param integer $first_row First row of the area to merge
  2470. * @param integer $first_col First column of the area to merge
  2471. * @param integer $last_row Last row of the area to merge
  2472. * @param integer $last_col Last column of the area to merge
  2473. */
  2474. function mergeCells($first_row, $first_col, $last_row, $last_col)
  2475. {
  2476. $record = 0x00E5; // Record identifier
  2477. $length = 0x000A; // Bytes to follow
  2478. $cref = 1; // Number of refs
  2479. // Swap last row/col for first row/col as necessary
  2480. if ($first_row > $last_row) {
  2481. list($first_row, $last_row) = array($last_row, $first_row);
  2482. }
  2483. if ($first_col > $last_col) {
  2484. list($first_col, $last_col) = array($last_col, $first_col);
  2485. }
  2486. $header = pack("vv", $record, $length);
  2487. $data = pack("vvvvv", $cref, $first_row, $last_row,
  2488. $first_col, $last_col);
  2489. $this->_append($header.$data);
  2490. }
  2491. /**
  2492. * Write the PRINTHEADERS BIFF record.
  2493. *
  2494. * @access private
  2495. */
  2496. function _storePrintHeaders()
  2497. {
  2498. $record = 0x002a; // Record identifier
  2499. $length = 0x0002; // Bytes to follow
  2500. $fPrintRwCol = $this->_print_headers; // Boolean flag
  2501. $header = pack("vv", $record, $length);
  2502. $data = pack("v", $fPrintRwCol);
  2503. $this->_prepend($header . $data);
  2504. }
  2505. /**
  2506. * Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
  2507. * GRIDSET record.
  2508. *
  2509. * @access private
  2510. */
  2511. function _storePrintGridlines()
  2512. {
  2513. $record = 0x002b; // Record identifier
  2514. $length = 0x0002; // Bytes to follow
  2515. $fPrintGrid = $this->_print_gridlines; // Boolean flag
  2516. $header = pack("vv", $record, $length);
  2517. $data = pack("v", $fPrintGrid);
  2518. $this->_prepend($header . $data);
  2519. }
  2520. /**
  2521. * Write the GRIDSET BIFF record. Must be used in conjunction with the
  2522. * PRINTGRIDLINES record.
  2523. *
  2524. * @access private
  2525. */
  2526. function _storeGridset()
  2527. {
  2528. $record = 0x0082; // Record identifier
  2529. $length = 0x0002; // Bytes to follow
  2530. $fGridSet = !($this->_print_gridlines); // Boolean flag
  2531. $header = pack("vv", $record, $length);
  2532. $data = pack("v", $fGridSet);
  2533. $this->_prepend($header . $data);
  2534. }
  2535. /**
  2536. * Write the GUTS BIFF record. This is used to configure the gutter margins
  2537. * where Excel outline symbols are displayed. The visibility of the gutters is
  2538. * controlled by a flag in WSBOOL.
  2539. *
  2540. * @see _storeWsbool()
  2541. * @access private
  2542. */
  2543. function _storeGuts()
  2544. {
  2545. $record = 0x0080; // Record identifier
  2546. $length = 0x0008; // Bytes to follow
  2547. $dxRwGut = 0x0000; // Size of row gutter
  2548. $dxColGut = 0x0000; // Size of col gutter
  2549. $row_level = $this->_outline_row_level;
  2550. $col_level = 0;
  2551. // Calculate the maximum column outline level. The equivalent calculation
  2552. // for the row outline level is carried out in setRow().
  2553. $colcount = count($this->_colinfo);
  2554. for ($i = 0; $i < $colcount; $i++) {
  2555. // Skip cols without outline level info.
  2556. if (count($this->_colinfo[$i]) >= 6) {
  2557. $col_level = max($this->_colinfo[$i][5], $col_level);
  2558. }
  2559. }
  2560. // Set the limits for the outline levels (0 <= x <= 7).
  2561. $col_level = max(0, min($col_level, 7));
  2562. // The displayed level is one greater than the max outline levels
  2563. if ($row_level) {
  2564. $row_level++;
  2565. }
  2566. if ($col_level) {
  2567. $col_level++;
  2568. }
  2569. $header = pack("vv", $record, $length);
  2570. $data = pack("vvvv", $dxRwGut, $dxColGut, $row_level, $col_level);
  2571. $this->_prepend($header.$data);
  2572. }
  2573. /**
  2574. * Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
  2575. * with the SETUP record.
  2576. *
  2577. * @access private
  2578. */
  2579. function _storeWsbool()
  2580. {
  2581. $record = 0x0081; // Record identifier
  2582. $length = 0x0002; // Bytes to follow
  2583. $grbit = 0x0000;
  2584. // The only option that is of interest is the flag for fit to page. So we
  2585. // set all the options in one go.
  2586. //
  2587. /*if ($this->_fit_page) {
  2588. $grbit = 0x05c1;
  2589. } else {
  2590. $grbit = 0x04c1;
  2591. }*/
  2592. // Set the option flags
  2593. $grbit |= 0x0001; // Auto page breaks visible
  2594. if ($this->_outline_style) {
  2595. $grbit |= 0x0020; // Auto outline styles
  2596. }
  2597. if ($this->_outline_below) {
  2598. $grbit |= 0x0040; // Outline summary below
  2599. }
  2600. if ($this->_outline_right) {
  2601. $grbit |= 0x0080; // Outline summary right
  2602. }
  2603. if ($this->_fit_page) {
  2604. $grbit |= 0x0100; // Page setup fit to page
  2605. }
  2606. if ($this->_outline_on) {
  2607. $grbit |= 0x0400; // Outline symbols displayed
  2608. }
  2609. $header = pack("vv", $record, $length);
  2610. $data = pack("v", $grbit);
  2611. $this->_prepend($header . $data);
  2612. }
  2613. /**
  2614. * Write the HORIZONTALPAGEBREAKS BIFF record.
  2615. *
  2616. * @access private
  2617. */
  2618. function _storeHbreak()
  2619. {
  2620. // Return if the user hasn't specified pagebreaks
  2621. if (empty($this->_hbreaks)) {
  2622. return;
  2623. }
  2624. // Sort and filter array of page breaks
  2625. $breaks = $this->_hbreaks;
  2626. sort($breaks, SORT_NUMERIC);
  2627. if ($breaks[0] == 0) { // don't use first break if it's 0
  2628. array_shift($breaks);
  2629. }
  2630. $record = 0x001b; // Record identifier
  2631. $cbrk = count($breaks); // Number of page breaks
  2632. if ($this->_BIFF_version == 0x0600) {
  2633. $length = 2 + 6*$cbrk; // Bytes to follow
  2634. } else {
  2635. $length = 2 + 2*$cbrk; // Bytes to follow
  2636. }
  2637. $header = pack("vv", $record, $length);
  2638. $data = pack("v", $cbrk);
  2639. // Append each page break
  2640. foreach ($breaks as $break) {
  2641. if ($this->_BIFF_version == 0x0600) {
  2642. $data .= pack("vvv", $break, 0x0000, 0x00ff);
  2643. } else {
  2644. $data .= pack("v", $break);
  2645. }
  2646. }
  2647. $this->_prepend($header.$data);
  2648. }
  2649. /**
  2650. * Write the VERTICALPAGEBREAKS BIFF record.
  2651. *
  2652. * @access private
  2653. */
  2654. function _storeVbreak()
  2655. {
  2656. // Return if the user hasn't specified pagebreaks
  2657. if (empty($this->_vbreaks)) {
  2658. return;
  2659. }
  2660. // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
  2661. // It is slightly higher in Excel 97/200, approx. 1026
  2662. $breaks = array_slice($this->_vbreaks,0,1000);
  2663. // Sort and filter array of page breaks
  2664. sort($breaks, SORT_NUMERIC);
  2665. if ($breaks[0] == 0) { // don't use first break if it's 0
  2666. array_shift($breaks);
  2667. }
  2668. $record = 0x001a; // Record identifier
  2669. $cbrk = count($breaks); // Number of page breaks
  2670. if ($this->_BIFF_version == 0x0600) {
  2671. $length = 2 + 6*$cbrk; // Bytes to follow
  2672. } else {
  2673. $length = 2 + 2*$cbrk; // Bytes to follow
  2674. }
  2675. $header = pack("vv", $record, $length);
  2676. $data = pack("v", $cbrk);
  2677. // Append each page break
  2678. foreach ($breaks as $break) {
  2679. if ($this->_BIFF_version == 0x0600) {
  2680. $data .= pack("vvv", $break, 0x0000, 0xffff);
  2681. } else {
  2682. $data .= pack("v", $break);
  2683. }
  2684. }
  2685. $this->_prepend($header . $data);
  2686. }
  2687. /**
  2688. * Set the Biff PROTECT record to indicate that the worksheet is protected.
  2689. *
  2690. * @access private
  2691. */
  2692. function _storeProtect()
  2693. {
  2694. // Exit unless sheet protection has been specified
  2695. if ($this->_protect == 0) {
  2696. return;
  2697. }
  2698. $record = 0x0012; // Record identifier
  2699. $length = 0x0002; // Bytes to follow
  2700. $fLock = $this->_protect; // Worksheet is protected
  2701. $header = pack("vv", $record, $length);
  2702. $data = pack("v", $fLock);
  2703. $this->_prepend($header.$data);
  2704. }
  2705. /**
  2706. * Write the worksheet PASSWORD record.
  2707. *
  2708. * @access private
  2709. */
  2710. function _storePassword()
  2711. {
  2712. // Exit unless sheet protection and password have been specified
  2713. if (($this->_protect == 0) || (!isset($this->_password))) {
  2714. return;
  2715. }
  2716. $record = 0x0013; // Record identifier
  2717. $length = 0x0002; // Bytes to follow
  2718. $wPassword = $this->_password; // Encoded password
  2719. $header = pack("vv", $record, $length);
  2720. $data = pack("v", $wPassword);
  2721. $this->_prepend($header . $data);
  2722. }
  2723. /**
  2724. * Insert a 24bit bitmap image in a worksheet.
  2725. *
  2726. * @access public
  2727. * @param integer $row The row we are going to insert the bitmap into
  2728. * @param integer $col The column we are going to insert the bitmap into
  2729. * @param string $bitmap The bitmap filename
  2730. * @param integer $x The horizontal position (offset) of the image inside the cell.
  2731. * @param integer $y The vertical position (offset) of the image inside the cell.
  2732. * @param integer $scale_x The horizontal scale
  2733. * @param integer $scale_y The vertical scale
  2734. */
  2735. function insertBitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1)
  2736. {
  2737. $bitmap_array = $this->_processBitmap($bitmap);
  2738. if ($this->isError($bitmap_array)) {
  2739. $this->writeString($row, $col, $bitmap_array->getMessage());
  2740. return;
  2741. }
  2742. list($width, $height, $size, $data) = $bitmap_array; //$this->_processBitmap($bitmap);
  2743. // Scale the frame of the image.
  2744. $width *= $scale_x;
  2745. $height *= $scale_y;
  2746. // Calculate the vertices of the image and write the OBJ record
  2747. $this->_positionImage($col, $row, $x, $y, $width, $height);
  2748. // Write the IMDATA record to store the bitmap data
  2749. $record = 0x007f;
  2750. $length = 8 + $size;
  2751. $cf = 0x09;
  2752. $env = 0x01;
  2753. $lcb = $size;
  2754. $header = pack("vvvvV", $record, $length, $cf, $env, $lcb);
  2755. $this->_append($header.$data);
  2756. }
  2757. /**
  2758. * Calculate the vertices that define the position of the image as required by
  2759. * the OBJ record.
  2760. *
  2761. * +------------+------------+
  2762. * | A | B |
  2763. * +-----+------------+------------+
  2764. * | |(x1,y1) | |
  2765. * | 1 |(A1)._______|______ |
  2766. * | | | | |
  2767. * | | | | |
  2768. * +-----+----| BITMAP |-----+
  2769. * | | | | |
  2770. * | 2 | |______________. |
  2771. * | | | (B2)|
  2772. * | | | (x2,y2)|
  2773. * +---- +------------+------------+
  2774. *
  2775. * Example of a bitmap that covers some of the area from cell A1 to cell B2.
  2776. *
  2777. * Based on the width and height of the bitmap we need to calculate 8 vars:
  2778. * $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
  2779. * The width and height of the cells are also variable and have to be taken into
  2780. * account.
  2781. * The values of $col_start and $row_start are passed in from the calling
  2782. * function. The values of $col_end and $row_end are calculated by subtracting
  2783. * the width and height of the bitmap from the width and height of the
  2784. * underlying cells.
  2785. * The vertices are expressed as a percentage of the underlying cell width as
  2786. * follows (rhs values are in pixels):
  2787. *
  2788. * x1 = X / W *1024
  2789. * y1 = Y / H *256
  2790. * x2 = (X-1) / W *1024
  2791. * y2 = (Y-1) / H *256
  2792. *
  2793. * Where: X is distance from the left side of the underlying cell
  2794. * Y is distance from the top of the underlying cell
  2795. * W is the width of the cell
  2796. * H is the height of the cell
  2797. *
  2798. * @access private
  2799. * @note the SDK incorrectly states that the height should be expressed as a
  2800. * percentage of 1024.
  2801. * @param integer $col_start Col containing upper left corner of object
  2802. * @param integer $row_start Row containing top left corner of object
  2803. * @param integer $x1 Distance to left side of object
  2804. * @param integer $y1 Distance to top of object
  2805. * @param integer $width Width of image frame
  2806. * @param integer $height Height of image frame
  2807. */
  2808. function _positionImage($col_start, $row_start, $x1, $y1, $width, $height)
  2809. {
  2810. // Initialise end cell to the same as the start cell
  2811. $col_end = $col_start; // Col containing lower right corner of object
  2812. $row_end = $row_start; // Row containing bottom right corner of object
  2813. // Zero the specified offset if greater than the cell dimensions
  2814. if ($x1 >= $this->_sizeCol($col_start)) {
  2815. $x1 = 0;
  2816. }
  2817. if ($y1 >= $this->_sizeRow($row_start)) {
  2818. $y1 = 0;
  2819. }
  2820. $width = $width + $x1 -1;
  2821. $height = $height + $y1 -1;
  2822. // Subtract the underlying cell widths to find the end cell of the image
  2823. while ($width >= $this->_sizeCol($col_end)) {
  2824. $width -= $this->_sizeCol($col_end);
  2825. $col_end++;
  2826. }
  2827. // Subtract the underlying cell heights to find the end cell of the image
  2828. while ($height >= $this->_sizeRow($row_end)) {
  2829. $height -= $this->_sizeRow($row_end);
  2830. $row_end++;
  2831. }
  2832. // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
  2833. // with zero eight or width.
  2834. //
  2835. if ($this->_sizeCol($col_start) == 0) {
  2836. return;
  2837. }
  2838. if ($this->_sizeCol($col_end) == 0) {
  2839. return;
  2840. }
  2841. if ($this->_sizeRow($row_start) == 0) {
  2842. return;
  2843. }
  2844. if ($this->_sizeRow($row_end) == 0) {
  2845. return;
  2846. }
  2847. // Convert the pixel values to the percentage value expected by Excel
  2848. $x1 = $x1 / $this->_sizeCol($col_start) * 1024;
  2849. $y1 = $y1 / $this->_sizeRow($row_start) * 256;
  2850. $x2 = $width / $this->_sizeCol($col_end) * 1024; // Distance to right side of object
  2851. $y2 = $height / $this->_sizeRow($row_end) * 256; // Distance to bottom of object
  2852. $this->_storeObjPicture($col_start, $x1,
  2853. $row_start, $y1,
  2854. $col_end, $x2,
  2855. $row_end, $y2);
  2856. }
  2857. /**
  2858. * Convert the width of a cell from user's units to pixels. By interpolation
  2859. * the relationship is: y = 7x +5. If the width hasn't been set by the user we
  2860. * use the default value. If the col is hidden we use a value of zero.
  2861. *
  2862. * @access private
  2863. * @param integer $col The column
  2864. * @return integer The width in pixels
  2865. */
  2866. function _sizeCol($col)
  2867. {
  2868. // Look up the cell value to see if it has been changed
  2869. if (isset($this->col_sizes[$col])) {
  2870. if ($this->col_sizes[$col] == 0) {
  2871. return(0);
  2872. } else {
  2873. return(floor(7 * $this->col_sizes[$col] + 5));
  2874. }
  2875. } else {
  2876. return(64);
  2877. }
  2878. }
  2879. /**
  2880. * Convert the height of a cell from user's units to pixels. By interpolation
  2881. * the relationship is: y = 4/3x. If the height hasn't been set by the user we
  2882. * use the default value. If the row is hidden we use a value of zero. (Not
  2883. * possible to hide row yet).
  2884. *
  2885. * @access private
  2886. * @param integer $row The row
  2887. * @return integer The width in pixels
  2888. */
  2889. function _sizeRow($row)
  2890. {
  2891. // Look up the cell value to see if it has been changed
  2892. if (isset($this->_row_sizes[$row])) {
  2893. if ($this->_row_sizes[$row] == 0) {
  2894. return(0);
  2895. } else {
  2896. return(floor(4/3 * $this->_row_sizes[$row]));
  2897. }
  2898. } else {
  2899. return(17);
  2900. }
  2901. }
  2902. /**
  2903. * Store the OBJ record that precedes an IMDATA record. This could be generalise
  2904. * to support other Excel objects.
  2905. *
  2906. * @access private
  2907. * @param integer $colL Column containing upper left corner of object
  2908. * @param integer $dxL Distance from left side of cell
  2909. * @param integer $rwT Row containing top left corner of object
  2910. * @param integer $dyT Distance from top of cell
  2911. * @param integer $colR Column containing lower right corner of object
  2912. * @param integer $dxR Distance from right of cell
  2913. * @param integer $rwB Row containing bottom right corner of object
  2914. * @param integer $dyB Distance from bottom of cell
  2915. */
  2916. function _storeObjPicture($colL,$dxL,$rwT,$dyT,$colR,$dxR,$rwB,$dyB)
  2917. {
  2918. $record = 0x005d; // Record identifier
  2919. $length = 0x003c; // Bytes to follow
  2920. $cObj = 0x0001; // Count of objects in file (set to 1)
  2921. $OT = 0x0008; // Object type. 8 = Picture
  2922. $id = 0x0001; // Object ID
  2923. $grbit = 0x0614; // Option flags
  2924. $cbMacro = 0x0000; // Length of FMLA structure
  2925. $Reserved1 = 0x0000; // Reserved
  2926. $Reserved2 = 0x0000; // Reserved
  2927. $icvBack = 0x09; // Background colour
  2928. $icvFore = 0x09; // Foreground colour
  2929. $fls = 0x00; // Fill pattern
  2930. $fAuto = 0x00; // Automatic fill
  2931. $icv = 0x08; // Line colour
  2932. $lns = 0xff; // Line style
  2933. $lnw = 0x01; // Line weight
  2934. $fAutoB = 0x00; // Automatic border
  2935. $frs = 0x0000; // Frame style
  2936. $cf = 0x0009; // Image format, 9 = bitmap
  2937. $Reserved3 = 0x0000; // Reserved
  2938. $cbPictFmla = 0x0000; // Length of FMLA structure
  2939. $Reserved4 = 0x0000; // Reserved
  2940. $grbit2 = 0x0001; // Option flags
  2941. $Reserved5 = 0x0000; // Reserved
  2942. $header = pack("vv", $record, $length);
  2943. $data = pack("V", $cObj);
  2944. $data .= pack("v", $OT);
  2945. $data .= pack("v", $id);
  2946. $data .= pack("v", $grbit);
  2947. $data .= pack("v", $colL);
  2948. $data .= pack("v", $dxL);
  2949. $data .= pack("v", $rwT);
  2950. $data .= pack("v", $dyT);
  2951. $data .= pack("v", $colR);
  2952. $data .= pack("v", $dxR);
  2953. $data .= pack("v", $rwB);
  2954. $data .= pack("v", $dyB);
  2955. $data .= pack("v", $cbMacro);
  2956. $data .= pack("V", $Reserved1);
  2957. $data .= pack("v", $Reserved2);
  2958. $data .= pack("C", $icvBack);
  2959. $data .= pack("C", $icvFore);
  2960. $data .= pack("C", $fls);
  2961. $data .= pack("C", $fAuto);
  2962. $data .= pack("C", $icv);
  2963. $data .= pack("C", $lns);
  2964. $data .= pack("C", $lnw);
  2965. $data .= pack("C", $fAutoB);
  2966. $data .= pack("v", $frs);
  2967. $data .= pack("V", $cf);
  2968. $data .= pack("v", $Reserved3);
  2969. $data .= pack("v", $cbPictFmla);
  2970. $data .= pack("v", $Reserved4);
  2971. $data .= pack("v", $grbit2);
  2972. $data .= pack("V", $Reserved5);
  2973. $this->_append($header . $data);
  2974. }
  2975. /**
  2976. * Convert a 24 bit bitmap into the modified internal format used by Windows.
  2977. * This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
  2978. * MSDN library.
  2979. *
  2980. * @access private
  2981. * @param string $bitmap The bitmap to process
  2982. * @return array Array with data and properties of the bitmap
  2983. */
  2984. function _processBitmap($bitmap)
  2985. {
  2986. // Open file.
  2987. $bmp_fd = @fopen($bitmap,"rb");
  2988. if (!$bmp_fd) {
  2989. $this->raiseError("Couldn't import $bitmap");
  2990. }
  2991. // Slurp the file into a string.
  2992. $data = fread($bmp_fd, filesize($bitmap));
  2993. // Check that the file is big enough to be a bitmap.
  2994. if (strlen($data) <= 0x36) {
  2995. $this->raiseError("$bitmap doesn't contain enough data.\n");
  2996. }
  2997. // The first 2 bytes are used to identify the bitmap.
  2998. $identity = unpack("A2ident", $data);
  2999. if ($identity['ident'] != "BM") {
  3000. $this->raiseError("$bitmap doesn't appear to be a valid bitmap image.\n");
  3001. }
  3002. // Remove bitmap data: ID.
  3003. $data = substr($data, 2);
  3004. // Read and remove the bitmap size. This is more reliable than reading
  3005. // the data size at offset 0x22.
  3006. //
  3007. $size_array = unpack("Vsa", substr($data, 0, 4));
  3008. $size = $size_array['sa'];
  3009. $data = substr($data, 4);
  3010. $size -= 0x36; // Subtract size of bitmap header.
  3011. $size += 0x0C; // Add size of BIFF header.
  3012. // Remove bitmap data: reserved, offset, header length.
  3013. $data = substr($data, 12);
  3014. // Read and remove the bitmap width and height. Verify the sizes.
  3015. $width_and_height = unpack("V2", substr($data, 0, 8));
  3016. $width = $width_and_height[1];
  3017. $height = $width_and_height[2];
  3018. $data = substr($data, 8);
  3019. if ($width > 0xFFFF) {
  3020. $this->raiseError("$bitmap: largest image width supported is 65k.\n");
  3021. }
  3022. if ($height > 0xFFFF) {
  3023. $this->raiseError("$bitmap: largest image height supported is 65k.\n");
  3024. }
  3025. // Read and remove the bitmap planes and bpp data. Verify them.
  3026. $planes_and_bitcount = unpack("v2", substr($data, 0, 4));
  3027. $data = substr($data, 4);
  3028. if ($planes_and_bitcount[2] != 24) { // Bitcount
  3029. $this->raiseError("$bitmap isn't a 24bit true color bitmap.\n");
  3030. }
  3031. if ($planes_and_bitcount[1] != 1) {
  3032. $this->raiseError("$bitmap: only 1 plane supported in bitmap image.\n");
  3033. }
  3034. // Read and remove the bitmap compression. Verify compression.
  3035. $compression = unpack("Vcomp", substr($data, 0, 4));
  3036. $data = substr($data, 4);
  3037. //$compression = 0;
  3038. if ($compression['comp'] != 0) {
  3039. $this->raiseError("$bitmap: compression not supported in bitmap image.\n");
  3040. }
  3041. // Remove bitmap data: data size, hres, vres, colours, imp. colours.
  3042. $data = substr($data, 20);
  3043. // Add the BITMAPCOREHEADER data
  3044. $header = pack("Vvvvv", 0x000c, $width, $height, 0x01, 0x18);
  3045. $data = $header . $data;
  3046. return (array($width, $height, $size, $data));
  3047. }
  3048. /**
  3049. * Store the window zoom factor. This should be a reduced fraction but for
  3050. * simplicity we will store all fractions with a numerator of 100.
  3051. *
  3052. * @access private
  3053. */
  3054. function _storeZoom()
  3055. {
  3056. // If scale is 100 we don't need to write a record
  3057. if ($this->_zoom == 100) {
  3058. return;
  3059. }
  3060. $record = 0x00A0; // Record identifier
  3061. $length = 0x0004; // Bytes to follow
  3062. $header = pack("vv", $record, $length);
  3063. $data = pack("vv", $this->_zoom, 100);
  3064. $this->_append($header . $data);
  3065. }
  3066. /**
  3067. * FIXME: add comments
  3068. */
  3069. function setValidation($row1, $col1, $row2, $col2, &$validator)
  3070. {
  3071. $this->_dv[] = $validator->_getData() .
  3072. pack("vvvvv", 1, $row1, $row2, $col1, $col2);
  3073. }
  3074. /**
  3075. * Store the DVAL and DV records.
  3076. *
  3077. * @access private
  3078. */
  3079. function _storeDataValidity()
  3080. {
  3081. $record = 0x01b2; // Record identifier
  3082. $length = 0x0012; // Bytes to follow
  3083. $grbit = 0x0002; // Prompt box at cell, no cached validity data at DV records
  3084. $horPos = 0x00000000; // Horizontal position of prompt box, if fixed position
  3085. $verPos = 0x00000000; // Vertical position of prompt box, if fixed position
  3086. $objId = 0xffffffff; // Object identifier of drop down arrow object, or -1 if not visible
  3087. $header = pack('vv', $record, $length);
  3088. $data = pack('vVVVV', $grbit, $horPos, $verPos, $objId,
  3089. count($this->_dv));
  3090. $this->_append($header.$data);
  3091. $record = 0x01be; // Record identifier
  3092. foreach ($this->_dv as $dv) {
  3093. $length = strlen($dv); // Bytes to follow
  3094. $header = pack("vv", $record, $length);
  3095. $this->_append($header . $dv);
  3096. }
  3097. }
  3098. }
  3099. ?>