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

Source for file BIFFwriter.php

Documentation is available at BIFFwriter.php

  1. <?php
  2. /*
  3. *  Module written/ported by Xavier Noguer <xnoguer@php.net>
  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@php.net
  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('PEAR.php');
  36.  
  37. /**
  38. * Class for writing Excel BIFF records.
  39. * From "MICROSOFT EXCEL BINARY FILE FORMAT" by Mark O'Brien (Microsoft Corporation):
  40. *
  41. * BIFF (BInary File Format) is the file format in which Excel documents are
  42. * saved on disk.  A BIFF file is a complete description of an Excel document.
  43. * BIFF files consist of sequences of variable-length records. There are many
  44. * different types of BIFF records.  For example, one record type describes a
  45. * formula entered into a cell; one describes the size and location of a
  46. * window into a document; another describes a picture format.
  47. *
  48. @author   Xavier Noguer <xnoguer@php.net>
  49. @category FileFormats
  50. @package  Spreadsheet_Excel_Writer
  51. */
  52.  
  53. {
  54.     /**
  55.     * The BIFF/Excel version (5).
  56.     * @var integer 
  57.     */
  58.     var $_BIFF_version = 0x0500;
  59.  
  60.     /**
  61.     * The byte order of this architecture. 0 => little endian, 1 => big endian
  62.     * @var integer 
  63.     */
  64.     var $_byte_order;
  65.  
  66.     /**
  67.     * The string containing the data of the BIFF stream
  68.     * @var string 
  69.     */
  70.     var $_data;
  71.  
  72.     /**
  73.     * The size of the data in bytes. Should be the same as strlen($this->_data)
  74.     * @var integer 
  75.     */
  76.     var $_datasize;
  77.  
  78.     /**
  79.     * The maximun length for a BIFF record. See _addContinue()
  80.     * @var integer 
  81.     * @see _addContinue()
  82.     */
  83.     var $_limit;
  84.  
  85.     /**
  86.     * Constructor
  87.     *
  88.     * @access public
  89.     */
  90.     {
  91.         $this->_byte_order '';
  92.         $this->_data       '';
  93.         $this->_datasize   = 0;
  94.         $this->_limit      = 2080;   
  95.         // Set the byte order
  96.         $this->_setByteOrder();
  97.     }
  98.  
  99.     /**
  100.     * Determine the byte order and store it as class data to avoid
  101.     * recalculating it for each call to new().
  102.     *
  103.     * @access private
  104.     */
  105.     function _setByteOrder()
  106.     {
  107.         // Check if "pack" gives the required IEEE 64bit float
  108.         $teststr pack("d"1.2345);
  109.         $number  pack("C8"0x8D0x970x6E0x120x830xC00xF30x3F);
  110.         if ($number == $teststr{
  111.             $byte_order = 0;    // Little Endian
  112.         }
  113.         elseif ($number == strrev($teststr)){
  114.             $byte_order = 1;    // Big Endian
  115.         }
  116.         else {
  117.             // Give up. I'll fix this in a later version.
  118.             return $this->raiseError("Required floating point format ".
  119.                                      "not supported on this platform.");
  120.         }
  121.         $this->_byte_order $byte_order;
  122.     }
  123.  
  124.     /**
  125.     * General storage function
  126.     *
  127.     * @param string $data binary data to prepend
  128.     * @access private
  129.     */
  130.     function _prepend($data)
  131.     {
  132.         if (strlen($data$this->_limit{
  133.             $data $this->_addContinue($data);
  134.         }
  135.         $this->_data      $data.$this->_data;
  136.         $this->_datasize += strlen($data);
  137.     }
  138.  
  139.     /**
  140.     * General storage function
  141.     *
  142.     * @param string $data binary data to append
  143.     * @access private
  144.     */
  145.     function _append($data)
  146.     {
  147.         if (strlen($data$this->_limit{
  148.             $data $this->_addContinue($data);
  149.         }
  150.         $this->_data      $this->_data.$data;
  151.         $this->_datasize += strlen($data);
  152.     }
  153.  
  154.     /**
  155.     * Writes Excel BOF record to indicate the beginning of a stream or
  156.     * sub-stream in the BIFF file.
  157.     *
  158.     * @param  integer $type Type of BIFF file to write: 0x0005 Workbook,
  159.     *                        0x0010 Worksheet.
  160.     * @access private
  161.     */
  162.     function _storeBof($type)
  163.     {
  164.         $record  = 0x0809;        // Record identifier
  165.  
  166.         // According to the SDK $build and $year should be set to zero.
  167.         // However, this throws a warning in Excel 5. So, use magic numbers.
  168.         if ($this->_BIFF_version == 0x0500{
  169.             $length  = 0x0008;
  170.             $unknown '';
  171.             $build   = 0x096C;
  172.             $year    = 0x07C9;
  173.         }
  174.         elseif ($this->_BIFF_version == 0x0600{
  175.             $length  = 0x0010;
  176.             $unknown pack("VV"0x000000410x00000006)//unknown last 8 bytes for BIFF8
  177.             $build   = 0x0DBB;
  178.             $year    = 0x07CC;
  179.         }
  180.         $version $this->_BIFF_version;
  181.    
  182.         $header  pack("vv",   $record$length);
  183.         $data    pack("vvvv"$version$type$build$year);
  184.         $this->_prepend($header.$data.$unknown);
  185.     }
  186.  
  187.     /**
  188.     * Writes Excel EOF record to indicate the end of a BIFF stream.
  189.     *
  190.     * @access private
  191.     */
  192.     function _storeEof(
  193.     {
  194.         $record    = 0x000A;   // Record identifier
  195.         $length    = 0x0000;   // Number of bytes to follow
  196.         $header    pack("vv"$record$length);
  197.         $this->_append($header);
  198.     }
  199.  
  200.     /**
  201.     * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
  202.     * Excel 97 the limit is 8228 bytes. Records that are longer than these limits
  203.     * must be split up into CONTINUE blocks.
  204.     *
  205.     * This function takes a long BIFF record and inserts CONTINUE records as
  206.     * necessary.
  207.     *
  208.     * @param  string  $data The original binary data to be written
  209.     * @return string        A very convenient string of continue blocks
  210.     * @access private
  211.     */
  212.     function _addContinue($data)
  213.     {
  214.         $limit      $this->_limit;
  215.         $record     = 0x003C;         // Record identifier
  216.  
  217.         // The first 2080/8224 bytes remain intact. However, we have to change
  218.         // the length field of the record.
  219.         $tmp substr($data02).pack("v"$limit-4).substr($data4$limit - 4);
  220.         
  221.         $header pack("vv"$record$limit);  // Headers for continue records
  222.  
  223.         // Retrieve chunks of 2080/8224 bytes +4 for the header.
  224.         for($i $limit$i strlen($data$limit$i += $limit)
  225.         {
  226.             $tmp .= $header;
  227.             $tmp .= substr($data$i$limit);
  228.         }
  229.  
  230.         // Retrieve the last chunk of data
  231.         $header  pack("vv"$recordstrlen($data$i);
  232.         $tmp    .= $header;
  233.         $tmp    .= substr($data,$i,strlen($data$i);
  234.  
  235.         return $tmp;
  236.     }
  237. }
  238. ?>

Documentation generated on Mon, 11 Mar 2019 13:51:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.