PageRenderTime 81ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_chronocontact/excelwriter/Writer/Worksheet.php

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