PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/vendors/excel/Worksheet.php

https://bitbucket.org/sanjeevam/beug
PHP | 3502 lines | 1738 code | 423 blank | 1341 comment | 267 complexity | 6dc3536487897df951f55d42db9228d4 MD5 | raw file

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. * Spreadsheet_Excel_Writer: A library for generating Excel Spreadsheets
  18. * Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  19. *
  20. * This library is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU Lesser General Public
  22. * License as published by the Free Software Foundation; either
  23. * version 2.1 of the License, or (at your option) any later version.
  24. *
  25. * This library is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  28. * Lesser General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Lesser General Public
  31. * License along with this library; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33. */
  34. require_once 'Parser.php';
  35. require_once 'BIFFwriter.php';
  36. /**
  37. * Class for generating Excel Spreadsheets
  38. *
  39. * @author Xavier Noguer <xnoguer@rezebra.com>
  40. * @category FileFormats
  41. * @package Spreadsheet_Excel_Writer
  42. */
  43. class Spreadsheet_Excel_Writer_Worksheet extends Spreadsheet_Excel_Writer_BIFFwriter
  44. {
  45. /**
  46. * Name of the Worksheet
  47. * @var string
  48. */
  49. var $name;
  50. /**
  51. * Index for the Worksheet
  52. * @var integer
  53. */
  54. var $index;
  55. /**
  56. * Reference to the (default) Format object for URLs
  57. * @var object Format
  58. */
  59. var $_url_format;
  60. /**
  61. * Reference to the parser used for parsing formulas
  62. * @var object Format
  63. */
  64. var $_parser;
  65. /**
  66. * Filehandle to the temporary file for storing data
  67. * @var resource
  68. */
  69. var $_filehandle;
  70. /**
  71. * Boolean indicating if we are using a temporary file for storing data
  72. * @var bool
  73. */
  74. var $_using_tmpfile;
  75. /**
  76. * Maximum number of rows for an Excel spreadsheet (BIFF5)
  77. * @var integer
  78. */
  79. var $_xls_rowmax;
  80. /**
  81. * Maximum number of columns for an Excel spreadsheet (BIFF5)
  82. * @var integer
  83. */
  84. var $_xls_colmax;
  85. /**
  86. * Maximum number of characters for a string (LABEL record in BIFF5)
  87. * @var integer
  88. */
  89. var $_xls_strmax;
  90. /**
  91. * First row for the DIMENSIONS record
  92. * @var integer
  93. * @see _storeDimensions()
  94. */
  95. var $_dim_rowmin;
  96. /**
  97. * Last row for the DIMENSIONS record
  98. * @var integer
  99. * @see _storeDimensions()
  100. */
  101. var $_dim_rowmax;
  102. /**
  103. * First column for the DIMENSIONS record
  104. * @var integer
  105. * @see _storeDimensions()
  106. */
  107. var $_dim_colmin;
  108. /**
  109. * Last column for the DIMENSIONS record
  110. * @var integer
  111. * @see _storeDimensions()
  112. */
  113. var $_dim_colmax;
  114. /**
  115. * Array containing format information for columns
  116. * @var array
  117. */
  118. var $_colinfo;
  119. /**
  120. * Array containing the selected area for the worksheet
  121. * @var array
  122. */
  123. var $_selection;
  124. /**
  125. * Array containing the panes for the worksheet
  126. * @var array
  127. */
  128. var $_panes;
  129. /**
  130. * The active pane for the worksheet
  131. * @var integer
  132. */
  133. var $_active_pane;
  134. /**
  135. * Bit specifying if panes are frozen
  136. * @var integer
  137. */
  138. var $_frozen;
  139. /**
  140. * Bit specifying if the worksheet is selected
  141. * @var integer
  142. */
  143. var $selected;
  144. /**
  145. * The paper size (for printing) (DOCUMENT!!!)
  146. * @var integer
  147. */
  148. var $_paper_size;
  149. /**
  150. * Bit specifying paper orientation (for printing). 0 => landscape, 1 => portrait
  151. * @var integer
  152. */
  153. var $_orientation;
  154. /**
  155. * The page header caption
  156. * @var string
  157. */
  158. var $_header;
  159. /**
  160. * The page footer caption
  161. * @var string
  162. */
  163. var $_footer;
  164. /**
  165. * The horizontal centering value for the page
  166. * @var integer
  167. */
  168. var $_hcenter;
  169. /**
  170. * The vertical centering value for the page
  171. * @var integer
  172. */
  173. var $_vcenter;
  174. /**
  175. * The margin for the header
  176. * @var float
  177. */
  178. var $_margin_head;
  179. /**
  180. * The margin for the footer
  181. * @var float
  182. */
  183. var $_margin_foot;
  184. /**
  185. * The left margin for the worksheet in inches
  186. * @var float
  187. */
  188. var $_margin_left;
  189. /**
  190. * The right margin for the worksheet in inches
  191. * @var float
  192. */
  193. var $_margin_right;
  194. /**
  195. * The top margin for the worksheet in inches
  196. * @var float
  197. */
  198. var $_margin_top;
  199. /**
  200. * The bottom margin for the worksheet in inches
  201. * @var float
  202. */
  203. var $_margin_bottom;
  204. /**
  205. * First row to reapeat on each printed page
  206. * @var integer
  207. */
  208. var $title_rowmin;
  209. /**
  210. * Last row to reapeat on each printed page
  211. * @var integer
  212. */
  213. var $title_rowmax;
  214. /**
  215. * First column to reapeat on each printed page
  216. * @var integer
  217. */
  218. var $title_colmin;
  219. /**
  220. * First row of the area to print
  221. * @var integer
  222. */
  223. var $print_rowmin;
  224. /**
  225. * Last row to of the area to print
  226. * @var integer
  227. */
  228. var $print_rowmax;
  229. /**
  230. * First column of the area to print
  231. * @var integer
  232. */
  233. var $print_colmin;
  234. /**
  235. * Last column of the area to print
  236. * @var integer
  237. */
  238. var $print_colmax;
  239. /**
  240. * Whether to use outline.
  241. * @var integer
  242. */
  243. var $_outline_on;
  244. /**
  245. * Auto outline styles.
  246. * @var bool
  247. */
  248. var $_outline_style;
  249. /**
  250. * Whether to have outline summary below.
  251. * @var bool
  252. */
  253. var $_outline_below;
  254. /**
  255. * Whether to have outline summary at the right.
  256. * @var bool
  257. */
  258. var $_outline_right;
  259. /**
  260. * Outline row level.
  261. * @var integer
  262. */
  263. var $_outline_row_level;
  264. /**
  265. * Whether to fit to page when printing or not.
  266. * @var bool
  267. */
  268. var $_fit_page;
  269. /**
  270. * Number of pages to fit wide
  271. * @var integer
  272. */
  273. var $_fit_width;
  274. /**
  275. * Number of pages to fit high
  276. * @var integer
  277. */
  278. var $_fit_height;
  279. /**
  280. * Reference to the total number of strings in the workbook
  281. * @var integer
  282. */
  283. var $_str_total;
  284. /**
  285. * Reference to the number of unique strings in the workbook
  286. * @var integer
  287. */
  288. var $_str_unique;
  289. /**
  290. * Reference to the array containing all the unique strings in the workbook
  291. * @var array
  292. */
  293. var $_str_table;
  294. /**
  295. * Merged cell ranges
  296. * @var array
  297. */
  298. var $_merged_ranges;
  299. /**
  300. * Charset encoding currently used when calling writeString()
  301. * @var string
  302. */
  303. var $_input_encoding;
  304. /**
  305. * Constructor
  306. *
  307. * @param string $name The name of the new worksheet
  308. * @param integer $index The index of the new worksheet
  309. * @param mixed &$activesheet The current activesheet of the workbook we belong to
  310. * @param mixed &$firstsheet The first worksheet in the workbook we belong to
  311. * @param mixed &$url_format The default format for hyperlinks
  312. * @param mixed &$parser The formula parser created for the Workbook
  313. * @access private
  314. */
  315. function Spreadsheet_Excel_Writer_Worksheet($BIFF_version, $name,
  316. $index, &$activesheet,
  317. &$firstsheet, &$str_total,
  318. &$str_unique, &$str_table,
  319. &$url_format, &$parser)
  320. {
  321. // It needs to call its parent's constructor explicitly
  322. $this->Spreadsheet_Excel_Writer_BIFFwriter();
  323. $this->_BIFF_version = $BIFF_version;
  324. $rowmax = 65536; // 16384 in Excel 5
  325. $colmax = 256;
  326. $this->name = $name;
  327. $this->index = $index;
  328. $this->activesheet = &$activesheet;
  329. $this->firstsheet = &$firstsheet;
  330. $this->_str_total = &$str_total;
  331. $this->_str_unique = &$str_unique;
  332. $this->_str_table = &$str_table;
  333. $this->_url_format = &$url_format;
  334. $this->_parser = &$parser;
  335. //$this->ext_sheets = array();
  336. $this->_filehandle = '';
  337. $this->_using_tmpfile = true;
  338. //$this->fileclosed = 0;
  339. //$this->offset = 0;
  340. $this->_xls_rowmax = $rowmax;
  341. $this->_xls_colmax = $colmax;
  342. $this->_xls_strmax = 255;
  343. $this->_dim_rowmin = $rowmax + 1;
  344. $this->_dim_rowmax = 0;
  345. $this->_dim_colmin = $colmax + 1;
  346. $this->_dim_colmax = 0;
  347. $this->_colinfo = array();
  348. $this->_selection = array(0,0,0,0);
  349. $this->_panes = array();
  350. $this->_active_pane = 3;
  351. $this->_frozen = 0;
  352. $this->selected = 0;
  353. $this->_paper_size = 0x0;
  354. $this->_orientation = 0x1;
  355. $this->_header = '';
  356. $this->_footer = '';
  357. $this->_hcenter = 0;
  358. $this->_vcenter = 0;
  359. $this->_margin_head = 0.50;
  360. $this->_margin_foot = 0.50;
  361. $this->_margin_left = 0.75;
  362. $this->_margin_right = 0.75;
  363. $this->_margin_top = 1.00;
  364. $this->_margin_bottom = 1.00;
  365. $this->title_rowmin = null;
  366. $this->title_rowmax = null;
  367. $this->title_colmin = null;
  368. $this->title_colmax = null;
  369. $this->print_rowmin = null;
  370. $this->print_rowmax = null;
  371. $this->print_colmin = null;
  372. $this->print_colmax = null;
  373. $this->_print_gridlines = 1;
  374. $this->_screen_gridlines = 1;
  375. $this->_print_headers = 0;
  376. $this->_fit_page = 0;
  377. $this->_fit_width = 0;
  378. $this->_fit_height = 0;
  379. $this->_hbreaks = array();
  380. $this->_vbreaks = array();
  381. $this->_protect = 0;
  382. $this->_password = null;
  383. $this->col_sizes = array();
  384. $this->_row_sizes = array();
  385. $this->_zoom = 100;
  386. $this->_print_scale = 100;
  387. $this->_outline_row_level = 0;
  388. $this->_outline_style = 0;
  389. $this->_outline_below = 1;
  390. $this->_outline_right = 1;
  391. $this->_outline_on = 1;
  392. $this->_merged_ranges = array();
  393. $this->_input_encoding = '';
  394. $this->_dv = array();
  395. $this->_initialize();
  396. }
  397. /**
  398. * Open a tmp file to store the majority of the Worksheet data. If this fails,
  399. * for example due to write permissions, store the data in memory. This can be
  400. * slow for large files.
  401. *
  402. * @access private
  403. */
  404. function _initialize()
  405. {
  406. // Open tmp file for storing Worksheet data
  407. $fh = tmpfile();
  408. if ($fh) {
  409. // Store filehandle
  410. $this->_filehandle = $fh;
  411. } else {
  412. // If tmpfile() fails store data in memory
  413. $this->_using_tmpfile = false;
  414. }
  415. }
  416. /**
  417. * Add data to the beginning of the workbook (note the reverse order)
  418. * and to the end of the workbook.
  419. *
  420. * @access public
  421. * @see Spreadsheet_Excel_Writer_Workbook::storeWorkbook()
  422. * @param array $sheetnames The array of sheetnames from the Workbook this
  423. * worksheet belongs to
  424. */
  425. function close($sheetnames)
  426. {
  427. $num_sheets = count($sheetnames);
  428. /***********************************************
  429. * Prepend in reverse order!!
  430. */
  431. // Prepend the sheet dimensions
  432. $this->_storeDimensions();
  433. // Prepend the sheet password
  434. $this->_storePassword();
  435. // Prepend the sheet protection
  436. $this->_storeProtect();
  437. // Prepend the page setup
  438. $this->_storeSetup();
  439. /* FIXME: margins are actually appended */
  440. // Prepend the bottom margin
  441. $this->_storeMarginBottom();
  442. // Prepend the top margin
  443. $this->_storeMarginTop();
  444. // Prepend the right margin
  445. $this->_storeMarginRight();
  446. // Prepend the left margin
  447. $this->_storeMarginLeft();
  448. // Prepend the page vertical centering
  449. $this->_storeVcenter();
  450. // Prepend the page horizontal centering
  451. $this->_storeHcenter();
  452. // Prepend the page footer
  453. $this->_storeFooter();
  454. // Prepend the page header
  455. $this->_storeHeader();
  456. // Prepend the vertical page breaks
  457. $this->_storeVbreak();
  458. // Prepend the horizontal page breaks
  459. $this->_storeHbreak();
  460. // Prepend WSBOOL
  461. $this->_storeWsbool();
  462. // Prepend GRIDSET
  463. $this->_storeGridset();
  464. // Prepend GUTS
  465. if ($this->_BIFF_version == 0x0500) {
  466. $this->_storeGuts();
  467. }
  468. // Prepend PRINTGRIDLINES
  469. $this->_storePrintGridlines();
  470. // Prepend PRINTHEADERS
  471. $this->_storePrintHeaders();
  472. // Prepend EXTERNSHEET references
  473. if ($this->_BIFF_version == 0x0500) {
  474. for ($i = $num_sheets; $i > 0; $i--) {
  475. $sheetname = $sheetnames[$i-1];
  476. $this->_storeExternsheet($sheetname);
  477. }
  478. }
  479. // Prepend the EXTERNCOUNT of external references.
  480. if ($this->_BIFF_version == 0x0500) {
  481. $this->_storeExterncount($num_sheets);
  482. }
  483. // Prepend the COLINFO records if they exist
  484. if (!empty($this->_colinfo)) {
  485. $colcount = count($this->_colinfo);
  486. for ($i = 0; $i < $colcount; $i++) {
  487. $this->_storeColinfo($this->_colinfo[$i]);
  488. }
  489. $this->_storeDefcol();
  490. }
  491. // Prepend the BOF record
  492. $this->_storeBof(0x0010);
  493. /*
  494. * End of prepend. Read upwards from here.
  495. ***********************************************/
  496. // Append
  497. $this->_storeWindow2();
  498. $this->_storeZoom();
  499. if (!empty($this->_panes)) {
  500. $this->_storePanes($this->_panes);
  501. }
  502. $this->_storeSelection($this->_selection);
  503. $this->_storeMergedCells();
  504. /* TODO: add data validity */
  505. /*if ($this->_BIFF_version == 0x0600) {
  506. $this->_storeDataValidity();
  507. }*/
  508. $this->_storeEof();
  509. }
  510. /**
  511. * Retrieve the worksheet name.
  512. * This is usefull when creating worksheets without a name.
  513. *
  514. * @access public
  515. * @return string The worksheet's name
  516. */
  517. function getName()
  518. {
  519. return $this->name;
  520. }
  521. /**
  522. * Retrieves data from memory in one chunk, or from disk in $buffer
  523. * sized chunks.
  524. *
  525. * @return string The data
  526. */
  527. function getData()
  528. {
  529. $buffer = 4096;
  530. // Return data stored in memory
  531. if (isset($this->_data)) {
  532. $tmp = $this->_data;
  533. unset($this->_data);
  534. $fh = $this->_filehandle;
  535. if ($this->_using_tmpfile) {
  536. fseek($fh, 0);
  537. }
  538. return $tmp;
  539. }
  540. // Return data stored on disk
  541. if ($this->_using_tmpfile) {
  542. if ($tmp = fread($this->_filehandle, $buffer)) {
  543. return $tmp;
  544. }
  545. }
  546. // No data to return
  547. return '';
  548. }
  549. /**
  550. * Sets a merged cell range
  551. *
  552. * @access public
  553. * @param integer $first_row First row of the area to merge
  554. * @param integer $first_col First column of the area to merge
  555. * @param integer $last_row Last row of the area to merge
  556. * @param integer $last_col Last column of the area to merge
  557. */
  558. function setMerge($first_row, $first_col, $last_row, $last_col)
  559. {
  560. if (($last_row < $first_row) || ($last_col < $first_col)) {
  561. return;
  562. }
  563. // don't check rowmin, rowmax, etc... because we don't know when this
  564. // is going to be called
  565. $this->_merged_ranges[] = array($first_row, $first_col, $last_row, $last_col);
  566. }
  567. /**
  568. * Set this worksheet as a selected worksheet,
  569. * i.e. the worksheet has its tab highlighted.
  570. *
  571. * @access public
  572. */
  573. function select()
  574. {
  575. $this->selected = 1;
  576. }
  577. /**
  578. * Set this worksheet as the active worksheet,
  579. * i.e. the worksheet that is displayed when the workbook is opened.
  580. * Also set it as selected.
  581. *
  582. * @access public
  583. */
  584. function activate()
  585. {
  586. $this->selected = 1;
  587. $this->activesheet = $this->index;
  588. }
  589. /**
  590. * Set this worksheet as the first visible sheet.
  591. * This is necessary when there are a large number of worksheets and the
  592. * activated worksheet is not visible on the screen.
  593. *
  594. * @access public
  595. */
  596. function setFirstSheet()
  597. {
  598. $this->firstsheet = $this->index;
  599. }
  600. /**
  601. * Set the worksheet protection flag
  602. * to prevent accidental modification and to
  603. * hide formulas if the locked and hidden format properties have been set.
  604. *
  605. * @access public
  606. * @param string $password The password to use for protecting the sheet.
  607. */
  608. function protect($password)
  609. {
  610. $this->_protect = 1;
  611. $this->_password = $this->_encodePassword($password);
  612. }
  613. /**
  614. * Set the width of a single column or a range of columns.
  615. *
  616. * @access public
  617. * @param integer $firstcol first column on the range
  618. * @param integer $lastcol last column on the range
  619. * @param integer $width width to set
  620. * @param mixed $format The optional XF format to apply to the columns
  621. * @param integer $hidden The optional hidden atribute
  622. * @param integer $level The optional outline level
  623. */
  624. function setColumn($firstcol, $lastcol, $width, $format = 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. $this->raiseError("Zoom factor $scale outside range: 10 <= zoom <= 400");
  961. $scale = 100;
  962. }
  963. $this->_zoom = floor($scale);
  964. }
  965. /**
  966. * Set the scale factor for the printed page.
  967. * It turns off the "fit to page" option
  968. *
  969. * @access public
  970. * @param integer $scale The optional scale factor. Defaults to 100
  971. */
  972. function setPrintScale($scale = 100)
  973. {
  974. // Confine the scale to Excel's range
  975. if ($scale < 10 || $scale > 400) {
  976. $this->raiseError("Print scale $scale outside range: 10 <= zoom <= 400");
  977. $scale = 100;
  978. }
  979. // Turn off "fit to page" option
  980. $this->_fit_page = 0;
  981. $this->_print_scale = floor($scale);
  982. }
  983. /**
  984. * Map to the appropriate write method acording to the token recieved.
  985. *
  986. * @access public
  987. * @param integer $row The row of the cell we are writing to
  988. * @param integer $col The column of the cell we are writing to
  989. * @param mixed $token What we are writing
  990. * @param mixed $format The optional format to apply to the cell
  991. */
  992. function write($row, $col, $token, $format = 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 (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
  999. // Match number
  1000. return $this->writeNumber($row, $col, $token, $format);
  1001. } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
  1002. // Match http or ftp URL
  1003. return $this->writeUrl($row, $col, $token, '', $format);
  1004. } elseif (preg_match("/^mailto:/", $token)) {
  1005. // Match mailto:
  1006. return $this->writeUrl($row, $col, $token, '', $format);
  1007. } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
  1008. // Match internal or external sheet link
  1009. return $this->writeUrl($row, $col, $token, '', $format);
  1010. } elseif (preg_match("/^=/", $token)) {
  1011. // Match formula
  1012. return $this->writeFormula($row, $col, $token, $format);
  1013. } elseif (preg_match("/^@/", $token)) {
  1014. // Match formula
  1015. return $this->writeFormula($row, $col, $token, $format);
  1016. } elseif ($token == '') {
  1017. // Match blank
  1018. return $this->writeBlank($row, $col, $format);
  1019. } else {
  1020. // Default: match string
  1021. return $this->writeString($row, $col, $token, $format);
  1022. }
  1023. }
  1024. /**
  1025. * Write an array of values as a row
  1026. *
  1027. * @access public
  1028. * @param integer $row The row we are writing to
  1029. * @param integer $col The first col (leftmost col) we are writing to
  1030. * @param array $val The array of values to write
  1031. * @param mixed $format The optional format to apply to the cell
  1032. * @return mixed PEAR_Error on failure
  1033. */
  1034. function writeRow($row, $col, $val, $format = null)
  1035. {
  1036. $retval = '';
  1037. if (is_array($val)) {
  1038. foreach ($val as $v) {
  1039. if (is_array($v)) {
  1040. $this->writeCol($row, $col, $v, $format);
  1041. } else {
  1042. $this->write($row, $col, $v, $format);
  1043. }
  1044. $col++;
  1045. }
  1046. } else {
  1047. $retval = new PEAR_Error('$val needs to be an array');
  1048. }
  1049. return($retval);
  1050. }
  1051. /**
  1052. * Write an array of values as a column
  1053. *
  1054. * @access public
  1055. * @param integer $row The first row (uppermost row) we are writing to
  1056. * @param integer $col The col we are writing to
  1057. * @param array $val The array of values to write
  1058. * @param mixed $format The optional format to apply to the cell
  1059. * @return mixed PEAR_Error on failure
  1060. */
  1061. function writeCol($row, $col, $val, $format = null)
  1062. {
  1063. $retval = '';
  1064. if (is_array($val)) {
  1065. foreach ($val as $v) {
  1066. $this->write($row, $col, $v, $format);
  1067. $row++;
  1068. }
  1069. } else {
  1070. $retval = new PEAR_Error('$val needs to be an array');
  1071. }
  1072. return($retval);
  1073. }
  1074. /**
  1075. * Returns an index to the XF record in the workbook
  1076. *
  1077. * @access private
  1078. * @param mixed &$format The optional XF format
  1079. * @return integer The XF record index
  1080. */
  1081. function _XF(&$format)
  1082. {
  1083. if ($format) {
  1084. return($format->getXfIndex());
  1085. } else {
  1086. return(0x0F);
  1087. }
  1088. }
  1089. /******************************************************************************
  1090. *******************************************************************************
  1091. *
  1092. * Internal methods
  1093. */
  1094. /**
  1095. * Store Worksheet data in memory using the parent's class append() or to a
  1096. * temporary file, the default.
  1097. *
  1098. * @access private
  1099. * @param string $data The binary data to append
  1100. */
  1101. function _append($data)
  1102. {
  1103. if ($this->_using_tmpfile) {
  1104. // Add CONTINUE records if necessary
  1105. if (strlen($data) > $this->_limit) {
  1106. $data = $this->_addContinue($data);
  1107. }
  1108. fwrite($this->_filehandle, $data);
  1109. $this->_datasize += strlen($data);
  1110. } else {
  1111. parent::_append($data);
  1112. }
  1113. }
  1114. /**
  1115. * Substitute an Excel cell reference in A1 notation for zero based row and
  1116. * column values in an argument list.
  1117. *
  1118. * Ex: ("A4", "Hello") is converted to (3, 0, "Hello").
  1119. *
  1120. * @access private
  1121. * @param string $cell The cell reference. Or range of cells.
  1122. * @return array
  1123. */
  1124. function _substituteCellref($cell)
  1125. {
  1126. $cell = strtoupper($cell);
  1127. // Convert a column range: 'A:A' or 'B:G'
  1128. if (preg_match("/([A-I]?[A-Z]):([A-I]?[A-Z])/", $cell, $match)) {
  1129. list($no_use, $col1) = $this->_cellToRowcol($match[1] .'1'); // Add a dummy row
  1130. list($no_use, $col2) = $this->_cellToRowcol($match[2] .'1'); // Add a dummy row
  1131. return(array($col1, $col2));
  1132. }
  1133. // Convert a cell range: 'A1:B7'
  1134. if (preg_match("/\$?([A-I]?[A-Z]\$?\d+):\$?([A-I]?[A-Z]\$?\d+)/", $cell, $match)) {
  1135. list($row1, $col1) = $this->_cellToRowcol($match[1]);
  1136. list($row2, $col2) = $this->_cellToRowcol($match[2]);
  1137. return(array($row1, $col1, $row2, $col2));
  1138. }
  1139. // Convert a cell reference: 'A1' or 'AD2000'
  1140. if (preg_match("/\$?([A-I]?[A-Z]\$?\d+)/", $cell)) {
  1141. list($row1, $col1) = $this->_cellToRowcol($match[1]);
  1142. return(array($row1, $col1));
  1143. }
  1144. // TODO use real error codes
  1145. $this->raiseError("Unknown cell reference $cell", 0, PEAR_ERROR_DIE);
  1146. }
  1147. /**
  1148. * Convert an Excel cell reference in A1 notation to a zero based row and column
  1149. * reference; converts C1 to (0, 2).
  1150. *
  1151. * @access private
  1152. * @param string $cell The cell reference.
  1153. * @return array containing (row, column)
  1154. */
  1155. function _cellToRowcol($cell)
  1156. {
  1157. preg_match("/\$?([A-I]?[A-Z])\$?(\d+)/",$cell,$match);
  1158. $col = $match[1];
  1159. $row = $match[2];
  1160. // Convert base26 column string to number
  1161. $chars = split('', $col);
  1162. $expn = 0;
  1163. $col = 0;
  1164. while ($chars) {
  1165. $char = array_pop($chars); // LS char first
  1166. $col += (ord($char) -ord('A') +1) * pow(26,$expn);
  1167. $expn++;
  1168. }
  1169. // Convert 1-index to zero-index
  1170. $row--;
  1171. $col--;
  1172. return(array($row, $col));
  1173. }
  1174. /**
  1175. * Based on the algorithm provided by Daniel Rentz of OpenOffice.
  1176. *
  1177. * @access private
  1178. * @param string $plaintext The password to be encoded in plaintext.
  1179. * @return string The encoded password
  1180. */
  1181. function _encodePassword($plaintext)
  1182. {
  1183. $password = 0x0000;
  1184. $i = 1; // char position
  1185. // split the plain text password in its component characters
  1186. $chars = preg_split('//', $plaintext, -1, PREG_SPLIT_NO_EMPTY);
  1187. foreach ($chars as $char) {
  1188. $value = ord($char) << $i; // shifted ASCII value
  1189. $rotated_bits = $value >> 15; // rotated bits beyond bit 15
  1190. $value &= 0x7fff; // first 15 bits
  1191. $password ^= ($value | $rotated_bits);
  1192. $i++;
  1193. }
  1194. $password ^= strlen($plaintext);
  1195. $password ^= 0xCE4B;
  1196. return($password);
  1197. }
  1198. /**
  1199. * This method sets the properties for outlining and grouping. The defaults
  1200. * correspond to Excel's defaults.
  1201. *
  1202. * @param bool $visible
  1203. * @param bool $symbols_below
  1204. * @param bool $symbols_right
  1205. * @param bool $auto_style
  1206. */
  1207. function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false)
  1208. {
  1209. $this->_outline_on = $visible;
  1210. $this->_outline_below = $symbols_below;
  1211. $this->_outline_right = $symbols_right;
  1212. $this->_outline_style = $auto_style;
  1213. // Ensure this is a boolean vale for Window2
  1214. if ($this->_outline_on) {
  1215. $this->_outline_on = 1;
  1216. }
  1217. }
  1218. /******************************************************************************
  1219. *******************************************************************************
  1220. *
  1221. * BIFF RECORDS
  1222. */
  1223. /**
  1224. * Write a double to the specified row and column (zero indexed).
  1225. * An integer can be written as a double. Excel will display an
  1226. * integer. $format is optional.
  1227. *
  1228. * Returns 0 : normal termination
  1229. * -2 : row or column out of range
  1230. *
  1231. * @access public
  1232. * @param integer $row Zero indexed row
  1233. * @param integer $col Zero indexed column
  1234. * @param float $num The number to write
  1235. * @param mixed $format The optional XF format
  1236. * @return integer
  1237. */
  1238. function writeNumber($row, $col, $num, $format = null)
  1239. {
  1240. $record = 0x0203; // Record identifier
  1241. $length = 0x000E; // Number of bytes to follow
  1242. $xf = $this->_XF($format); // The cell format
  1243. // Check that row and col are valid and store max and min values
  1244. if ($row >= $this->_xls_rowmax) {
  1245. return(-2);
  1246. }
  1247. if ($col >= $this->_xls_colmax) {
  1248. return(-2);
  1249. }
  1250. if ($row < $this->_dim_rowmin) {
  1251. $this->_dim_rowmin = $row;
  1252. }
  1253. if ($row > $this->_dim_rowmax) {
  1254. $this->_dim_rowmax = $row;
  1255. }
  1256. if ($col < $this->_dim_colmin) {
  1257. $this->_dim_colmin = $col;
  1258. }
  1259. if ($col > $this->_dim_colmax) {
  1260. $this->_dim_colmax = $col;
  1261. }
  1262. $header = pack("vv", $record, $length);
  1263. $data = pack("vvv", $row, $col, $xf);
  1264. $xl_double = pack("d", $num);
  1265. if ($this->_byte_order) { // if it's Big Endian
  1266. $xl_double = strrev($xl_double);
  1267. }
  1268. $this->_append($header.$data.$xl_double);
  1269. return(0);
  1270. }
  1271. /**
  1272. * Write a string to the specified row and column (zero indexed).
  1273. * NOTE: there is an Excel 5 defined limit of 255 characters.
  1274. * $format is optional.
  1275. * Returns 0 : normal termination
  1276. * -2 : row or column out of range
  1277. * -3 : long string truncated to 255 chars
  1278. *
  1279. * @access public
  1280. * @param integer $row Zero indexed row
  1281. * @param integer $col Zero indexed column
  1282. * @param string $str The string to write
  1283. * @param mixed $format The XF format for the cell
  1284. * @return integer
  1285. */
  1286. function writeString($row, $col, $str, $format = null)
  1287. {
  1288. if ($this->_BIFF_version == 0x0600) {
  1289. return $this->writeStringBIFF8($row, $col, $str, $format);
  1290. }
  1291. $strlen = strlen($str);
  1292. $record = 0x0204; // Record identifier
  1293. $length = 0x0008 + $strlen; // Bytes to follow
  1294. $xf = $this->_XF($format); // The cell format
  1295. $str_error = 0;
  1296. // Check that row and col are valid and store max and min values
  1297. if ($row >= $this->_xls_rowmax) {
  1298. return(-2);
  1299. }
  1300. if ($col >= $this->_xls_colmax) {
  1301. return(-2);
  1302. }
  1303. if ($row < $this->_dim_rowmin) {
  1304. $this->_dim_rowmin = $row;
  1305. }
  1306. if ($row > $this->_dim_rowmax) {
  1307. $this->_dim_rowmax = $row;
  1308. }
  1309. if ($col < $this->_dim_colmin) {
  1310. $this->_dim_colmin = $col;
  1311. }
  1312. if ($col > $this->_dim_colmax) {
  1313. $this->_dim_colmax = $col;
  1314. }
  1315. if ($strlen > $this->_xls_strmax) { // LABEL must be < 255 chars
  1316. $str = substr($str, 0, $this->_xls_strmax);
  1317. $length = 0x0008 + $this->_xls_strmax;
  1318. $strlen = $this->_xls_strmax;
  1319. $str_error = -3;
  1320. }
  1321. $header = pack("vv", $record, $length);
  1322. $data = pack("vvvv", $row, $col, $xf, $strlen);
  1323. $this->_append($header . $data . $str);
  1324. return($str_error);
  1325. }
  1326. /**
  1327. * Sets Input Encoding for writing strings
  1328. *
  1329. * @access public
  1330. * @param string $encoding The encoding. Ex: 'UTF-16LE', 'utf-8', 'ISO-859-7'
  1331. */
  1332. function setInputEncoding($encoding)
  1333. {
  1334. if ($encoding != 'UTF-16LE' && !function_exists('iconv')) {
  1335. $this->raiseError("Using an input encoding other than UTF-16LE requires PHP support for iconv");
  1336. }
  1337. $this->_input_encoding = $encoding;
  1338. }
  1339. /**
  1340. * Write a string to the specified row and column (zero indexed).
  1341. * This is the BIFF8 version (no 255 chars limit).
  1342. * $format is optional.
  1343. * Returns 0 : normal termination
  1344. * -2 : row or column out of range
  1345. * -3 : long string truncated to 255 chars
  1346. *
  1347. * @access public
  1348. * @param integer $row Zero indexed row
  1349. * @param integer $col Zero indexed column
  1350. * @param string $str The string to write
  1351. * @param mixed $format The XF format for the cell
  1352. * @return integer
  1353. */
  1354. function writeStringBIFF8($row, $col, $str, $format = null)
  1355. {
  1356. if ($this->_input_encoding == 'UTF-16LE')
  1357. {
  1358. $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1359. $encoding = 0x1;
  1360. }
  1361. elseif ($this->_input_encoding != '')
  1362. {
  1363. $str = iconv($this->_input_encoding, 'UTF-16LE', $str);
  1364. $strlen = function_exists('mb_strlen') ? mb_strlen($str, 'UTF-16LE') : (strlen($str) / 2);
  1365. $encoding = 0x1;
  1366. }
  1367. else
  1368. {
  1369. $strlen = strlen($str);
  1370. $encoding = 0x0;
  1371. }
  1372. $record = 0x00FD; // Record identifier
  1373. $length = 0x000A; // Bytes to follow
  1374. $xf = $this->_XF($format); // The cell format
  1375. $str_error = 0;
  1376. // Check that row and col are valid and store max and min values
  1377. if ($this->_checkRowCol($row, $col) == false) {
  1378. return -2;
  1379. }
  1380. $str = pack('vC', $strlen, $encoding).$str;
  1381. /* check if string is already present */
  1382. if (!isset($this->_str_table[$str])) {
  1383. $this->_str_table[$str] = $this->_str_unique++;
  1384. }
  1385. $this->_str_total++;
  1386. $header = pack('vv', $record, $length);
  1387. $data = pack('vvvV', $row, $col, $xf, $this->_str_table[$str]);
  1388. $this->_append($header.$data);
  1389. return $str_error;
  1390. }
  1391. /**
  1392. * Check row and col before writing to a cell, and update the sheet's
  1393. * dimensions accordingly
  1394. *
  1395. * @access private
  1396. * @param integer $row Zero indexed row
  1397. * @param integer $col Zero indexed column
  1398. * @return boolean true for success, false if row and/or col are grester
  1399. * then maximums allowed.
  1400. */
  1401. function _checkRowCol($row, $col)
  1402. {
  1403. if ($row >= $this->_xls_rowmax) {
  1404. return false;
  1405. }
  1406. if ($col >= $this->_xls_colmax) {
  1407. return false;
  1408. }
  1409. if ($row < $this->_dim_rowmin) {
  1410. $this->_dim_rowmin = $row;
  1411. }
  1412. if ($row > $this->_dim_rowmax) {
  1413. $this->_dim_rowmax = $row;
  1414. }
  1415. if ($col < $this->_dim_colmin) {
  1416. $this->_dim_colmin = $col;
  1417. }
  1418. if ($col > $this->_dim_colmax) {
  1419. $this->_dim_colmax = $col;
  1420. }
  1421. return true;
  1422. }
  1423. /**
  1424. * Writes a note associated with the cell given by the row and column.
  1425. * NOTE records don't have a length limit.
  1426. *
  1427. * @access public
  1428. * @param integer $row Zero indexed row
  1429. * @param integer $col Zero indexed column
  1430. * @param string $note The note to write
  1431. */
  1432. function writeNote($row, $col, $note)
  1433. {
  1434. $note_length = strlen($note);
  1435. $record = 0x001C; // Record identifier
  1436. $max_length = 2048; // Maximun length for a NOTE record
  1437. //$length = 0x0006 + $note_length; // Bytes to follow
  1438. // Check that row and col are valid and store max and min values
  1439. if ($row >= $this->_xls_rowmax) {
  1440. return(-2);
  1441. }
  1442. if ($col >= $this->_xls_colmax) {
  1443. return(-2);
  1444. }
  1445. if ($row < $this->_dim_rowmin) {
  1446. $this->_dim_rowmin = $row;
  1447. }
  1448. if ($row > $this->_dim_rowmax) {
  1449. $this->_dim_rowmax = $row;
  1450. }
  1451. if ($col < $this->_dim_colmin) {
  1452. $this->_dim_colmin = $col;
  1453. }
  1454. if ($col > $this->_dim_colmax) {
  1455. $this->_dim_colmax = $col;
  1456. }
  1457. // Length for this record is no more than 2048 + 6
  1458. $length = 0x0006 + min($note_length, 2048);
  1459. $header = pack("vv", $record, $length);
  1460. $data = pack("vvv", $row, $col, $note_length);
  1461. $this->_append($header . $data . substr($note, 0, 2048));
  1462. for ($i = $max_length; $i < $note_length; $i += $max_length) {
  1463. $chunk = substr($note, $i, $max_length);
  1464. $length = 0x0006 + strlen($chunk);
  1465. $header = pack("vv", $record, $length);
  1466. $data = pack("vvv", -1, 0, strlen($chunk));
  1467. $this->_append($header.$data.$chunk);
  1468. }
  1469. return(0);
  1470. }
  1471. /**
  1472. * Write a blank cell to the specified row and column (zero indexed).
  1473. * A blank cell is used to specify formatting without adding a string
  1474. * or a number.
  1475. *
  1476. * A blank cell without a format serves no purpose. Therefore, we don't write
  1477. * a BLANK record unless a format is specified.
  1478. *
  1479. * Returns 0 : normal termination (including no format)
  1480. * -1 : insufficient number of arguments
  1481. * -2 : row or column out of range
  1482. *
  1483. * @access public
  1484. * @param integer $row Zero indexed row
  1485. * @param integer $col Zero indexed column
  1486. * @param mixed $format The XF format
  1487. */
  1488. function writeBlank($row, $col, $format)
  1489. {
  1490. // Don't write a blank cell unless it has a format
  1491. if (!$format) {
  1492. return(0);
  1493. }
  1494. $record = 0x0201; // Record identifier
  1495. $length = 0x0006; // Number of bytes to follow
  1496. $xf = $this->_XF($format); // The cell format
  1497. // Check that row and col are valid and store max and min values
  1498. if ($row >= $this->_xls_rowmax) {
  1499. return(-2);
  1500. }
  1501. if ($col >= $this->_xls_colmax) {
  1502. return(-2);
  1503. }
  1504. if ($row < $this->_dim_rowmin) {
  1505. $this->_dim_rowmin = $row;
  1506. }
  1507. if ($row > $this->_dim_rowmax) {
  1508. $this->_dim_rowmax = $row;
  1509. }
  1510. if ($col < $this->_dim_colmin) {
  1511. $this->_dim_colmin = $col;
  1512. }
  1513. if ($col > $this->_dim_colmax) {
  1514. $this->_dim_colmax = $col;
  1515. }
  1516. $header = pack("vv", $record, $length);
  1517. $data = pack("vvv", $row, $col, $xf);
  1518. $this->_append($header . $data);
  1519. return 0;
  1520. }
  1521. /**
  1522. * Write a formula to the specified row and column (zero indexed).
  1523. * The textual representation of the formula is passed to the parser in
  1524. * Parser.php which returns a packed binary string.
  1525. *
  1526. * Returns 0 : normal termination
  1527. * -1 : formula errors (bad formula)
  1528. * -2 : row or column out of range
  1529. *
  1530. * @access public
  1531. * @param integer $row Zero indexed row
  1532. * @param integer $col Zero indexed column
  1533. * @param string $formula The formula text string
  1534. * @param mixed $format The optional XF format
  1535. * @return integer
  1536. */
  1537. function writeFormula($row, $col, $formula, $format = null)
  1538. {
  1539. $record = 0x0006; // Record identifier
  1540. // Excel normally stores the last calculated value of the formula in $num.
  1541. // Clearly we are not in a position to calculate this a priori. Instead
  1542. // we set $num to zero and set the option flags in $grbit to ensure
  1543. // automatic calculation of the formula when the file is opened.
  1544. //
  1545. $xf = $this->_XF($format); // The cell format
  1546. $num = 0x00; // Current value of formula
  1547. $grbit = 0x03; // Option flags
  1548. $unknown = 0x0000; // Must be ze…

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