BIFFwriter.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. // Original file header of PEAR::Spreadsheet_Excel_Writer_BIFFwriter (used as the base for this class):
  28. // -----------------------------------------------------------------------------------------
  29. // * Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
  30. // *
  31. // * The majority of this is _NOT_ my code. I simply ported it from the
  32. // * PERL Spreadsheet::WriteExcel module.
  33. // *
  34. // * The author of the Spreadsheet::WriteExcel module is John McNamara
  35. // * <jmcnamara@cpan.org>
  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 xnoguer@rezebra.com
  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. * PHPExcel_Writer_Excel5_BIFFwriter
  62. *
  63. * @category PHPExcel
  64. * @package PHPExcel_Writer_Excel5
  65. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  66. */
  67. class PHPExcel_Writer_Excel5_BIFFwriter
  68. {
  69. /**
  70. * The byte order of this architecture. 0 => little endian, 1 => big endian
  71. * @var integer
  72. */
  73. private static $_byte_order;
  74. /**
  75. * The string containing the data of the BIFF stream
  76. * @var string
  77. */
  78. public $_data;
  79. /**
  80. * The size of the data in bytes. Should be the same as strlen($this->_data)
  81. * @var integer
  82. */
  83. public $_datasize;
  84. /**
  85. * The maximum length for a BIFF record (excluding record header and length field). See _addContinue()
  86. * @var integer
  87. * @see _addContinue()
  88. */
  89. public $_limit = 8224;
  90. /**
  91. * Constructor
  92. */
  93. public function __construct()
  94. {
  95. $this->_data = '';
  96. $this->_datasize = 0;
  97. // $this->_limit = 8224;
  98. }
  99. /**
  100. * Determine the byte order and store it as class data to avoid
  101. * recalculating it for each call to new().
  102. *
  103. * @return int
  104. */
  105. public static function getByteOrder()
  106. {
  107. if (!isset(self::$_byte_order)) {
  108. // Check if "pack" gives the required IEEE 64bit float
  109. $teststr = pack("d", 1.2345);
  110. $number = pack("C8", 0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
  111. if ($number == $teststr) {
  112. $byte_order = 0; // Little Endian
  113. } elseif ($number == strrev($teststr)){
  114. $byte_order = 1; // Big Endian
  115. } else {
  116. // Give up. I'll fix this in a later version.
  117. throw new PHPExcel_Writer_Exception("Required floating point format not supported on this platform.");
  118. }
  119. self::$_byte_order = $byte_order;
  120. }
  121. return self::$_byte_order;
  122. }
  123. /**
  124. * General storage function
  125. *
  126. * @param string $data binary data to append
  127. * @access private
  128. */
  129. function _append($data)
  130. {
  131. if (strlen($data) - 4 > $this->_limit) {
  132. $data = $this->_addContinue($data);
  133. }
  134. $this->_data .= $data;
  135. $this->_datasize += strlen($data);
  136. }
  137. /**
  138. * General storage function like _append, but returns string instead of modifying $this->_data
  139. *
  140. * @param string $data binary data to write
  141. * @return string
  142. */
  143. public function writeData($data)
  144. {
  145. if (strlen($data) - 4 > $this->_limit) {
  146. $data = $this->_addContinue($data);
  147. }
  148. $this->_datasize += strlen($data);
  149. return $data;
  150. }
  151. /**
  152. * Writes Excel BOF record to indicate the beginning of a stream or
  153. * sub-stream in the BIFF file.
  154. *
  155. * @param integer $type Type of BIFF file to write: 0x0005 Workbook,
  156. * 0x0010 Worksheet.
  157. * @access private
  158. */
  159. function _storeBof($type)
  160. {
  161. $record = 0x0809; // Record identifier (BIFF5-BIFF8)
  162. $length = 0x0010;
  163. // by inspection of real files, MS Office Excel 2007 writes the following
  164. $unknown = pack("VV", 0x000100D1, 0x00000406);
  165. $build = 0x0DBB; // Excel 97
  166. $year = 0x07CC; // Excel 97
  167. $version = 0x0600; // BIFF8
  168. $header = pack("vv", $record, $length);
  169. $data = pack("vvvv", $version, $type, $build, $year);
  170. $this->_append($header . $data . $unknown);
  171. }
  172. /**
  173. * Writes Excel EOF record to indicate the end of a BIFF stream.
  174. *
  175. * @access private
  176. */
  177. function _storeEof()
  178. {
  179. $record = 0x000A; // Record identifier
  180. $length = 0x0000; // Number of bytes to follow
  181. $header = pack("vv", $record, $length);
  182. $this->_append($header);
  183. }
  184. /**
  185. * Writes Excel EOF record to indicate the end of a BIFF stream.
  186. *
  187. * @access private
  188. */
  189. public function writeEof()
  190. {
  191. $record = 0x000A; // Record identifier
  192. $length = 0x0000; // Number of bytes to follow
  193. $header = pack("vv", $record, $length);
  194. return $this->writeData($header);
  195. }
  196. /**
  197. * Excel limits the size of BIFF records. In Excel 5 the limit is 2084 bytes. In
  198. * Excel 97 the limit is 8228 bytes. Records that are longer than these limits
  199. * must be split up into CONTINUE blocks.
  200. *
  201. * This function takes a long BIFF record and inserts CONTINUE records as
  202. * necessary.
  203. *
  204. * @param string $data The original binary data to be written
  205. * @return string A very convenient string of continue blocks
  206. * @access private
  207. */
  208. function _addContinue($data)
  209. {
  210. $limit = $this->_limit;
  211. $record = 0x003C; // Record identifier
  212. // The first 2080/8224 bytes remain intact. However, we have to change
  213. // the length field of the record.
  214. $tmp = substr($data, 0, 2) . pack("v", $limit) . substr($data, 4, $limit);
  215. $header = pack("vv", $record, $limit); // Headers for continue records
  216. // Retrieve chunks of 2080/8224 bytes +4 for the header.
  217. $data_length = strlen($data);
  218. for ($i = $limit + 4; $i < ($data_length - $limit); $i += $limit) {
  219. $tmp .= $header;
  220. $tmp .= substr($data, $i, $limit);
  221. }
  222. // Retrieve the last chunk of data
  223. $header = pack("vv", $record, strlen($data) - $i);
  224. $tmp .= $header;
  225. $tmp .= substr($data, $i, strlen($data) - $i);
  226. return $tmp;
  227. }
  228. }