PageRenderTime 84ms CodeModel.GetById 25ms RepoModel.GetById 4ms app.codeStats 0ms

/branches/v1.6.3/Classes/PHPExcel/Writer/Excel5/Worksheet.php

#
PHP | 3573 lines | 1775 code | 433 blank | 1365 comment | 277 complexity | 50bcb82b48de4f778ad78b454d0de8f9 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full 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. * PHPExcel_Writer_Excel5_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 'PHPExcel/Writer/Excel5/Parser.php';
  35. require_once 'PHPExcel/Writer/Excel5/BIFFwriter.php';
  36. /**
  37. * Class for generating Excel Spreadsheets
  38. *
  39. * @author Xavier Noguer <xnoguer@rezebra.com>
  40. * @category FileFormats
  41. * @package PHPExcel_Writer_Excel5_Writer
  42. */
  43. class PHPExcel_Writer_Excel5_Worksheet extends PHPExcel_Writer_Excel5_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. * The temporary dir for storing files
  306. * @var string
  307. */
  308. var $_tmp_dir;
  309. /**
  310. * List of temporary files created
  311. * @var array
  312. */
  313. var $_tempFilesCreated = array();
  314. /**
  315. * Constructor
  316. *
  317. * @param string $name The name of the new worksheet
  318. * @param integer $index The index of the new worksheet
  319. * @param mixed &$activesheet The current activesheet of the workbook we belong to
  320. * @param mixed &$firstsheet The first worksheet in the workbook we belong to
  321. * @param mixed &$url_format The default format for hyperlinks
  322. * @param mixed &$parser The formula parser created for the Workbook
  323. * @param string $tempDir The temporary directory to be used
  324. * @access private
  325. */
  326. function PHPExcel_Writer_Excel5_Worksheet($BIFF_version, $name,
  327. $index, &$activesheet,
  328. &$firstsheet, &$str_total,
  329. &$str_unique, &$str_table,
  330. &$url_format, &$parser, $tempDir = '')
  331. {
  332. // It needs to call its parent's constructor explicitly
  333. $this->PHPExcel_Writer_Excel5_BIFFwriter();
  334. $this->_BIFF_version = $BIFF_version;
  335. $rowmax = 65536; // 16384 in Excel 5
  336. $colmax = 256;
  337. $this->name = $name;
  338. $this->index = $index;
  339. $this->activesheet = &$activesheet;
  340. $this->firstsheet = &$firstsheet;
  341. $this->_str_total = &$str_total;
  342. $this->_str_unique = &$str_unique;
  343. $this->_str_table = &$str_table;
  344. $this->_url_format = &$url_format;
  345. $this->_parser = &$parser;
  346. //$this->ext_sheets = array();
  347. $this->_filehandle = '';
  348. $this->_using_tmpfile = true;
  349. //$this->fileclosed = 0;
  350. //$this->offset = 0;
  351. $this->_xls_rowmax = $rowmax;
  352. $this->_xls_colmax = $colmax;
  353. $this->_xls_strmax = 255;
  354. $this->_dim_rowmin = $rowmax + 1;
  355. $this->_dim_rowmax = 0;
  356. $this->_dim_colmin = $colmax + 1;
  357. $this->_dim_colmax = 0;
  358. $this->_colinfo = array();
  359. $this->_selection = array(0,0,0,0);
  360. $this->_panes = array();
  361. $this->_active_pane = 3;
  362. $this->_frozen = 0;
  363. $this->selected = 0;
  364. $this->_paper_size = 0x0;
  365. $this->_orientation = 0x1;
  366. $this->_header = '';
  367. $this->_footer = '';
  368. $this->_hcenter = 0;
  369. $this->_vcenter = 0;
  370. $this->_margin_head = 0.50;
  371. $this->_margin_foot = 0.50;
  372. $this->_margin_left = 0.75;
  373. $this->_margin_right = 0.75;
  374. $this->_margin_top = 1.00;
  375. $this->_margin_bottom = 1.00;
  376. $this->title_rowmin = null;
  377. $this->title_rowmax = null;
  378. $this->title_colmin = null;
  379. $this->title_colmax = null;
  380. $this->print_rowmin = null;
  381. $this->print_rowmax = null;
  382. $this->print_colmin = null;
  383. $this->print_colmax = null;
  384. $this->_print_gridlines = 1;
  385. $this->_screen_gridlines = 1;
  386. $this->_print_headers = 0;
  387. $this->_fit_page = 0;
  388. $this->_fit_width = 0;
  389. $this->_fit_height = 0;
  390. $this->_hbreaks = array();
  391. $this->_vbreaks = array();
  392. $this->_protect = 0;
  393. $this->_password = null;
  394. $this->col_sizes = array();
  395. $this->_row_sizes = array();
  396. $this->_zoom = 100;
  397. $this->_print_scale = 100;
  398. $this->_outline_row_level = 0;
  399. $this->_outline_style = 0;
  400. $this->_outline_below = 1;
  401. $this->_outline_right = 1;
  402. $this->_outline_on = 1;
  403. $this->_merged_ranges = array();
  404. $this->_input_encoding = '';
  405. $this->_dv = array();
  406. $this->_tmp_dir = $tempDir;
  407. $this->_initialize();
  408. }
  409. /**
  410. * Cleanup
  411. */
  412. public function cleanup() {
  413. @fclose($this->_filehandle);
  414. foreach ($this->_tempFilesCreated as $file) {
  415. @unlink($file);
  416. }
  417. }
  418. /**
  419. * Open a tmp file to store the majority of the Worksheet data. If this fails,
  420. * for example due to write permissions, store the data in memory. This can be
  421. * slow for large files.
  422. *
  423. * @access private
  424. */
  425. function _initialize()
  426. {
  427. // Open tmp file for storing Worksheet data
  428. $fileName = tempnam($this->_tmp_dir, 'XLSHEET');
  429. $fh = fopen($fileName, 'w+');
  430. if ($fh) {
  431. // Store filehandle
  432. $this->_filehandle = $fh;
  433. $this->_tempFilesCreated[] = $fileName;
  434. } else {
  435. // If tmpfile() fails store data in memory
  436. $this->_using_tmpfile = false;
  437. }
  438. }
  439. /**
  440. * Sets the temp dir used for storing files
  441. *
  442. * @access public
  443. * @param string $dir The dir to be used as temp dir
  444. * @return true if given dir is valid, false otherwise
  445. */
  446. function setTempDir($dir)
  447. {
  448. if (is_dir($dir)) {
  449. $this->_tmp_dir = $dir;
  450. return true;
  451. }
  452. return false;
  453. }
  454. /**
  455. * Add data to the beginning of the workbook (note the reverse order)
  456. * and to the end of the workbook.
  457. *
  458. * @access public
  459. * @see PHPExcel_Writer_Excel5_Workbook::storeWorkbook()
  460. * @param array $sheetnames The array of sheetnames from the Workbook this
  461. * worksheet belongs to
  462. */
  463. function close($sheetnames)
  464. {
  465. $num_sheets = count($sheetnames);
  466. /***********************************************
  467. * Prepend in reverse order!!
  468. */
  469. // Prepend the sheet dimensions
  470. $this->_storeDimensions();
  471. // Prepend the sheet password
  472. $this->_storePassword();
  473. // Prepend the sheet protection
  474. $this->_storeProtect();
  475. // Prepend the page setup
  476. $this->_storeSetup();
  477. /* FIXME: margins are actually appended */
  478. // Prepend the bottom margin
  479. $this->_storeMarginBottom();
  480. // Prepend the top margin
  481. $this->_storeMarginTop();
  482. // Prepend the right margin
  483. $this->_storeMarginRight();
  484. // Prepend the left margin
  485. $this->_storeMarginLeft();
  486. // Prepend the page vertical centering
  487. $this->_storeVcenter();
  488. // Prepend the page horizontal centering
  489. $this->_storeHcenter();
  490. // Prepend the page footer
  491. $this->_storeFooter();
  492. // Prepend the page header
  493. $this->_storeHeader();
  494. // Prepend the vertical page breaks
  495. $this->_storeVbreak();
  496. // Prepend the horizontal page breaks
  497. $this->_storeHbreak();
  498. // Prepend WSBOOL
  499. $this->_storeWsbool();
  500. // Prepend GRIDSET
  501. $this->_storeGridset();
  502. // Prepend GUTS
  503. if ($this->_BIFF_version == 0x0500) {
  504. $this->_storeGuts();
  505. }
  506. // Prepend PRINTGRIDLINES
  507. $this->_storePrintGridlines();
  508. // Prepend PRINTHEADERS
  509. $this->_storePrintHeaders();
  510. // Prepend EXTERNSHEET references
  511. if ($this->_BIFF_version == 0x0500) {
  512. for ($i = $num_sheets; $i > 0; $i--) {
  513. $sheetname = $sheetnames[$i-1];
  514. $this->_storeExternsheet($sheetname);
  515. }
  516. }
  517. // Prepend the EXTERNCOUNT of external references.
  518. if ($this->_BIFF_version == 0x0500) {
  519. $this->_storeExterncount($num_sheets);
  520. }
  521. // Prepend the COLINFO records if they exist
  522. if (!empty($this->_colinfo)) {
  523. $colcount = count($this->_colinfo);
  524. for ($i = 0; $i < $colcount; $i++) {
  525. $this->_storeColinfo($this->_colinfo[$i]);
  526. }
  527. $this->_storeDefcol();
  528. }
  529. // Prepend the BOF record
  530. $this->_storeBof(0x0010);
  531. /*
  532. * End of prepend. Read upwards from here.
  533. ***********************************************/
  534. // Append
  535. $this->_storeWindow2();
  536. $this->_storeZoom();
  537. if (!empty($this->_panes)) {
  538. $this->_storePanes($this->_panes);
  539. }
  540. $this->_storeSelection($this->_selection);
  541. $this->_storeMergedCells();
  542. /* TODO: add data validity */
  543. /*if ($this->_BIFF_version == 0x0600) {
  544. $this->_storeDataValidity();
  545. }*/
  546. $this->_storeEof();
  547. }
  548. /**
  549. * Retrieve the worksheet name.
  550. * This is usefull when creating worksheets without a name.
  551. *
  552. * @access public
  553. * @return string The worksheet's name
  554. */
  555. function getName()
  556. {
  557. return $this->name;
  558. }
  559. /**
  560. * Retrieves data from memory in one chunk, or from disk in $buffer
  561. * sized chunks.
  562. *
  563. * @return string The data
  564. */
  565. function getData()
  566. {
  567. $buffer = 4096;
  568. // Return data stored in memory
  569. if (isset($this->_data)) {
  570. $tmp = $this->_data;
  571. unset($this->_data);
  572. $fh = $this->_filehandle;
  573. if ($this->_using_tmpfile) {
  574. fseek($fh, 0);
  575. }
  576. return $tmp;
  577. }
  578. // Return data stored on disk
  579. if ($this->_using_tmpfile) {
  580. if ($tmp = fread($this->_filehandle, $buffer)) {
  581. return $tmp;
  582. }
  583. }
  584. // No data to return
  585. return '';
  586. }
  587. /**
  588. * Sets a merged cell range
  589. *
  590. * @access public
  591. * @param integer $first_row First row of the area to merge
  592. * @param integer $first_col First column of the area to merge
  593. * @param integer $last_row Last row of the area to merge
  594. * @param integer $last_col Last column of the area to merge
  595. */
  596. function setMerge($first_row, $first_col, $last_row, $last_col)
  597. {
  598. if (($last_row < $first_row) || ($last_col < $first_col)) {
  599. return;
  600. }
  601. // don't check rowmin, rowmax, etc... because we don't know when this
  602. // is going to be called
  603. $this->_merged_ranges[] = array($first_row, $first_col, $last_row, $last_col);
  604. }
  605. /**
  606. * Set this worksheet as a selected worksheet,
  607. * i.e. the worksheet has its tab highlighted.
  608. *
  609. * @access public
  610. */
  611. function select()
  612. {
  613. $this->selected = 1;
  614. }
  615. /**
  616. * Set this worksheet as the active worksheet,
  617. * i.e. the worksheet that is displayed when the workbook is opened.
  618. * Also set it as selected.
  619. *
  620. * @access public
  621. */
  622. function activate()
  623. {
  624. $this->selected = 1;
  625. $this->activesheet = $this->index;
  626. }
  627. /**
  628. * Set this worksheet as the first visible sheet.
  629. * This is necessary when there are a large number of worksheets and the
  630. * activated worksheet is not visible on the screen.
  631. *
  632. * @access public
  633. */
  634. function setFirstSheet()
  635. {
  636. $this->firstsheet = $this->index;
  637. }
  638. /**
  639. * Set the worksheet protection flag
  640. * to prevent accidental modification and to
  641. * hide formulas if the locked and hidden format properties have been set.
  642. *
  643. * @access public
  644. * @param string $password The password to use for protecting the sheet.
  645. */
  646. function protect($password, $encoded = false)
  647. {
  648. $this->_protect = 1;
  649. $this->_password = ($encoded ? $password : $this->_encodePassword($password));
  650. }
  651. /**
  652. * Set the width of a single column or a range of columns.
  653. *
  654. * @access public
  655. * @param integer $firstcol first column on the range
  656. * @param integer $lastcol last column on the range
  657. * @param integer $width width to set
  658. * @param mixed $format The optional XF format to apply to the columns
  659. * @param integer $hidden The optional hidden atribute
  660. * @param integer $level The optional outline level
  661. */
  662. function setColumn($firstcol, $lastcol, $width, $format = null, $hidden = 0, $level = 0)
  663. {
  664. $this->_colinfo[] = array($firstcol, $lastcol, $width, &$format, $hidden, $level);
  665. // Set width to zero if column is hidden
  666. $width = ($hidden) ? 0 : $width;
  667. for ($col = $firstcol; $col <= $lastcol; $col++) {
  668. $this->col_sizes[$col] = $width;
  669. }
  670. }
  671. /**
  672. * Set which cell or cells are selected in a worksheet
  673. *
  674. * @access public
  675. * @param integer $first_row first row in the selected quadrant
  676. * @param integer $first_column first column in the selected quadrant
  677. * @param integer $last_row last row in the selected quadrant
  678. * @param integer $last_column last column in the selected quadrant
  679. */
  680. function setSelection($first_row,$first_column,$last_row,$last_column)
  681. {
  682. $this->_selection = array($first_row,$first_column,$last_row,$last_column);
  683. }
  684. /**
  685. * Set panes and mark them as frozen.
  686. *
  687. * @access public
  688. * @param array $panes This is the only parameter received and is composed of the following:
  689. * 0 => Vertical split position,
  690. * 1 => Horizontal split position
  691. * 2 => Top row visible
  692. * 3 => Leftmost column visible
  693. * 4 => Active pane
  694. */
  695. function freezePanes($panes)
  696. {
  697. $this->_frozen = 1;
  698. $this->_panes = $panes;
  699. }
  700. /**
  701. * Set panes and mark them as unfrozen.
  702. *
  703. * @access public
  704. * @param array $panes This is the only parameter received and is composed of the following:
  705. * 0 => Vertical split position,
  706. * 1 => Horizontal split position
  707. * 2 => Top row visible
  708. * 3 => Leftmost column visible
  709. * 4 => Active pane
  710. */
  711. function thawPanes($panes)
  712. {
  713. $this->_frozen = 0;
  714. $this->_panes = $panes;
  715. }
  716. /**
  717. * Set the page orientation as portrait.
  718. *
  719. * @access public
  720. */
  721. function setPortrait()
  722. {
  723. $this->_orientation = 1;
  724. }
  725. /**
  726. * Set the page orientation as landscape.
  727. *
  728. * @access public
  729. */
  730. function setLandscape()
  731. {
  732. $this->_orientation = 0;
  733. }
  734. /**
  735. * Set the paper type. Ex. 1 = US Letter, 9 = A4
  736. *
  737. * @access public
  738. * @param integer $size The type of paper size to use
  739. */
  740. function setPaper($size = 0)
  741. {
  742. $this->_paper_size = $size;
  743. }
  744. /**
  745. * Set the page header caption and optional margin.
  746. *
  747. * @access public
  748. * @param string $string The header text
  749. * @param float $margin optional head margin in inches.
  750. */
  751. function setHeader($string,$margin = 0.50)
  752. {
  753. if (strlen($string) >= 255) {
  754. //carp 'Header string must be less than 255 characters';
  755. return;
  756. }
  757. $this->_header = $string;
  758. $this->_margin_head = $margin;
  759. }
  760. /**
  761. * Set the page footer caption and optional margin.
  762. *
  763. * @access public
  764. * @param string $string The footer text
  765. * @param float $margin optional foot margin in inches.
  766. */
  767. function setFooter($string,$margin = 0.50)
  768. {
  769. if (strlen($string) >= 255) {
  770. //carp 'Footer string must be less than 255 characters';
  771. return;
  772. }
  773. $this->_footer = $string;
  774. $this->_margin_foot = $margin;
  775. }
  776. /**
  777. * Center the page horinzontally.
  778. *
  779. * @access public
  780. * @param integer $center the optional value for centering. Defaults to 1 (center).
  781. */
  782. function centerHorizontally($center = 1)
  783. {
  784. $this->_hcenter = $center;
  785. }
  786. /**
  787. * Center the page vertically.
  788. *
  789. * @access public
  790. * @param integer $center the optional value for centering. Defaults to 1 (center).
  791. */
  792. function centerVertically($center = 1)
  793. {
  794. $this->_vcenter = $center;
  795. }
  796. /**
  797. * Set all the page margins to the same value in inches.
  798. *
  799. * @access public
  800. * @param float $margin The margin to set in inches
  801. */
  802. function setMargins($margin)
  803. {
  804. $this->setMarginLeft($margin);
  805. $this->setMarginRight($margin);
  806. $this->setMarginTop($margin);
  807. $this->setMarginBottom($margin);
  808. }
  809. /**
  810. * Set the left and right margins to the same value in inches.
  811. *
  812. * @access public
  813. * @param float $margin The margin to set in inches
  814. */
  815. function setMargins_LR($margin)
  816. {
  817. $this->setMarginLeft($margin);
  818. $this->setMarginRight($margin);
  819. }
  820. /**
  821. * Set the top and bottom margins to the same value in inches.
  822. *
  823. * @access public
  824. * @param float $margin The margin to set in inches
  825. */
  826. function setMargins_TB($margin)
  827. {
  828. $this->setMarginTop($margin);
  829. $this->setMarginBottom($margin);
  830. }
  831. /**
  832. * Set the left margin in inches.
  833. *
  834. * @access public
  835. * @param float $margin The margin to set in inches
  836. */
  837. function setMarginLeft($margin = 0.75)
  838. {
  839. $this->_margin_left = $margin;
  840. }
  841. /**
  842. * Set the right margin in inches.
  843. *
  844. * @access public
  845. * @param float $margin The margin to set in inches
  846. */
  847. function setMarginRight($margin = 0.75)
  848. {
  849. $this->_margin_right = $margin;
  850. }
  851. /**
  852. * Set the top margin in inches.
  853. *
  854. * @access public
  855. * @param float $margin The margin to set in inches
  856. */
  857. function setMarginTop($margin = 1.00)
  858. {
  859. $this->_margin_top = $margin;
  860. }
  861. /**
  862. * Set the bottom margin in inches.
  863. *
  864. * @access public
  865. * @param float $margin The margin to set in inches
  866. */
  867. function setMarginBottom($margin = 1.00)
  868. {
  869. $this->_margin_bottom = $margin;
  870. }
  871. /**
  872. * Set the rows to repeat at the top of each printed page.
  873. *
  874. * @access public
  875. * @param integer $first_row First row to repeat
  876. * @param integer $last_row Last row to repeat. Optional.
  877. */
  878. function repeatRows($first_row, $last_row = null)
  879. {
  880. $this->title_rowmin = $first_row;
  881. if (isset($last_row)) { //Second row is optional
  882. $this->title_rowmax = $last_row;
  883. } else {
  884. $this->title_rowmax = $first_row;
  885. }
  886. }
  887. /**
  888. * Set the columns to repeat at the left hand side of each printed page.
  889. *
  890. * @access public
  891. * @param integer $first_col First column to repeat
  892. * @param integer $last_col Last column to repeat. Optional.
  893. */
  894. function repeatColumns($first_col, $last_col = null)
  895. {
  896. $this->title_colmin = $first_col;
  897. if (isset($last_col)) { // Second col is optional
  898. $this->title_colmax = $last_col;
  899. } else {
  900. $this->title_colmax = $first_col;
  901. }
  902. }
  903. /**
  904. * Set the area of each worksheet that will be printed.
  905. *
  906. * @access public
  907. * @param integer $first_row First row of the area to print
  908. * @param integer $first_col First column of the area to print
  909. * @param integer $last_row Last row of the area to print
  910. * @param integer $last_col Last column of the area to print
  911. */
  912. function printArea($first_row, $first_col, $last_row, $last_col)
  913. {
  914. $this->print_rowmin = $first_row;
  915. $this->print_colmin = $first_col;
  916. $this->print_rowmax = $last_row;
  917. $this->print_colmax = $last_col;
  918. }
  919. /**
  920. * Set the option to hide gridlines on the printed page.
  921. *
  922. * @access public
  923. */
  924. function hideGridlines()
  925. {
  926. $this->_print_gridlines = 0;
  927. }
  928. /**
  929. * Set the option to hide gridlines on the worksheet (as seen on the screen).
  930. *
  931. * @access public
  932. */
  933. function hideScreenGridlines()
  934. {
  935. $this->_screen_gridlines = 0;
  936. }
  937. /**
  938. * Set the option to print the row and column headers on the printed page.
  939. *
  940. * @access public
  941. * @param integer $print Whether to print the headers or not. Defaults to 1 (print).
  942. */
  943. function printRowColHeaders($print = 1)
  944. {
  945. $this->_print_headers = $print;
  946. }
  947. /**
  948. * Set the vertical and horizontal number of pages that will define the maximum area printed.
  949. * It doesn't seem to work with OpenOffice.
  950. *
  951. * @access public
  952. * @param integer $width Maximun width of printed area in pages
  953. * @param integer $height Maximun heigth of printed area in pages
  954. * @see setPrintScale()
  955. */
  956. function fitToPages($width, $height)
  957. {
  958. $this->_fit_page = 1;
  959. $this->_fit_width = $width;
  960. $this->_fit_height = $height;
  961. }
  962. /**
  963. * Store the horizontal page breaks on a worksheet (for printing).
  964. * The breaks represent the row after which the break is inserted.
  965. *
  966. * @access public
  967. * @param array $breaks Array containing the horizontal page breaks
  968. */
  969. function setHPagebreaks($breaks)
  970. {
  971. foreach ($breaks as $break) {
  972. array_push($this->_hbreaks, $break);
  973. }
  974. }
  975. /**
  976. * Store the vertical page breaks on a worksheet (for printing).
  977. * The breaks represent the column after which the break is inserted.
  978. *
  979. * @access public
  980. * @param array $breaks Array containing the vertical page breaks
  981. */
  982. function setVPagebreaks($breaks)
  983. {
  984. foreach ($breaks as $break) {
  985. array_push($this->_vbreaks, $break);
  986. }
  987. }
  988. /**
  989. * Set the worksheet zoom factor.
  990. *
  991. * @access public
  992. * @param integer $scale The zoom factor
  993. */
  994. function setZoom($scale = 100)
  995. {
  996. // Confine the scale to Excel's range
  997. if ($scale < 10 || $scale > 400) {
  998. throw new Exception("Zoom factor $scale outside range: 10 <= zoom <= 400");
  999. $scale = 100;
  1000. }
  1001. $this->_zoom = floor($scale);
  1002. }
  1003. /**
  1004. * Set the scale factor for the printed page.
  1005. * It turns off the "fit to page" option
  1006. *
  1007. * @access public
  1008. * @param integer $scale The optional scale factor. Defaults to 100
  1009. */
  1010. function setPrintScale($scale = 100)
  1011. {
  1012. // Confine the scale to Excel's range
  1013. if ($scale < 10 || $scale > 400) {
  1014. throw new Exception("Print scale $scale outside range: 10 <= zoom <= 400");
  1015. $scale = 100;
  1016. }
  1017. // Turn off "fit to page" option
  1018. $this->_fit_page = 0;
  1019. $this->_print_scale = floor($scale);
  1020. }
  1021. /**
  1022. * Map to the appropriate write method acording to the token recieved.
  1023. *
  1024. * @access public
  1025. * @param integer $row The row of the cell we are writing to
  1026. * @param integer $col The column of the cell we are writing to
  1027. * @param mixed $token What we are writing
  1028. * @param mixed $format The optional format to apply to the cell
  1029. */
  1030. function write($row, $col, $token, $format = null, $numberFormat = null)
  1031. {
  1032. // Check for a cell reference in A1 notation and substitute row and column
  1033. /*if ($_[0] =~ /^\D/) {
  1034. @_ = $this->_substituteCellref(@_);
  1035. }*/
  1036. if (($numberFormat != 'General') && (PHPExcel_Shared_Date::isDateTimeFormatCode($numberFormat))) {
  1037. if (is_string($token)) {
  1038. // Error string
  1039. return $this->writeString($row, $col, $token, $format);
  1040. } elseif (!is_float($token)) {
  1041. // PHP serialized date/time or date/time object
  1042. return $this->writeNumber($row, $col, PHPExcel_Shared_Date::PHPToExcel($token), $format);
  1043. } else {
  1044. // Excel serialized date/time
  1045. return $this->writeNumber($row, $col, $token, $format);
  1046. }
  1047. } elseif (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
  1048. // Match number
  1049. return $this->writeNumber($row, $col, $token, $format);
  1050. } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
  1051. // Match http or ftp URL
  1052. return $this->writeUrl($row, $col, $token, '', $format);
  1053. } elseif (preg_match("/^mailto:/", $token)) {
  1054. // Match mailto:
  1055. return $this->writeUrl($row, $col, $token, '', $format);
  1056. } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
  1057. // Match internal or external sheet link
  1058. return $this->writeUrl($row, $col, $token, '', $format);
  1059. } elseif (preg_match("/^=/", $token)) {
  1060. // Match formula
  1061. return $this->writeFormula($row, $col, $token, $format);
  1062. } elseif (preg_match("/^@/", $token)) {
  1063. // Match formula
  1064. return $this->writeFormula($row, $col, $token, $format);
  1065. } elseif ($token == '') {
  1066. // Match blank
  1067. return $this->writeBlank($row, $col, $format);
  1068. } else {
  1069. // Default: match string
  1070. return $this->writeString($row, $col, $token, $format);
  1071. }
  1072. }
  1073. /**
  1074. * Write an array of values as a row
  1075. *
  1076. * @access public
  1077. * @param integer $row The row we are writing to
  1078. * @param integer $col The first col (leftmost col) we are writing to
  1079. * @param array $val The array of values to write
  1080. * @param mixed $format The optional format to apply to the cell
  1081. */
  1082. function writeRow($row, $col, $val, $format = null, $numberFormat = null)
  1083. {
  1084. $retval = '';
  1085. if (is_array($val)) {
  1086. foreach ($val as $v) {
  1087. if (is_array($v)) {
  1088. $this->writeCol($row, $col, $v, $format, $numberFormat = null);
  1089. } else {
  1090. $this->write($row, $col, $v, $format, $numberFormat);
  1091. }
  1092. $col++;
  1093. }
  1094. } else {
  1095. throw new Exception('$val needs to be an array');
  1096. }
  1097. return($retval);
  1098. }
  1099. /**
  1100. * Write an array of values as a column
  1101. *
  1102. * @access public
  1103. * @param integer $row The first row (uppermost row) we are writing to
  1104. * @param integer $col The col we are writing to
  1105. * @param array $val The array of values to write
  1106. * @param mixed $format The optional format to apply to the cell
  1107. */
  1108. function writeCol($row, $col, $val, $format = null, $numberFormat = null)
  1109. {
  1110. $retval = '';
  1111. if (is_array($val)) {
  1112. foreach ($val as $v) {
  1113. $this->write($row, $col, $v, $format, $numberFormat);
  1114. $row++;
  1115. }
  1116. } else {
  1117. throw new Exception('$val needs to be an array');
  1118. }
  1119. return($retval);
  1120. }
  1121. /**
  1122. * Returns an index to the XF record in the workbook
  1123. *
  1124. * @access private
  1125. * @param mixed &$format The optional XF format
  1126. * @return integer The XF record index
  1127. */
  1128. function _XF(&$format)
  1129. {
  1130. if ($format) {
  1131. return($format->getXfIndex());
  1132. } else {
  1133. return(0x0F);
  1134. }
  1135. }
  1136. /******************************************************************************
  1137. *******************************************************************************
  1138. *
  1139. * Internal methods
  1140. */
  1141. /**
  1142. * Store Worksheet data in memory using the parent's class append() or to a
  1143. * temporary file, the default.
  1144. *
  1145. * @access private
  1146. * @param string $data The binary data to append
  1147. */
  1148. function _append($data)
  1149. {
  1150. if ($this->_using_tmpfile) {
  1151. // Add CONTINUE records if necessary
  1152. if (strlen($data) > $this->_limit) {
  1153. $data = $this->_addContinue($data);
  1154. }
  1155. fwrite($this->_filehandle, $data);
  1156. $this->_datasize += strlen($data);
  1157. } else {
  1158. parent::_append($data);
  1159. }
  1160. }
  1161. /**
  1162. * Substitute an Excel cell reference in A1 notation for zero based row and
  1163. * column values in an argument list.
  1164. *
  1165. * Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
  1166. *
  1167. * @access private
  1168. * @param string $cell The cell reference. Or range of cells.
  1169. * @return array
  1170. */
  1171. function _substituteCellref($cell)
  1172. {
  1173. $cell = strtoupper($cell);
  1174. // Convert a column range: 'A:A' or 'B:G'
  1175. if (preg_match("/([A-I]?[A-Z]):([A-I]?[A-Z])/", $cell, $match)) {
  1176. list($no_use, $col1) = $this->_cellToRowcol($match[1] .'1'); // Add a dummy row
  1177. list($no_use, $col2) = $this->_cellToRowcol($match[2] .'1'); // Add a dummy row
  1178. return(array($col1, $col2));
  1179. }
  1180. // Convert a cell range: 'A1:B7'
  1181. if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/", $cell, $match)) {
  1182. list($row1, $col1) = $this->_cellToRowcol($match[1]);
  1183. list($row2, $col2) = $this->_cellToRowcol($match[2]);
  1184. return(array($row1, $col1, $row2, $col2));
  1185. }
  1186. // Convert a cell reference: 'A1' or 'AD2000'
  1187. if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) {
  1188. list($row1, $col1) = $this->_cellToRowcol($match[1]);
  1189. return(array($row1, $col1));
  1190. }
  1191. // TODO use real error codes
  1192. throw new Exception("Unknown cell reference $cell");
  1193. }
  1194. /**
  1195. * Convert an Excel cell reference in A1 notation to a zero based row and column
  1196. * reference; converts C1 to (0, 2).
  1197. *
  1198. * @access private
  1199. * @param string $cell The cell reference.
  1200. * @return array containing (row, column)
  1201. */
  1202. function _cellToRowcol($cell)
  1203. {
  1204. preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
  1205. $col = $match[1];
  1206. $row = $match[2];
  1207. // Convert base26 column string to number
  1208. $chars = split('', $col);
  1209. $expn = 0;
  1210. $col = 0;
  1211. while ($chars) {
  1212. $char = array_pop($chars); // LS char first
  1213. $col += (ord($char) -ord('A') +1) * pow(26,$expn);
  1214. $expn++;
  1215. }
  1216. // Convert 1-index to zero-index
  1217. $row--;
  1218. $col--;
  1219. return(array($row, $col));
  1220. }
  1221. /**
  1222. * Based on the algorithm provided by Daniel Rentz of OpenOffice.
  1223. *
  1224. * @access private
  1225. * @param string $plaintext The password to be encoded in plaintext.
  1226. * @return string The encoded password
  1227. */
  1228. function _encodePassword($plaintext)
  1229. {
  1230. $password = 0x0000;
  1231. $i = 1; // char position
  1232. // split the plain text password in its component characters
  1233. $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY);
  1234. foreach ($chars as $char) {
  1235. $value = ord($char) << $i; // shifted ASCII value
  1236. $rotated_bits = $value >> 15; // rotated bits beyond bit 15
  1237. $value &= 0x7fff; // first 15 bits
  1238. $password ^= ($value | $rotated_bits);
  1239. $i++;
  1240. }
  1241. $password ^= strlen($plaintext);
  1242. $password ^= 0xCE4B;
  1243. return($password);
  1244. }
  1245. /**
  1246. * This method sets the properties for outlining and grouping. The defaults
  1247. * correspond to Excel's defaults.
  1248. *
  1249. * @param bool $visible
  1250. * @param bool $symbols_below
  1251. * @param bool $symbols_right
  1252. * @param bool $auto_style
  1253. */
  1254. function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
  1255. {
  1256. $this->_outline_on = $visible;
  1257. $this->_outline_below = $symbols_below;
  1258. $this->_outline_right = $symbols_right;
  1259. $this->_outline_style = $auto_style;
  1260. // Ensure this is a boolean vale for Window2
  1261. if ($this->_outline_on) {
  1262. $this->_outline_on = 1;
  1263. }
  1264. }
  1265. /******************************************************************************
  1266. *******************************************************************************
  1267. *
  1268. * BIFF RECORDS
  1269. */
  1270. /**
  1271. * Write a double to the specified row and column (zero indexed).
  1272. * An integer can be written as a double. Excel will display an
  1273. * integer. $format is optional.
  1274. *
  1275. * Returns 0 : normal termination
  1276. * -2 : row or column out of range
  1277. *
  1278. * @access public
  1279. * @param integer $row Zero indexed row
  1280. * @param integer $col Zero indexed column
  1281. * @param float $num The number to write
  1282. * @param mixed $format The optional XF format
  1283. * @return integer
  1284. */
  1285. function writeNumber($row, $col, $num, $format = null)
  1286. {
  1287. $record = 0x0203; // Record identifier
  1288. $length = 0x000E; // Number of bytes to follow
  1289. $xf = $this->_XF($format); // The cell format
  1290. // Check that row and col are valid and store max and min values
  1291. if ($row >= $this->_xls_rowmax) {
  1292. return(-2);
  1293. }
  1294. if ($col >= $this->_xls_colmax) {
  1295. return(-2);
  1296. }
  1297. if ($row < $this->_dim_rowmin) {
  1298. $this->_dim_rowmin = $row;
  1299. }
  1300. if ($row > $this->_dim_rowmax) {
  1301. $this->_dim_rowmax = $row;
  1302. }
  1303. if ($col < $this->_dim_colmin) {
  1304. $this->_dim_colmin = $col;
  1305. }
  1306. if ($col > $this->_dim_colmax) {
  1307. $this->_dim_colmax = $col;
  1308. }
  1309. $header = pack("vv", $record, $length);
  1310. $data = pack("vvv", $row, $col, $xf);
  1311. $xl_double = pack("d", $num);
  1312. if ($this->_byte_order) { // if it's Big Endian
  1313. $xl_double = strrev($xl_double);
  1314. }
  1315. $this->_append($header.$data.$xl_double);
  1316. return(0);
  1317. }
  1318. /**
  1319. * Write a string to the specified row and column (zero indexed).
  1320. * NOTE: there is an Excel 5 defined limit of 255 characters.
  1321. * $format is optional.
  1322. * Returns 0 : normal termination
  1323. * -2 : row or column out of range
  1324. * -3 : long string truncated to 255 chars
  1325. *
  1326. * @access public
  1327. * @param integer $row Zero indexed row
  1328. * @param integer $col Zero indexed column
  1329. * @param string $str The string to write
  1330. * @param mixed $format The XF format for the cell
  1331. * @return integer
  1332. */
  1333. function writeString($row, $col, $str, $format = null)
  1334. {
  1335. if ($this->_BIFF_version == 0x0600) {
  1336. return $this->writeStringBIFF8($row, $col, $str, $format);
  1337. }
  1338. $strlen = strlen($str);
  1339. $record = 0x0204; // Record identifier
  1340. $length = 0x0008 + $strlen; // Bytes to follow
  1341. $xf = $this->_XF($format); // The cell format
  1342. $str_error = 0;
  1343. // Check that row and col are valid and store max and min values
  1344. if ($row >= $this->_xls_rowmax) {
  1345. return(-2);
  1346. }
  1347. if ($col >= $this->_xls_colmax) {
  1348. return(-2);
  1349. }
  1350. if ($row < $this->_dim_rowmin) {
  1351. $this->_dim_rowmin = $row;
  1352. }
  1353. if ($row > $this->_dim_rowmax) {
  1354. $this->_dim_rowmax = $row;
  1355. }
  1356. if ($col < $this->_dim_colmin) {
  1357. $this->_dim_colmin = $col;
  1358. }
  1359. if ($col > $this->_dim_colmax) {
  1360. $this->_dim_colmax = $col;
  1361. }
  1362. if ($strlen > $this->_xls_strmax) { // LABEL must be < 255 chars
  1363. $str = substr($str, 0, $this->_xls_strmax);
  1364. $length = 0x0008 + $this->_xls_strmax;
  1365. $strlen = $this->_xls_strmax;
  1366. $str_error = -3;
  1367. }
  1368. $header = pack("vv", $record, $length);
  1369. $data = pack("vvvv", $row, $col, $xf, $strlen);
  1370. $this->_append($header . $data . $str);
  1371. return($str_error);
  1372. }
  1373. var $_biff8_input_encoding = 'UTF-16LE';
  1374. function setBIFF8InputEncoding($encoding) {
  1375. if ($encoding != 'UTF-16LE' && !function_exists('iconv')) {
  1376. $this->raiseError("Using an input encoding other than UTF-16LE requires PHP support for iconv");
  1377. }
  1378. $this->_biff8_input_encoding = $encoding;
  1379. }
  1380. /**
  1381. * Sets Input Encoding for writing strings
  1382. *
  1383. * @access public
  1384. * @param string $encoding The encoding. Ex: 'UTF-16LE', 'utf-8', 'ISO-859-7'
  1385. */
  1386. function setInputEncoding($encoding)
  1387. {
  1388. if ($encoding != 'UTF-16LE' && !function_exists('iconv')) {
  1389. throw new Exception("Using an input encoding other than UTF-16LE requires PHP support for iconv");
  1390. }
  1391. $this->_input_encoding = $encoding;
  1392. }
  1393. /**
  1394. * Write a string to the specified row and column (zero indexed).
  1395. * This is the BIFF8 version (no 255 chars limit).
  1396. * $format is optional.
  1397. * Returns 0 : normal termination
  1398. * -2 : row or column out of range
  1399. * -3 : long string truncated to 255 chars
  1400. *
  1401. * @access public
  1402. * @param integer $row Zero indexed row
  1403. * @param integer $col Zero indexed column
  1404. * @param string $str The string to write
  1405. * @param mixed $format The XF format for the cell
  1406. * @return integer
  1407. */
  1408. function writeStringBIFF8($row, $col, $str, $format = null)
  1409. {
  1410. if ($this->_input_encoding == 'UTF-16LE')
  1411. {
  1412. $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1413. $encoding = 0x1;
  1414. }
  1415. elseif ($this->_input_encoding != '')
  1416. {
  1417. $str = iconv($this->_input_encoding, 'UTF-16LE', $str);
  1418. $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1419. $encoding = 0x1;
  1420. }
  1421. else
  1422. {
  1423. $strlen = strlen($str);
  1424. $encoding = 0x0;
  1425. }
  1426. $record = 0x00FD; // Record identifier
  1427. $length = 0x000A; // Bytes to follow
  1428. $xf = $this->_XF($format); // The cell format
  1429. $str_error = 0;
  1430. // Check that row and col are valid and store max and min values
  1431. if ($this->_checkRowCol($row, $col) == false) {
  1432. return -2;
  1433. }
  1434. $str = pack('vC', $strlen, $encoding).$str;
  1435. /* check if string is already present */
  1436. if (!isset($this->_str_table[$str])) {
  1437. $this->_str_table[$str] = $this->_str_unique++;
  1438. }
  1439. $this->_str_total++;
  1440. $header = pack('vv', $record, $length);
  1441. $data = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]);
  1442. $this->_append($header.$data);
  1443. return $str_error;
  1444. }
  1445. /**
  1446. * Check row and col before writing to a cell, and update the sheet's
  1447. * dimensions accordingly
  1448. *
  1449. * @access private
  1450. * @param integer $row Zero indexed row
  1451. * @param integer $col Zero indexed column
  1452. * @return boolean true for success, false if row and/or col are grester
  1453. * then maximums allowed.
  1454. */
  1455. function _checkRowCol($row, $col)
  1456. {
  1457. if ($row >= $this->_xls_rowmax) {
  1458. return false;
  1459. }
  1460. if ($col >= $this->_xls_colmax) {
  1461. return false;
  1462. }
  1463. if ($row < $this->_dim_rowmin) {
  1464. $this->_dim_rowmin = $row;
  1465. }
  1466. if ($row > $this->_dim_rowmax) {
  1467. $this->_dim_rowmax = $row;
  1468. }
  1469. if ($col < $this->_dim_colmin) {
  1470. $this->_dim_colmin = $col;
  1471. }
  1472. if ($col > $this->_dim_colmax) {
  1473. $this->_dim_colmax = $col;
  1474. }
  1475. return true;
  1476. }
  1477. /**
  1478. * Writes a note associated with the cell given by the row and column.
  1479. * NOTE records don't have a length limit.
  1480. *
  1481. * @access public
  1482. * @param integer $row Zero indexed row
  1483. * @param integer $col Zero indexed column
  1484. * @param string $note The note to write
  1485. */
  1486. function writeNote($row, $col, $note)
  1487. {
  1488. $note_length = strlen($note);
  1489. $record = 0x001C; // Record identifier
  1490. $max_length = 2048; // Maximun length for a NOTE record
  1491. //$length = 0x0006 + $note_length; // Bytes to follow
  1492. // Check that row and col are valid and store max and min values
  1493. if ($row >= $this->_xls_rowmax) {
  1494. return(-2);
  1495. }
  1496. if ($col >= $this->_xls_colmax) {
  1497. return(-2);
  1498. }
  1499. if ($row < $this->_dim_rowmin) {
  1500. $this->_dim_rowmin = $row;
  1501. }
  1502. if ($row > $this->_dim_rowmax) {
  1503. $this->_dim_rowmax = $row;
  1504. }
  1505. if ($col < $this->_dim_colmin) {
  1506. $this->_dim_colmin = $col;
  1507. }
  1508. if ($col > $this->_dim_colmax) {
  1509. $this->_dim_colmax = $col;
  1510. }
  1511. // Length for this record is no more than 2048 + 6
  1512. $length = 0x0006 + min($note_length, 2048);
  1513. $header = pack("vv", $record, $length);
  1514. $data = pack("vvv", $row, $col, $note_length);
  1515. $this->_append($header . $data . substr($note, 0, 2048));
  1516. for ($i = $max_length; $i < $note_length; $i += $max_length) {
  1517. $chunk = substr($note, $i, $max_length);
  1518. $length = 0x0006 + strlen($chunk);
  1519. $header = pack("vv", $record, $length);
  1520. $data = pack("vvv", -1, 0, strlen($chunk));
  1521. $this->_append($header.$data.$chunk);
  1522. }
  1523. return(0);
  1524. }
  1525. /**
  1526. * Write a blank cell to the specified row and column (zero indexed).
  1527. * A blank cell is used to specify formatting without adding a string
  1528. * or a number.
  1529. *
  1530. * A blank cell without a format serves no purpose. Therefore, we don't write
  1531. * a BLANK record unless a format is specified.
  1532. *
  1533. * Returns 0 : normal termination (including no format)
  1534. * -1 : insufficient number of arguments
  1535. * -2 : row or column out of range
  1536. *
  1537. * @access public
  1538. * @param integer $row Zero indexed row
  1539. * @param integer $col Zero indexed column
  1540. * @param mixed $format The XF format
  1541. */
  1542. function writeBlank($row, $col, $format)
  1543. {
  1544. // Don't write a blank cell unless it has a format
  1545. if (!$format) {
  1546. return(0);
  1547. }
  1548. $record = 0x0201; // Record identifier
  1549. $length = 0x0006; // Number of bytes to follow
  1550. $xf = $this->_XF($format); // The cell format
  1551. // Check that row and col are valid and store max and min values
  1552. if ($row >= $this->_xls_rowmax) {
  1553. return(-2);
  1554. }
  1555. if ($col >= $this->_xls_colmax) {
  1556. return(-2);
  1557. }
  1558. if ($row < $this->_dim_rowmin) {
  1559. $this->_dim_rowmin = $row;
  1560. }
  1561. if ($row > $this->_dim_rowmax) {
  1562. $this->_dim_rowmax = $row;
  1563. }
  1564. if ($col < $this->_dim_colmin) {
  1565. $this->_dim_colmin = $col;
  1566. }
  1567. if ($col > $this->_dim_colmax) {
  1568. $this->_dim_colmax = $col;
  1569. }
  1570. $header = pack("vv", $record, $length);
  1571. $data = pack("vvv", $row, $col, $xf);
  1572. $this->_append($header . $data);
  1573. return 0;
  1574. }
  1575. /**
  1576. * Write a formula to the specified row and column (zero indexed).
  1577. * The textual representation of the formula is passed to the parser in
  1578. * Parser.php which returns a packed binary string.
  1579. *
  1580. * Returns 0 : normal termination
  1581. * -1 : formula errors (bad formula)
  1582. * -2 : row or column out of range
  1583. *
  1584. * @access public
  1585. * @param integer $row Zero indexed row
  1586. * @param integer $col Zero indexed column
  1587. * @param string $formula The formula text string
  1588. * @param mixed $format The optional XF format
  1589. * @return integer
  1590. */
  1591. function writeFormula($row, $col, $formula, $format = null)
  1592. {
  1593. $record = 0x0006; // Record identifier
  1594. // Excel normally stores the last calculated value of the formula in $num.
  1595. // Clearly we are not in a position to calculate this a priori. Instead
  1596. // we set $num to zero and set the option flags in $grbit to ensure
  1597. // automatic calculation of the formula when the file is opened.
  1598. //
  1599. $xf = $this->_XF($format); // The cell format
  1600. $num = 0x00; // Current value of formula
  1601. $grbit = 0x03; // Option flags
  1602. $unknown = 0x0000; // Must be zero
  1603. // Check that row and col are valid and store max and min values
  1604. if ($this->_checkRowCol($row, $col) == false) {
  1605. return -2;
  1606. }
  1607. // Strip the '=' or '@' sign at the beginning of the formula string
  1608. if (preg_match("/^=/", $formula)) {
  1609. $formula = preg_replace("/(^=)/", "", $formula);
  1610. } elseif (preg_match("/^@/", $formula)) {
  1611. $formula = preg_replace("/(^@)/", "", $formula);
  1612. } else {
  1613. // Error handling
  1614. $this->writeString($row, $col, 'Unrecognised character for formula');
  1615. return -1;
  1616. }
  1617. // Parse the formula using the parser in Parser.php
  1618. $error = $this->_parser->parse($formula);
  1619. $formula = $this->_parser->toReversePolish();
  1620. $formlen = strlen($formula); // Length of the binary string
  1621. $length = 0x16 + $formlen; // Length of the record data
  1622. $header = pack("vv", $record, $length);
  1623. $data = pack("vvvdvVv", $row, $col, $xf, $num,
  1624. $grbit, $unknown, $formlen);
  1625. $this->_append($header . $data . $formula);
  1626. return 0;
  1627. }
  1628. /**
  1629. * Write a hyperlink.
  1630. * This is comprised of two elements: the visible label and
  1631. * the invisible link. The visible label is the same as the link unless an
  1632. * alternative string is specified. The label is written using the
  1633. * writeString() method. Therefore the 255 characters string limit applies.
  1634. * $string and $format are optional.
  1635. *
  1636. * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
  1637. * directory url.
  1638. *
  1639. * Returns 0 : normal termination
  1640. * -2 : row or column out of range
  1641. * -3 : long string truncated to 255 chars
  1642. *
  1643. * @access public
  1644. * @param integer $row Row
  1645. * @param integer $col Column
  1646. * @param string $url URL string
  1647. * @param string $string Alternative label
  1648. * @param mixed $format The cell format
  1649. * @return integer
  1650. */
  1651. function writeUrl($row, $col, $url, $string = '', $format = null)
  1652. {
  1653. // Add start row and col to arg list
  1654. return($this->_writeUrlRange($row, $col, $row, $col, $url, $string, $format));
  1655. }
  1656. /**
  1657. * This is the more general form of writeUrl(). It allows a hyperlink to be
  1658. * written to a range of cells. This function also decides the type of hyperlink
  1659. * to be written. These are either, Web (http, ftp, mailto), Internal
  1660. * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
  1661. *
  1662. * @access private
  1663. * @see writeUrl()
  1664. * @param integer $row1 Start row
  1665. * @param integer $col1 Start column
  1666. * @param integer $row2 End row
  1667. * @param integer $col2 End column
  1668. * @param string $url URL string
  1669. * @param string $string Alternative label
  1670. * @param mixed $format The cell format
  1671. * @return integer
  1672. */
  1673. function _writeUrlRange($row1, $col1, $row2, $col2, $url, $string = '', $format = null)
  1674. {
  1675. // Check for internal/external sheet links or default to web link
  1676. if (preg_match('[^internal:]', $url)) {
  1677. return($this->_writeUrlInternal($row1, $col1, $row2, $col2, $url, $string, $format));
  1678. }
  1679. if (preg_match('[^external:]', $url)) {
  1680. return($this->_writeUrlExternal($row1, $col1, $row2, $col2, $url, $string, $format));
  1681. }
  1682. return($this->_writeUrlWeb($row1, $col1, $row2, $col2, $url, $string, $format));
  1683. }
  1684. /**
  1685. * Used to write http, ftp and mailto hyperlinks.
  1686. * The link type ($options) is 0x03 is the same as absolute dir ref without
  1687. * sheet. However it is differentiated by the $unknown2 data stream.
  1688. *
  1689. * @access private
  1690. * @see writeUrl()
  1691. * @param integer $row1 Start row
  1692. * @param integer $col1 Start column
  1693. * @param integer $row2 End row
  1694. * @param integer $col2 End column
  1695. * @param string $url URL string
  1696. * @param string $str Alternative label
  1697. * @param mixed $format The cell format
  1698. * @return integer
  1699. */
  1700. function _writeUrlWeb($row1, $col1, $row2, $col2, $url, $str, $format = null)
  1701. {
  1702. $record = 0x01B8; // Record identifier
  1703. $length = 0x00000; // Bytes to follow
  1704. if (!$format) {
  1705. $format = $this->_url_format;
  1706. }
  1707. // Write the visible label using the writeString() method.
  1708. if ($str == '') {
  1709. $str = $url;
  1710. }
  1711. $str_error = $this->writeString($row1, $col1, $str, $format);
  1712. if (($str_error == -2) || ($str_error == -3)) {
  1713. return $str_error;
  1714. }
  1715. // Pack the undocumented parts of the hyperlink stream
  1716. $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
  1717. $unknown2 = pack("H*", "E0C9EA79F9BACE118C8200AA004BA90B");
  1718. // Pack the option flags
  1719. $options = pack("V", 0x03);
  1720. // Convert URL to a null terminated wchar string
  1721. $url = join("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
  1722. $url = $url . "\0\0\0";
  1723. // Pack the length of the URL
  1724. $url_len = pack("V", strlen($url));
  1725. // Calculate the data length
  1726. $length = 0x34 + strlen($url);
  1727. // Pack the header data
  1728. $header = pack("vv", $record, $length);
  1729. $data = pack("vvvv", $row1, $row2, $col1, $col2);
  1730. // Write the packed data
  1731. $this->_append($header . $data .
  1732. $unknown1 . $options .
  1733. $unknown2 . $url_len . $url);
  1734. return($str_error);
  1735. }
  1736. /**
  1737. * Used to write internal reference hyperlinks such as "Sheet1!A1".
  1738. *
  1739. * @access private
  1740. * @see writeUrl()
  1741. * @param integer $row1 Start row
  1742. * @param integer $col1 Start column
  1743. * @param integer $row2 End row
  1744. * @param integer $col2 End column
  1745. * @param string $url URL string
  1746. * @param string $str Alternative label
  1747. * @param mixed $format The cell format
  1748. * @return integer
  1749. */
  1750. function _writeUrlInternal($row1, $col1, $row2, $col2, $url, $str, $format = null)
  1751. {
  1752. $record = 0x01B8; // Record identifier
  1753. $length = 0x00000; // Bytes to follow
  1754. if (!$format) {
  1755. $format = $this->_url_format;
  1756. }
  1757. // Strip URL type
  1758. $url = preg_replace('/^internal:/', '', $url);
  1759. // Write the visible label
  1760. if ($str == '') {
  1761. $str = $url;
  1762. }
  1763. $str_error = $this->writeString($row1, $col1, $str, $format);
  1764. if (($str_error == -2) || ($str_error == -3)) {
  1765. return $str_error;
  1766. }
  1767. // Pack the undocumented parts of the hyperlink stream
  1768. $unknown1 = pack("H*", "D0C9EA79F9BACE118C8200AA004BA90B02000000");
  1769. // Pack the option flags
  1770. $options = pack("V", 0x08);
  1771. // Convert the URL type and to a null terminated wchar string
  1772. $url = join("\0", preg_split(

Large files files are truncated, but you can click here to view the full file