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

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