Subversion Repositories eFlore/Applications.cel

Rev

Rev 970 | Rev 1605 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 970 Rev 1604
Line 1... Line 1...
1
<?php
1
<?php
2
/*
2
/*
3
*  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
3
*  Module written/ported by Xavier Noguer <xnoguer@php.net>
4
*
4
*
5
*  The majority of this is _NOT_ my code.  I simply ported it from the
5
*  The majority of this is _NOT_ my code.  I simply ported it from the
6
*  PERL Spreadsheet::WriteExcel module.
6
*  PERL Spreadsheet::WriteExcel module.
7
*
7
*
8
*  The author of the Spreadsheet::WriteExcel module is John McNamara 
8
*  The author of the Spreadsheet::WriteExcel module is John McNamara
9
*  <jmcnamara@cpan.org>
9
*  <jmcnamara@cpan.org>
10
*
10
*
11
*  I _DO_ maintain this code, and John McNamara has nothing to do with the
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
12
*  porting of this code to PHP.  Any questions directly related to this
13
*  class library should be directed to me.
13
*  class library should be directed to me.
14
*
14
*
15
*  License Information:
15
*  License Information:
16
*
16
*
17
*    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
17
*    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
18
*    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
18
*    Copyright (c) 2002-2003 Xavier Noguer xnoguer@php.net
19
*
19
*
20
*    This library is free software; you can redistribute it and/or
20
*    This library is free software; you can redistribute it and/or
21
*    modify it under the terms of the GNU Lesser General Public
21
*    modify it under the terms of the GNU Lesser General Public
22
*    License as published by the Free Software Foundation; either
22
*    License as published by the Free Software Foundation; either
23
*    version 2.1 of the License, or (at your option) any later version.
23
*    version 2.1 of the License, or (at your option) any later version.
Line 30... Line 30...
30
*    You should have received a copy of the GNU Lesser General Public
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
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
32
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33
*/
33
*/
Line 34... Line 34...
34
 
34
 
Line 35... Line 35...
35
//require_once('PEAR.php');
35
require_once 'PEAR.php';
36
 
36
 
37
/**
37
/**
38
* Class for writing Excel BIFF records.
38
* Class for writing Excel BIFF records.
39
* 
39
*
40
* From "MICROSOFT EXCEL BINARY FILE FORMAT" by Mark O'Brien (Microsoft Corporation):
40
* From "MICROSOFT EXCEL BINARY FILE FORMAT" by Mark O'Brien (Microsoft Corporation):
41
*
41
*
42
* BIFF (BInary File Format) is the file format in which Excel documents are 
42
* BIFF (BInary File Format) is the file format in which Excel documents are
43
* saved on disk.  A BIFF file is a complete description of an Excel document.
43
* saved on disk.  A BIFF file is a complete description of an Excel document.
44
* BIFF files consist of sequences of variable-length records. There are many 
44
* BIFF files consist of sequences of variable-length records. There are many
45
* different types of BIFF records.  For example, one record type describes a 
45
* different types of BIFF records.  For example, one record type describes a
46
* formula entered into a cell; one describes the size and location of a 
46
* formula entered into a cell; one describes the size and location of a
47
* window into a document; another describes a picture format.
47
* window into a document; another describes a picture format.
48
*
48
*
49
* @author   Xavier Noguer <xnoguer@rezebra.com>
49
* @author   Xavier Noguer <xnoguer@php.net>
50
* @category FileFormats
50
* @category FileFormats
Line 51... Line 51...
51
* @package  Spreadsheet_Excel_Writer
51
* @package  Spreadsheet_Excel_Writer
52
*/
52
*/
53
 
53
 
