PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/admin/pear/Spreadsheet/Excel/Writer/Worksheet.php

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