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