54
class Spreadsheet_Excel_Writer_BIFFwriter extends PEAR
54
class Spreadsheet_Excel_Writer_BIFFwriter extends PEAR
55
{
55
{
56
    /**
56
    /**
57
    * The BIFF/Excel version (5). 
57
    * The BIFF/Excel version (5).
Line 58... Line 58...
58
    * @var integer
58
    * @var integer
59
    */
59
    */
60
    var $_BIFF_version = 0x0500;
60
    var $_BIFF_version = 0x0500;
61
 
61
 
62
    /**
62
    /**
Line 63... Line 63...
63
    * The byte order of this architecture. 0 => little endian, 1 => big endian 
63
    * The byte order of this architecture. 0 => little endian, 1 => big endian
Line 81... Line 81...
81
    * The maximun length for a BIFF record. See _addContinue()
81
    * The maximun length for a BIFF record. See _addContinue()
82
    * @var integer
82
    * @var integer
83
    * @see _addContinue()
83
    * @see _addContinue()
84
    */
84
    */
85
    var $_limit;
85
    var $_limit;
86
 
86
 
-
 
87
    /**
-
 
88
    * The temporary dir for storing the OLE file
-
 
89
    * @var string
-
 
90
    */
-
 
91
    var $_tmp_dir;
-
 
92
 
-
 
93
    /**
-
 
94
    * The temporary file for storing the OLE file
-
 
95
    * @var string
-
 
96
    */
-
 
97
    var $_tmp_file;
-
 
98
 
87
    /**
99
    /**
88
    * Constructor
100
    * Constructor
89
    *
101
    *
90
    * @access public
102
    * @access public
91
    */
103
    */
92
    function Spreadsheet_Excel_Writer_BIFFwriter()
104
    function Spreadsheet_Excel_Writer_BIFFwriter()
93
    {
105
    {
94
        $this->_byte_order = '';
106
        $this->_byte_order = '';
95
        $this->_data       = '';
107
        $this->_data       = '';
96
        $this->_datasize   = 0;
108
        $this->_datasize   = 0;
97
        $this->_limit      = 2080;   
109
        $this->_limit      = 2080;
-
 
110
        $this->_tmp_dir    = '';
98
        // Set the byte order
111
        // Set the byte order
99
        $this->_setByteOrder();
112
        $this->_setByteOrder();
100
    }
113
    }
Line 101... Line 114...
101
 
114
 
102
/**
115
    /**
103
* Determine the byte order and store it as class data to avoid
116
    * Determine the byte order and store it as class data to avoid
104
* recalculating it for each call to new().
117
    * recalculating it for each call to new().
105
*
118
    *
106
* @access private
119
    * @access private
107
*/
120
    */
108
    function _setByteOrder()
121
    function _setByteOrder()
109
    {
-
 
110
        if ($this->_byte_order == '')
-
 
111
        {
122
    {
112
            // Check if "pack" gives the required IEEE 64bit float
123
        // Check if "pack" gives the required IEEE 64bit float
113
            $teststr = pack("d", 1.2345);
124
        $teststr = pack("d", 1.2345);
114
            $number  = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
125
        $number  = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
115
            if ($number == $teststr) {
126
        if ($number == $teststr) {
116
                $byte_order = 0;    // Little Endian
-
 
117
            }
127
            $byte_order = 0;    // Little Endian
118
            elseif ($number == strrev($teststr)){
128
        } elseif ($number == strrev($teststr)){
119
                $byte_order = 1;    // Big Endian
-
 
120
            }
129
            $byte_order = 1;    // Big Endian
121
            else {
130
        } else {
122
                // Give up. I'll fix this in a later version.
131
            // Give up. I'll fix this in a later version.
123
                $this->raiseError("Required floating point format not supported ".
132
            return $this->raiseError("Required floating point format ".
124
                    "on this platform.");
-
 
125
            }
133
                                     "not supported on this platform.");
126
        }
134
        }
127
        $this->_byte_order = $byte_order;
135
        $this->_byte_order = $byte_order;
Line 128... Line 136...
128
    }
136
    }
129
 
137
 
130
/**
138
    /**
131
* General storage function
139
    * General storage function
132
*
140
    *
133
* @param string $data binary data to prepend
141
    * @param string $data binary data to prepend
134
* @access private
142
    * @access private
135
*/
143
    */
136
    function _prepend($data)
144
    function _prepend($data)
137
    {
145
    {
138
        if (strlen($data) > $this->_limit) {
146
        if (strlen($data) > $this->_limit) {
139
            $data = $this->_addContinue($data);
147
            $data = $this->_addContinue($data);
140
        }
148
        }
141
        $this->_data      = $data.$this->_data;
149
        $this->_data      = $data.$this->_data;
Line 142... Line 150...
142
        $this->_datasize += strlen($data);
150
        $this->_datasize += strlen($data);
143
    }
151
    }
144
 
152
 
145
/**
153
    /**
146
* General storage function
154
    * General storage function
147
*
155
    *
148
* @param string $data binary data to append
156
    * @param string $data binary data to append
149
* @access private
157
    * @access private
150
*/
158
    */
151
    function _append($data)
159
    function _append($data)
152
    {
160
    {
153
        if (strlen($data) > $this->_limit) {
161
        if (strlen($data) > $this->_limit) {
154
            $data = $this->_addContinue($data);
162
            $data = $this->_addContinue($data);
155
        }
163
        }
Line 156... Line 164...
156
        $this->_data      = $this->_data.$data;
164
        $this->_data      = $this->_data.$data;
157
        $this->_datasize += strlen($data);
165
        $this->_datasize += strlen($data);
158
    }
166
    }
159
 
167
 
160
/**
168
    /**
-
 
169
    * Writes Excel BOF record to indicate the beginning of a stream or
161
* Writes Excel BOF record to indicate the beginning of a stream or
170
    * sub-stream in the BIFF file.
162
* sub-stream in the BIFF file.
171
    *
163
*
172
    * @param  integer $type Type of BIFF file to write: 0x0005 Workbook,
164
* @param  integer $type type of BIFF file to write: 0x0005 Workbook, 0x0010 Worksheet.
173
    *                       0x0010 Worksheet.
165
* @access private
174
    * @access private
166
*/
-
 
167
    function _storeBof($type)
-
 
168
    {
175
    */
169
        $record  = 0x0809;        // Record identifier
176
    function _storeBof($type)
170
        $length  = 0x0008;        // Number of bytes to follow
177
    {
-
 
178
        $record  = 0x0809;        // Record identifier
-
 
179
 
171
        $version = $this->_BIFF_version;
180
        // According to the SDK $build and $year should be set to zero.
172
   
181
        // However, this throws a warning in Excel 5. So, use magic numbers.
173
        // According to the SDK $build and $year should be set to zero.
182
        if ($this->_BIFF_version == 0x0500) {
-
 
183
            $length  = 0x0008;
-
 
184
            $unknown = '';
-
 
185
            $build   = 0x096C;
-
 
186
            $year    = 0x07C9;
-
 
187
        } elseif ($this->_BIFF_version == 0x0600) {
-
 
188
            $length  = 0x0010;
-
 
189
            $unknown = pack("VV", 0x00000041, 0x00000006); //unknown last 8 bytes for BIFF8
174
        // However, this throws a warning in Excel 5. So, use these
190
            $build   = 0x0DBB;
175
        // magic numbers.
191
            $year    = 0x07CC;
176
        $build   = 0x096C;
192
        }
177
        $year    = 0x07C9;
193
        $version = $this->_BIFF_version;
178
   
194
 
Line 179... Line 195...
179
        $header  = pack("vv",   $record, $length);
195
        $header  = pack("vv",   $record, $length);
180
        $data    = pack("vvvv", $version, $type, $build, $year);
196
        $data    = pack("vvvv", $version, $type, $build, $year);
181
        $this->_prepend($header.$data);
197
        $this->_prepend($header . $data . $unknown);
182
    }
198
    }
183
 
199
 
184
/**
200
    /**
185
* Writes Excel EOF record to indicate the end of a BIFF stream.
201
    * Writes Excel EOF record to indicate the end of a BIFF stream.
186
*
202
    *
187
* @access private
203
    * @access private
188
*/
204
    */
189
    function _storeEof() 
205
    function _storeEof()
190
    {
206
    {
Line 191... Line 207...
191
        $record    = 0x000A;   // Record identifier
207
        $record    = 0x000A;   // Record identifier
192
        $length    = 0x0000;   // Number of bytes to follow
208
        $length    = 0x0000;   // Number of bytes to follow
193
        $header    = pack("vv", $record, $length);
209
        $header    = pack("vv", $record, $length);
194
        $this->_append($header);
210
        $this->_append($header);
195
    }
211
    }
196
 
212
 
197
/**
213
    /**
198
* Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
214
    * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
199
* Excel 97 the limit is 8228 bytes. Records that are longer than these limits
215
    * Excel 97 the limit is 8228 bytes. Records that are longer than these limits
200
* must be split up into CONTINUE blocks.
216
    * must be split up into CONTINUE blocks.
201
*
217
    *
202
* This function takes a long BIFF record and inserts CONTINUE records as
218
    * This function takes a long BIFF record and inserts CONTINUE records as
203
* necessary.
219
    * necessary.
204
*
220
    *
205
* @param  string  $data The original binary data to be written
221
    * @param  string  $data The original binary data to be written
206
* @return string        A very convenient string of continue blocks
222
    * @return string        A very convenient string of continue blocks
207
* @access private
223
    * @access private
208
*/
224
    */
209
    function _addContinue($data)
225
    function _addContinue($data)
210
    {
226
    {
211
        $limit      = $this->_limit;
227
        $limit  = $this->_limit;
212
        $record     = 0x003C;         // Record identifier
228
        $record = 0x003C;         // Record identifier
213
 
229
 
214
        // The first 2080/8224 bytes remain intact. However, we have to change
230
        // The first 2080/8224 bytes remain intact. However, we have to change
-
 
231
        // the length field of the record.
215
        // the length field of the record.
232
        $tmp = substr($data, 0, 2).pack("v", $limit-4).substr($data, 4, $limit - 4);
216
        $tmp = substr($data, 0, 2).pack("v", $limit-4).substr($data, 4, $limit - 4);
-
 
217
        
233
 
218
        $header = pack("vv", $record, $limit);  // Headers for continue records
234
        $header = pack("vv", $record, $limit);  // Headers for continue records
219
 
235
 
220
        // Retrieve chunks of 2080/8224 bytes +4 for the header.
236
        // Retrieve chunks of 2080/8224 bytes +4 for the header.
221
        for($i = $limit; $i < strlen($data) - $limit; $i += $limit)
237
        $data_length = strlen($data);
222
        {
238
        for ($i = $limit; $i <  ($data_length - $limit); $i += $limit) {
223
            $tmp .= $header;
239
            $tmp .= $header;
224
            $tmp .= substr($data, $i, $limit);
240
            $tmp .= substr($data, $i, $limit);
225
        }
241
        }
226
 
242
 
-
 
243
        // Retrieve the last chunk of data
-
 
244
        $header  = pack("vv", $record, strlen($data) - $i);
-
 
245
        $tmp    .= $header;
-
 
246
        $tmp    .= substr($data, $i, strlen($data) - $i);
-
 
247
 
-
 
248
        return $tmp;
-
 
249
    }
-
 
250
 
-
 
251
    /**
-
 
252
    * Sets the temp dir used for storing the OLE file
-
 
253
    *
-
 
254
    * @access public
-
 
255
    * @param string $dir The dir to be used as temp dir
-
 
256
    * @return true if given dir is valid, false otherwise
-
 
257
    */
-
 
258
    function setTempDir($dir)
227
        // Retrieve the last chunk of data
259
    {
228
        $header  = pack("vv", $record, strlen($data) - $i);
260
        if (is_dir($dir)) {
229
        $tmp    .= $header;
261
            $this->_tmp_dir = $dir;