PageRenderTime 140ms CodeModel.GetById 25ms RepoModel.GetById 3ms app.codeStats 0ms

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

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

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