Spreadsheet_Excel_Writer
[ class tree: Spreadsheet_Excel_Writer ] [ index: Spreadsheet_Excel_Writer ] [ all elements ]

Source for file Worksheet.php

Documentation is available at Worksheet.php

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

Documentation generated on Mon, 11 Mar 2019 15:47:17 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.