• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/Spreadsheet/Excel/Writer/BIFFwriter.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 *  Module written/ported by Xavier Noguer <xnoguer@php.net>
00004 *
00005 *  The majority of this is _NOT_ my code.  I simply ported it from the
00006 *  PERL Spreadsheet::WriteExcel module.
00007 *
00008 *  The author of the Spreadsheet::WriteExcel module is John McNamara 
00009 *  <jmcnamara@cpan.org>
00010 *
00011 *  I _DO_ maintain this code, and John McNamara has nothing to do with the
00012 *  porting of this code to PHP.  Any questions directly related to this
00013 *  class library should be directed to me.
00014 *
00015 *  License Information:
00016 *
00017 *    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
00018 *    Copyright (c) 2002-2003 Xavier Noguer xnoguer@php.net
00019 *
00020 *    This library is free software; you can redistribute it and/or
00021 *    modify it under the terms of the GNU Lesser General Public
00022 *    License as published by the Free Software Foundation; either
00023 *    version 2.1 of the License, or (at your option) any later version.
00024 *
00025 *    This library is distributed in the hope that it will be useful,
00026 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00028 *    Lesser General Public License for more details.
00029 *
00030 *    You should have received a copy of the GNU Lesser General Public
00031 *    License along with this library; if not, write to the Free Software
00032 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00033 */
00034 
00035 require_once('PEAR.php');
00036 
00054 class Spreadsheet_Excel_Writer_BIFFwriter extends PEAR
00055 {
00060     var $_BIFF_version = 0x0500;
00061 
00066     var $_byte_order;
00067 
00072     var $_data;
00073 
00078     var $_datasize;
00079 
00085     var $_limit;
00086  
00092     function Spreadsheet_Excel_Writer_BIFFwriter()
00093     {
00094         $this->_byte_order = '';
00095         $this->_data       = '';
00096         $this->_datasize   = 0;
00097         $this->_limit      = 2080;   
00098         // Set the byte order
00099         $this->_setByteOrder();
00100     }
00101 
00108     function _setByteOrder()
00109     {
00110         // Check if "pack" gives the required IEEE 64bit float
00111         $teststr = pack("d", 1.2345);
00112         $number  = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
00113         if ($number == $teststr) {
00114             $byte_order = 0;    // Little Endian
00115         }
00116         elseif ($number == strrev($teststr)){
00117             $byte_order = 1;    // Big Endian
00118         }
00119         else {
00120             // Give up. I'll fix this in a later version.
00121             return $this->raiseError("Required floating point format ".
00122                                      "not supported on this platform.");
00123         }
00124         $this->_byte_order = $byte_order;
00125     }
00126 
00133     function _prepend($data)
00134     {
00135         if (strlen($data) > $this->_limit) {
00136             $data = $this->_addContinue($data);
00137         }
00138         $this->_data      = $data.$this->_data;
00139         $this->_datasize += strlen($data);
00140     }
00141 
00148     function _append($data)
00149     {
00150         if (strlen($data) > $this->_limit) {
00151             $data = $this->_addContinue($data);
00152         }
00153         $this->_data      = $this->_data.$data;
00154         $this->_datasize += strlen($data);
00155     }
00156 
00165     function _storeBof($type)
00166     {
00167         $record  = 0x0809;        // Record identifier
00168 
00169         // According to the SDK $build and $year should be set to zero.
00170         // However, this throws a warning in Excel 5. So, use magic numbers.
00171         if ($this->_BIFF_version == 0x0500) {
00172             $length  = 0x0008;
00173             $unknown = '';
00174             $build   = 0x096C;
00175             $year    = 0x07C9;
00176         }
00177         elseif ($this->_BIFF_version == 0x0600) {
00178             $length  = 0x0010;
00179             $unknown = pack("VV", 0x00000041, 0x00000006); //unknown last 8 bytes for BIFF8
00180             $build   = 0x0DBB;
00181             $year    = 0x07CC;
00182         }
00183         $version = $this->_BIFF_version;
00184    
00185         $header  = pack("vv",   $record, $length);
00186         $data    = pack("vvvv", $version, $type, $build, $year);
00187         $this->_prepend($header.$data.$unknown);
00188     }
00189 
00195     function _storeEof() 
00196     {
00197         $record    = 0x000A;   // Record identifier
00198         $length    = 0x0000;   // Number of bytes to follow
00199         $header    = pack("vv", $record, $length);
00200         $this->_append($header);
00201     }
00202 
00215     function _addContinue($data)
00216     {
00217         $limit      = $this->_limit;
00218         $record     = 0x003C;         // Record identifier
00219  
00220         // The first 2080/8224 bytes remain intact. However, we have to change
00221         // the length field of the record.
00222         $tmp = substr($data, 0, 2).pack("v", $limit-4).substr($data, 4, $limit - 4);
00223         
00224         $header = pack("vv", $record, $limit);  // Headers for continue records
00225  
00226         // Retrieve chunks of 2080/8224 bytes +4 for the header.
00227         for($i = $limit; $i < strlen($data) - $limit; $i += $limit)
00228         {
00229             $tmp .= $header;
00230             $tmp .= substr($data, $i, $limit);
00231         }
00232  
00233         // Retrieve the last chunk of data
00234         $header  = pack("vv", $record, strlen($data) - $i);
00235         $tmp    .= $header;
00236         $tmp    .= substr($data,$i,strlen($data) - $i);
00237  
00238         return $tmp;
00239     }
00240 }
00241 ?>

Generated on Fri Dec 13 2013 13:52:08 for ILIAS Release_3_7_x_branch .rev 46817 by  doxygen 1.7.1