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

Source for file BIFFwriter.php

Documentation is available at BIFFwriter.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Writer_Excel5
  23.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28. // Original file header of PEAR::Spreadsheet_Excel_Writer_BIFFwriter (used as the base for this class):
  29. // -----------------------------------------------------------------------------------------
  30. // *  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  31. // *
  32. // *  The majority of this is _NOT_ my code.  I simply ported it from the
  33. // *  PERL Spreadsheet::WriteExcel module.
  34. // *
  35. // *  The author of the Spreadsheet::WriteExcel module is John McNamara
  36. // *  <jmcnamara@cpan.org>
  37. // *
  38. // *  I _DO_ maintain this code, and John McNamara has nothing to do with the
  39. // *  porting of this code to PHP.  Any questions directly related to this
  40. // *  class library should be directed to me.
  41. // *
  42. // *  License Information:
  43. // *
  44. // *    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
  45. // *    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
  46. // *
  47. // *    This library is free software; you can redistribute it and/or
  48. // *    modify it under the terms of the GNU Lesser General Public
  49. // *    License as published by the Free Software Foundation; either
  50. // *    version 2.1 of the License, or (at your option) any later version.
  51. // *
  52. // *    This library is distributed in the hope that it will be useful,
  53. // *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  54. // *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  55. // *    Lesser General Public License for more details.
  56. // *
  57. // *    You should have received a copy of the GNU Lesser General Public
  58. // *    License along with this library; if not, write to the Free Software
  59. // *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  60. // */
  61.  
  62.  
  63. /**
  64.  * PHPExcel_Writer_Excel5_BIFFwriter
  65.  *
  66.  * @category   PHPExcel
  67.  * @package    PHPExcel_Writer_Excel5
  68.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  69.  */
  70. {
  71.     /**
  72.      * The BIFF/Excel version (5).
  73.      * @var integer 
  74.      */
  75.     public $_BIFF_version = 0x0500;
  76.  
  77.     /**
  78.      * The byte order of this architecture. 0 => little endian, 1 => big endian
  79.      * @var integer 
  80.      */
  81.     private static $_byte_order;
  82.  
  83.     /**
  84.      * The string containing the data of the BIFF stream
  85.      * @var string 
  86.      */
  87.     public $_data;
  88.  
  89.     /**
  90.      * The size of the data in bytes. Should be the same as strlen($this->_data)
  91.      * @var integer 
  92.      */
  93.     public $_datasize;
  94.  
  95.     /**
  96.      * The maximum length for a BIFF record (excluding record header and length field). See _addContinue()
  97.      * @var integer 
  98.      * @see _addContinue()
  99.      */
  100.     public $_limit;
  101.  
  102.     /**
  103.      * Constructor
  104.      */
  105.     public function __construct()
  106.     {
  107.         $this->_data       = '';
  108.         $this->_datasize   = 0;
  109.         $this->_limit      = 2080;
  110.     }
  111.  
  112.     /**
  113.      * Determine the byte order and store it as class data to avoid
  114.      * recalculating it for each call to new().
  115.      *
  116.      * @return int 
  117.      */
  118.     public static function getByteOrder()
  119.     {
  120.         if (!isset(self::$_byte_order)) {
  121.             // Check if "pack" gives the required IEEE 64bit float
  122.             $teststr pack("d"1.2345);
  123.             $number  pack("C8"0x8D0x970x6E0x120x830xC00xF30x3F);
  124.             if ($number == $teststr{
  125.                 $byte_order 0;    // Little Endian
  126.             elseif ($number == strrev($teststr)){
  127.                 $byte_order 1;    // Big Endian
  128.             else {
  129.                 // Give up. I'll fix this in a later version.
  130.                 throw new Exception("Required floating point format ".
  131.                                          "not supported on this platform.");
  132.             }
  133.             self::$_byte_order $byte_order;
  134.         }
  135.  
  136.         return self::$_byte_order;
  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        .= $data;
  151.         $this->_datasize    += strlen($data);
  152.     }
  153.  
  154.     /**
  155.      * General storage function like _append, but returns string instead of modifying $this->_data
  156.      *
  157.      * @param string $data binary data to write
  158.      * @return string 
  159.      */
  160.     public function writeData($data)
  161.     {
  162.         if (strlen($data$this->_limit{
  163.             $data $this->_addContinue($data);
  164.         }
  165.         $this->_datasize += strlen($data);
  166.  
  167.         return $data;
  168.     }
  169.  
  170.     /**
  171.      * Writes Excel BOF record to indicate the beginning of a stream or
  172.      * sub-stream in the BIFF file.
  173.      *
  174.      * @param  integer $type Type of BIFF file to write: 0x0005 Workbook,
  175.      *                        0x0010 Worksheet.
  176.      * @access private
  177.      */
  178.     function _storeBof($type)
  179.     {
  180.         $record  0x0809;        // Record identifier
  181.  
  182.         // According to the SDK $build and $year should be set to zero.
  183.         // However, this throws a warning in Excel 5. So, use magic numbers.
  184.         if ($this->_BIFF_version == 0x0500{
  185.             $length  0x0008;
  186.             $unknown '';
  187.             $build   0x096C;
  188.             $year    0x07C9;
  189.         elseif ($this->_BIFF_version == 0x0600{
  190.             $length  0x0010;
  191.  
  192.             // by inspection of real files, MS Office Excel 2007 writes the following
  193.             $unknown pack("VV"0x000100D10x00000406);
  194.  
  195.             $build   0x0DBB;
  196.             $year    0x07CC;
  197.         }
  198.         $version $this->_BIFF_version;
  199.  
  200.         $header  pack("vv",   $record$length);
  201.         $data    pack("vvvv"$version$type$build$year);
  202.         $this->_append($header $data $unknown);
  203.     }
  204.  
  205.     /**
  206.      * Writes Excel EOF record to indicate the end of a BIFF stream.
  207.      *
  208.      * @access private
  209.      */
  210.     function _storeEof()
  211.     {
  212.         $record    0x000A;   // Record identifier
  213.         $length    0x0000;   // Number of bytes to follow
  214.         $header    pack("vv"$record$length);
  215.         $this->_append($header);
  216.     }
  217.  
  218.     /**
  219.      * Writes Excel EOF record to indicate the end of a BIFF stream.
  220.      *
  221.      * @access private
  222.      */
  223.     public function writeEof()
  224.     {
  225.         $record    0x000A;   // Record identifier
  226.         $length    0x0000;   // Number of bytes to follow
  227.         $header    pack("vv"$record$length);
  228.         return $this->writeData($header);
  229.     }
  230.  
  231.     /**
  232.      * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
  233.      * Excel 97 the limit is 8228 bytes. Records that are longer than these limits
  234.      * must be split up into CONTINUE blocks.
  235.      *
  236.      * This function takes a long BIFF record and inserts CONTINUE records as
  237.      * necessary.
  238.      *
  239.      * @param  string  $data The original binary data to be written
  240.      * @return string        A very convenient string of continue blocks
  241.      * @access private
  242.      */
  243.     function _addContinue($data)
  244.     {
  245.         $limit  $this->_limit;
  246.         $record 0x003C;         // Record identifier
  247.  
  248.         // The first 2080/8224 bytes remain intact. However, we have to change
  249.         // the length field of the record.
  250.         $tmp substr($data02pack("v"$limitsubstr($data4$limit);
  251.  
  252.         $header pack("vv"$record$limit);  // Headers for continue records
  253.  
  254.         // Retrieve chunks of 2080/8224 bytes +4 for the header.
  255.         $data_length strlen($data);
  256.         for ($i $limit 4$i ($data_length $limit)$i += $limit{
  257.             $tmp .= $header;
  258.             $tmp .= substr($data$i$limit);
  259.         }
  260.  
  261.         // Retrieve the last chunk of data
  262.         $header  pack("vv"$recordstrlen($data$i);
  263.         $tmp    .= $header;
  264.         $tmp    .= substr($data$istrlen($data$i);
  265.  
  266.         return $tmp;
  267.     }
  268.  
  269. }

Documentation generated on Sun, 27 Feb 2011 16:28:33 -0800 by phpDocumentor 1.4.3