PageRenderTime 74ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/functions/PEAR/Spreadsheet/Excel/Writer/Worksheet.php

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