Excel5.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. <?php
  2. class PHPExcel_Reader_Excel5 {
  3. // ParseXL definitions
  4. const XLS_BIFF8 = 0x0600;
  5. const XLS_BIFF7 = 0x0500;
  6. const XLS_WorkbookGlobals = 0x0005;
  7. const XLS_Worksheet = 0x0010;
  8. // record identifiers
  9. const XLS_Type_FORMULA = 0x0006;
  10. const XLS_Type_FORMULA2 = 0x0406;
  11. const XLS_Type_EOF = 0x000a;
  12. const XLS_Type_PROTECT = 0x0012;
  13. const XLS_Type_OBJECTPROTECT = 0x0063;
  14. const XLS_Type_SCENPROTECT = 0x00dd;
  15. const XLS_Type_PASSWORD = 0x0013;
  16. const XLS_Type_HEADER = 0x0014;
  17. const XLS_Type_FOOTER = 0x0015;
  18. const XLS_Type_EXTERNSHEET = 0x0017;
  19. const XLS_Type_DEFINEDNAME = 0x0018;
  20. const XLS_Type_VERTICALPAGEBREAKS = 0x001a;
  21. const XLS_Type_HORIZONTALPAGEBREAKS = 0x001b;
  22. const XLS_Type_NOTE = 0x001c;
  23. const XLS_Type_SELECTION = 0x001d;
  24. const XLS_Type_DATEMODE = 0x0022;
  25. const XLS_Type_EXTERNNAME = 0x0023;
  26. const XLS_Type_LEFTMARGIN = 0x0026;
  27. const XLS_Type_RIGHTMARGIN = 0x0027;
  28. const XLS_Type_TOPMARGIN = 0x0028;
  29. const XLS_Type_BOTTOMMARGIN = 0x0029;
  30. const XLS_Type_PRINTGRIDLINES = 0x002b;
  31. const XLS_Type_FILEPASS = 0x002f;
  32. const XLS_Type_FONT = 0x0031;
  33. const XLS_Type_CONTINUE = 0x003c;
  34. const XLS_Type_PANE = 0x0041;
  35. const XLS_Type_CODEPAGE = 0x0042;
  36. const XLS_Type_DEFCOLWIDTH = 0x0055;
  37. const XLS_Type_OBJ = 0x005d;
  38. const XLS_Type_COLINFO = 0x007d;
  39. const XLS_Type_IMDATA = 0x007f;
  40. const XLS_Type_SHEETPR = 0x0081;
  41. const XLS_Type_HCENTER = 0x0083;
  42. const XLS_Type_VCENTER = 0x0084;
  43. const XLS_Type_SHEET = 0x0085;
  44. const XLS_Type_PALETTE = 0x0092;
  45. const XLS_Type_SCL = 0x00a0;
  46. const XLS_Type_PAGESETUP = 0x00a1;
  47. const XLS_Type_MULRK = 0x00bd;
  48. const XLS_Type_MULBLANK = 0x00be;
  49. const XLS_Type_DBCELL = 0x00d7;
  50. const XLS_Type_XF = 0x00e0;
  51. const XLS_Type_MERGEDCELLS = 0x00e5;
  52. const XLS_Type_MSODRAWINGGROUP = 0x00eb;
  53. const XLS_Type_MSODRAWING = 0x00ec;
  54. const XLS_Type_SST = 0x00fc;
  55. const XLS_Type_LABELSST = 0x00fd;
  56. const XLS_Type_EXTSST = 0x00ff;
  57. const XLS_Type_EXTERNALBOOK = 0x01ae;
  58. const XLS_Type_DATAVALIDATIONS = 0x01b2;
  59. const XLS_Type_TXO = 0x01b6;
  60. const XLS_Type_HYPERLINK = 0x01b8;
  61. const XLS_Type_DATAVALIDATION = 0x01be;
  62. const XLS_Type_DIMENSION = 0x0200;
  63. const XLS_Type_BLANK = 0x0201;
  64. const XLS_Type_NUMBER = 0x0203;
  65. const XLS_Type_LABEL = 0x0204;
  66. const XLS_Type_BOOLERR = 0x0205;
  67. const XLS_Type_STRING = 0x0207;
  68. const XLS_Type_ROW = 0x0208;
  69. const XLS_Type_INDEX = 0x020b;
  70. const XLS_Type_ARRAY = 0x0221;
  71. const XLS_Type_DEFAULTROWHEIGHT = 0x0225;
  72. const XLS_Type_WINDOW2 = 0x023e;
  73. const XLS_Type_RK = 0x007e;
  74. const XLS_Type_RK2 = 0x027e;
  75. const XLS_Type_STYLE = 0x0293;
  76. const XLS_Type_FORMAT = 0x041e;
  77. const XLS_Type_SHAREDFMLA = 0x04bc;
  78. const XLS_Type_BOF = 0x0809;
  79. const XLS_Type_SHEETPROTECTION = 0x0867;
  80. const XLS_Type_RANGEPROTECTION = 0x0868;
  81. const XLS_Type_SHEETLAYOUT = 0x0862;
  82. const XLS_Type_XFEXT = 0x087d;
  83. const XLS_Type_PAGELAYOUTVIEW = 0x088b;
  84. const XLS_Type_UNKNOWN = 0xffff;
  85. // Encryption type
  86. const MS_BIFF_CRYPTO_NONE = 0;
  87. const MS_BIFF_CRYPTO_XOR = 1;
  88. const MS_BIFF_CRYPTO_RC4 = 2;
  89. // Size of stream blocks when using RC4 encryption
  90. const REKEY_BLOCK = 0x400;
  91. private $_pos;
  92. private $_data;
  93. private $_cell;
  94. private $_sst;
  95. private $_sheets;
  96. private $_dataSize;
  97. private static $_codepage = 'CP1252';
  98. private $index = 0;
  99. private $curretSheet = 0;
  100. private $builtInFormats = array(
  101. 0 => 'General',
  102. 1 => '0',
  103. 2 => '0.00',
  104. 3 => '#,##0',
  105. 4 => '#,##0.00',
  106. 9 => '0%',
  107. 10 => '0.00%',
  108. 11 => '0.00E+00',
  109. 12 => '# ?/?',
  110. 13 => '# ??/??',
  111. 14 => 'yyyy/m/d',
  112. 15 => 'd-mmm-yy',
  113. 16 => 'd-mmm',
  114. 17 => 'mmm-yy',
  115. 18 => 'h:mm AM/PM',
  116. 19 => 'h:mm:ss AM/PM',
  117. 20 => 'h:mm',
  118. 21 => 'h:mm:ss',
  119. 22 => 'yyyy/m/d h:mm',
  120. 28 => 'm月d日',
  121. 31 => 'yyyy年m月d日',
  122. 32 => 'h时mmi分',
  123. 33 => 'h时mmi分ss秒',
  124. 34 => 'AM/PM h时mmi分',
  125. 35 => 'AM/PM h时mmi分ss秒',
  126. 37 => '#,##0 ;(#,##0)',
  127. 38 => '#,##0 ;[Red](#,##0)',
  128. 39 => '#,##0.00;(#,##0.00)',
  129. 40 => '#,##0.00;[Red](#,##0.00)',
  130. 44 => '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
  131. 45 => 'mm:ss',
  132. 46 => '[h]:mm:ss',
  133. 47 => 'mm:ss.0',
  134. 48 => '##0.0E+0',
  135. 49 => '@',
  136. // CHT
  137. 27 => 'yyyy年m月',
  138. 30 => 'm/d/yy',
  139. 36 => '[$-404]e/m/d',
  140. 50 => '[$-404]e/m/d',
  141. 57 => '[$-404]e/m/d',
  142. // THA
  143. 59 => 't0',
  144. 60 => 't0.00',
  145. 61 => 't#,##0',
  146. 62 => 't#,##0.00',
  147. 67 => 't0%',
  148. 68 => 't0.00%',
  149. 69 => 't# ?/?',
  150. 70 => 't# ??/??'
  151. );
  152. public $error = false;
  153. /**
  154. * Create a new Spreadsheet_Excel_Reader instance
  155. */
  156. public function __construct($file) {
  157. if ( ! file_exists($file)) {
  158. throw new Exception("Could not open " . $file . " for reading! File does not exist.");
  159. }
  160. try {
  161. $ole = new PHPExcel_Reader_OLERead();
  162. $ole->read($file);
  163. $this->_data = $ole->getStream($ole->wrkbook);
  164. return true;
  165. } catch (Exception $e) {
  166. return false;
  167. }
  168. }
  169. /**
  170. * Changes sheet to another.
  171. * @param bool
  172. */
  173. public function ChangeSheet($index){
  174. $this->curretSheet = $index;
  175. return true;
  176. }
  177. /**
  178. * 获取Cell数据
  179. */
  180. public function getCell(){
  181. $this->_cell = array();
  182. $this->_endRow = false;
  183. $this->_key = null;
  184. if( ! $this->_parse){
  185. $this->_parse = true;
  186. $this->_pos = 0;
  187. // Parse Workbook Global Substream
  188. while ($this->_pos < $this->_dataSize) {
  189. $code = self::_GetInt2d($this->_data, $this->_pos);
  190. switch ($code) {
  191. case self::XLS_Type_SST: $this->_readSst(); break;
  192. case self::XLS_Type_CODEPAGE: $this->_readCodepage(); break;
  193. case self::XLS_Type_DATEMODE: $this->_readDateMode(); break;
  194. case self::XLS_Type_FORMAT: $this->_readFormat(); break;
  195. case self::XLS_Type_XF: $this->_readXf(); break;
  196. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  197. default: $this->_readDefault(); break;
  198. }
  199. }
  200. }
  201. // Parse the individual sheet
  202. $this->_pos = $this->_lastPos ? $this->_lastPos : $this->_sheets[$this->curretSheet]['offset'];
  203. while ($this->_pos <= $this->_dataSize - 4) {
  204. if($this->_endRow) break;
  205. $code = self::_GetInt2d($this->_data, $this->_pos);
  206. switch ($code) {
  207. //case self::XLS_Type_RK:
  208. case self::XLS_Type_RK2: $this->_readRk(); break;
  209. case self::XLS_Type_LABELSST: $this->_readLabelSst(); break;
  210. case self::XLS_Type_MULRK: $this->_readMulRk(); break;
  211. case self::XLS_Type_NUMBER: $this->_readNumber(); break;
  212. case self::XLS_Type_FORMULA:
  213. case self::XLS_Type_FORMULA2: $this->_readFormula(); break;
  214. case self::XLS_Type_BOOLERR: $this->_readBoolErr(); break;
  215. case self::XLS_Type_STRING: $this->_readString(); break;
  216. case self::XLS_Type_MULBLANK: $this->_readBlank(); break;
  217. case self::XLS_Type_LABEL: $this->_readLabel(); break;
  218. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  219. default: $this->_readDefault(); break;
  220. }
  221. }
  222. return $this->_cell;
  223. }
  224. /**
  225. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  226. */
  227. public function getWorksheetInfo() {
  228. if( ! $this->_sheets){
  229. $this->_dataSize = strlen($this->_data); // total byte size of Excel data (workbook global substream + sheet substreams)
  230. $this->_pos = 0;
  231. $this->_sheets = array();
  232. // Parse Workbook Global Substream
  233. while ($this->_pos < $this->_dataSize) {
  234. $code = self::_GetInt2d($this->_data, $this->_pos);
  235. switch ($code) {
  236. case self::XLS_Type_BOF: $this->_readBof(); break;
  237. case self::XLS_Type_SHEET: $this->_readSheet(); break;
  238. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  239. default: $this->_readDefault(); break;
  240. }
  241. }
  242. }
  243. if( ! isset($this->_sheets[$this->curretSheet])){
  244. return array();
  245. }
  246. $sheetInfo = array(
  247. 'worksheetName' => $this->_sheets[$this->curretSheet]['name'],
  248. 'lastColumnLetter' => 'A',
  249. 'lastColumnIndex' => 0,
  250. 'totalRows' => 0,
  251. 'totalColumns' => 0
  252. );
  253. // Parse the individual sheet
  254. $this->_pos = $this->_sheets[$this->curretSheet]['offset'];
  255. while ($this->_pos <= $this->_dataSize - 4) {
  256. $code = self::_GetInt2d($this->_data, $this->_pos);
  257. switch ($code) {
  258. case self::XLS_Type_RK2:
  259. case self::XLS_Type_LABELSST:
  260. case self::XLS_Type_NUMBER:
  261. case self::XLS_Type_FORMULA:
  262. case self::XLS_Type_BOOLERR:
  263. case self::XLS_Type_LABEL:
  264. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  265. $recordData = substr($this->_data, $this->_pos + 4, $length);
  266. // move stream pointer to next record
  267. $this->_pos += 4 + $length;
  268. $rowIndex = self::_GetInt2d($recordData, 0) + 1;
  269. $columnIndex = self::_GetInt2d($recordData, 2);
  270. $sheetInfo['totalRows'] = max($sheetInfo['totalRows'], $rowIndex);
  271. $sheetInfo['lastColumnIndex'] = max($sheetInfo['lastColumnIndex'], $columnIndex);
  272. break;
  273. case self::XLS_Type_BOF: $this->_readBof(); break;
  274. case self::XLS_Type_EOF: $this->_readDefault(); break 2;
  275. default: $this->_readDefault(); break;
  276. }
  277. $sheetInfo['totalColumns'] = $sheetInfo['lastColumnIndex'] + 1;
  278. $sheetInfo['lastColumnLetter'] = self::_stringFromColumnIndex($sheetInfo['lastColumnIndex']);
  279. }
  280. return $sheetInfo;
  281. }
  282. private function _addCell($row, $column, $value, $xfIndex){
  283. if(is_null($this->_key)){
  284. $this->_key = $row;
  285. }
  286. if($row > $this->_key){
  287. $this->_endRow = true;
  288. return false;
  289. }
  290. $xfRecord = $this->xfRecords[$xfIndex];
  291. $this->_lastPos = $this->_pos;
  292. $this->_cell[$column] = $this->_format_value($value, $xfRecord['format']);
  293. }
  294. private function _format_value($value = '0', $format = 'General'){
  295. if ( ! is_numeric($value) || $format == 'General' || $format == '@') return $value;
  296. $sections = explode(';', $format);
  297. switch (count($sections)) {
  298. case 1:
  299. $format = $sections[0];
  300. break;
  301. case 2:
  302. $format = ($value >= 0) ? $sections[0] : $sections[1];
  303. $value = abs($value); // Use the absolute value
  304. break;
  305. case 3:
  306. $format = ($value > 0) ?
  307. $sections[0] : ( ($value < 0) ?
  308. $sections[1] : $sections[2]);
  309. $value = abs($value); // Use the absolute value
  310. break;
  311. case 4:
  312. $format = ($value > 0) ?
  313. $sections[0] : ( ($value < 0) ?
  314. $sections[1] : $sections[2]);
  315. $value = abs($value); // Use the absolute value
  316. break;
  317. default:
  318. // something is wrong, just use first section
  319. $format = $sections[0];
  320. break;
  321. }
  322. $color_regex = '/^\\[[a-zA-Z]+\\]/';
  323. $format = preg_replace($color_regex, '', $format);
  324. if (preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $format)) { // datetime format
  325. $value = $this->_formatAsDate($value, $format);
  326. } else if (preg_match('/%$/', $format)) { // % number format
  327. $value = self::_formatAsPercentage($value, $format);
  328. } else {
  329. if ($format === '[$EUR ]#,##0.00_-') {
  330. $value = 'EUR ' . sprintf('%1.2f', $value);
  331. } else {
  332. // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  333. $format = preg_replace('/_./', '', $format);
  334. // Some non-number characters are escaped with \, which we don't need
  335. $format = preg_replace("/\\\\/", '', $format);
  336. // Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
  337. $format = str_replace(array('"','*'), '', $format);
  338. // Find out if we need thousands separator
  339. // This is indicated by a comma enclosed by a digit placeholder:
  340. // #,# or 0,0
  341. $useThousands = preg_match('/(#,#|0,0)/', $format);
  342. if ($useThousands) {
  343. $format = preg_replace('/0,0/', '00', $format);
  344. $format = preg_replace('/#,#/', '##', $format);
  345. }
  346. // Scale thousands, millions,...
  347. // This is indicated by a number of commas after a digit placeholder:
  348. // #, or 0.0,,
  349. $scale = 1; // same as no scale
  350. $matches = array();
  351. if (preg_match('/(#|0)(,+)/', $format, $matches)) {
  352. $scale = pow(1000, strlen($matches[2]));
  353. // strip the commas
  354. $format = preg_replace('/0,+/', '0', $format);
  355. $format = preg_replace('/#,+/', '#', $format);
  356. }
  357. if (preg_match('/#?.*\?\/\?/', $format, $m)) {
  358. //echo 'Format mask is fractional '.$format.' <br />';
  359. if ($value != (int)$value) {
  360. self::_formatAsFraction($value, $format);
  361. }
  362. } else {
  363. // Handle the number itself
  364. // scale number
  365. $value = $value / $scale;
  366. // Strip #
  367. $format = preg_replace('/\\#/', '0', $format);
  368. $n = "/\[[^\]]+\]/";
  369. $m = preg_replace($n, '', $format);
  370. $number_regex = "/(0+)(\.?)(0*)/";
  371. if (preg_match($number_regex, $m, $matches)) {
  372. $left = $matches[1];
  373. $dec = $matches[2];
  374. $right = $matches[3];
  375. // minimun width of formatted number (including dot)
  376. $minWidth = strlen($left) + strlen($dec) + strlen($right);
  377. if ($useThousands) {
  378. $value = number_format(
  379. $value
  380. , strlen($right)
  381. , '.'
  382. , ','
  383. );
  384. $value = preg_replace($number_regex, $value, $format);
  385. } else {
  386. if (preg_match('/[0#]E[+-]0/i', $format)) {
  387. // Scientific format
  388. $value = sprintf('%5.2E', $value);
  389. } elseif (preg_match('/0([^\d\.]+)0/', $format)) {
  390. $value = self::_complexNumberFormatMask($value, $format);
  391. } else {
  392. $sprintf_pattern = "%0$minWidth." . strlen($right) . "f";
  393. $value = sprintf($sprintf_pattern, $value);
  394. $value = preg_replace($number_regex, $value, $format);
  395. }
  396. }
  397. }
  398. }
  399. if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
  400. // Currency or Accounting
  401. $currencyFormat = $m[0];
  402. $currencyCode = $m[1];
  403. list($currencyCode) = explode('-',$currencyCode);
  404. if ($currencyCode == '') {
  405. $currencyCode = '$';
  406. }
  407. $value = preg_replace('/\[\$([^\]]*)\]/u',$currencyCode,$value);
  408. }
  409. }
  410. }
  411. return $value;
  412. }
  413. private static function _complexNumberFormatMask($number, $mask) {
  414. if (strpos($mask,'.') !== false) {
  415. $numbers = explode('.', $number . '.0');
  416. $masks = explode('.', $mask . '.0');
  417. $result1 = self::_complexNumberFormatMask($numbers[0], $masks[0]);
  418. $result2 = strrev(self::_complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1])));
  419. return $result1 . '.' . $result2;
  420. }
  421. $r = preg_match_all('/0+/', $mask, $result, PREG_OFFSET_CAPTURE);
  422. if ($r > 1) {
  423. $result = array_reverse($result[0]);
  424. foreach($result as $block) {
  425. $divisor = 1 . $block[0];
  426. $size = strlen($block[0]);
  427. $offset = $block[1];
  428. $blockValue = sprintf(
  429. '%0' . $size . 'd',
  430. fmod($number, $divisor)
  431. );
  432. $number = floor($number / $divisor);
  433. $mask = substr_replace($mask,$blockValue, $offset, $size);
  434. }
  435. if ($number > 0) {
  436. $mask = substr_replace($mask, $number, $offset, 0);
  437. }
  438. $result = $mask;
  439. } else {
  440. $result = $number;
  441. }
  442. return $result;
  443. }
  444. private static function _formatAsFraction(&$value, &$format)
  445. {
  446. $sign = ($value < 0) ? '-' : '';
  447. $integerPart = floor(abs($value));
  448. $decimalPart = trim(fmod(abs($value),1),'0.');
  449. $decimalLength = strlen($decimalPart);
  450. $decimalDivisor = pow(10,$decimalLength);
  451. $GCD = self::GCD(array($decimalPart, $decimalDivisor));
  452. $adjustedDecimalPart = $decimalPart/$GCD;
  453. $adjustedDecimalDivisor = $decimalDivisor/$GCD;
  454. if ((strpos($format,'0') !== false) || (strpos($format,'#') !== false) || (substr($format,0,3) == '? ?')) {
  455. if ($integerPart == 0) {
  456. $integerPart = '';
  457. }
  458. $value = "$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
  459. } else {
  460. $adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
  461. $value = "$sign$adjustedDecimalPart/$adjustedDecimalDivisor";
  462. }
  463. }
  464. private static function GCD($params) {
  465. $returnValue = 1;
  466. $allValuesFactors = array();
  467. $flattenArr = self::flattenArray($params);
  468. foreach($flattenArr as $value) {
  469. if (!is_numeric($value)) {
  470. return '#VALUE!';
  471. } elseif ($value == 0) {
  472. continue;
  473. } elseif($value < 0) {
  474. return '#NULL!';
  475. }
  476. $myFactors = self::_factors($value);
  477. $myCountedFactors = array_count_values($myFactors);
  478. $allValuesFactors[] = $myCountedFactors;
  479. }
  480. $allValuesCount = count($allValuesFactors);
  481. if ($allValuesCount == 0) {
  482. return 0;
  483. }
  484. $mergedArray = $allValuesFactors[0];
  485. for ($i=1;$i < $allValuesCount; ++$i) {
  486. $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]);
  487. }
  488. $mergedArrayValues = count($mergedArray);
  489. if ($mergedArrayValues == 0) {
  490. return $returnValue;
  491. } elseif ($mergedArrayValues > 1) {
  492. foreach($mergedArray as $mergedKey => $mergedValue) {
  493. foreach($allValuesFactors as $highestPowerTest) {
  494. foreach($highestPowerTest as $testKey => $testValue) {
  495. if (($testKey == $mergedKey) && ($testValue < $mergedValue)) {
  496. $mergedArray[$mergedKey] = $testValue;
  497. $mergedValue = $testValue;
  498. }
  499. }
  500. }
  501. }
  502. $returnValue = 1;
  503. foreach($mergedArray as $key => $value) {
  504. $returnValue *= pow($key,$value);
  505. }
  506. return $returnValue;
  507. } else {
  508. $keys = array_keys($mergedArray);
  509. $key = $keys[0];
  510. $value = $mergedArray[$key];
  511. foreach($allValuesFactors as $testValue) {
  512. foreach($testValue as $mergedKey => $mergedValue) {
  513. if (($mergedKey == $key) && ($mergedValue < $value)) {
  514. $value = $mergedValue;
  515. }
  516. }
  517. }
  518. return pow($key,$value);
  519. }
  520. }
  521. private static function flattenArray($array) {
  522. if (!is_array($array)) {
  523. return (array) $array;
  524. }
  525. $arrayValues = array();
  526. foreach ($array as $value) {
  527. if (is_array($value)) {
  528. foreach ($value as $val) {
  529. if (is_array($val)) {
  530. foreach ($val as $v) {
  531. $arrayValues[] = $v;
  532. }
  533. } else {
  534. $arrayValues[] = $val;
  535. }
  536. }
  537. } else {
  538. $arrayValues[] = $value;
  539. }
  540. }
  541. return $arrayValues;
  542. }
  543. private static function _formatAsPercentage(&$value, &$format)
  544. {
  545. if ($format === '0%') {
  546. $value = round( (100 * $value), 0) . '%';
  547. } else {
  548. if (preg_match('/\.[#0]+/i', $format, $m)) {
  549. $s = substr($m[0], 0, 1) . (strlen($m[0]) - 1);
  550. $format = str_replace($m[0], $s, $format);
  551. }
  552. if (preg_match('/^[#0]+/', $format, $m)) {
  553. $format = str_replace($m[0], strlen($m[0]), $format);
  554. }
  555. $format = '%' . str_replace('%', 'f%%', $format);
  556. $value = sprintf($format, 100 * $value);
  557. }
  558. return $value;
  559. }
  560. /**
  561. * Search/replace values to convert Excel date/time format masks to PHP format masks
  562. *
  563. * @var array
  564. */
  565. private static $_dateFormatReplacements = array(
  566. // first remove escapes related to non-format characters
  567. '\\' => '',
  568. // 12-hour suffix
  569. 'am/pm' => 'A',
  570. // 4-digit year
  571. 'e' => 'Y',
  572. 'yyyy' => 'Y',
  573. // 2-digit year
  574. 'yy' => 'y',
  575. // first letter of month - no php equivalent
  576. 'mmmmm' => 'M',
  577. // full month name
  578. 'mmmm' => 'F',
  579. // short month name
  580. 'mmm' => 'M',
  581. // mm is minutes if time, but can also be month w/leading zero
  582. // so we try to identify times be the inclusion of a : separator in the mask
  583. // It isn't perfect, but the best way I know how
  584. ':mm' => ':i',
  585. 'mm:' => 'i:',
  586. 'mmi' => 'i',
  587. // month leading zero
  588. 'mm' => 'm',
  589. // month no leading zero
  590. 'm' => 'n',
  591. // full day of week name
  592. 'dddd' => 'l',
  593. // short day of week name
  594. 'ddd' => 'D',
  595. // days leading zero
  596. 'dd' => 'd',
  597. // days no leading zero
  598. 'd' => 'j',
  599. // seconds
  600. 'ss' => 's',
  601. // fractional seconds - no php equivalent
  602. '.s' => ''
  603. );
  604. /**
  605. * Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock)
  606. *
  607. * @var array
  608. */
  609. private static $_dateFormatReplacements24 = array(
  610. 'hh' => 'H',
  611. 'h' => 'G'
  612. );
  613. /**
  614. * Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock)
  615. *
  616. * @var array
  617. */
  618. private static $_dateFormatReplacements12 = array(
  619. 'hh' => 'h',
  620. 'h' => 'g'
  621. );
  622. private function _formatAsDate(&$value, &$format)
  623. {
  624. // dvc: convert Excel formats to PHP date formats
  625. // strip off first part containing e.g. [$-F800] or [$USD-409]
  626. // general syntax: [$<Currency string>-<language info>]
  627. // language info is in hexadecimal
  628. $format = preg_replace('/^(\[\$[A-Z]*-[0-9A-F]*\])/i', '', $format);
  629. // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case
  630. $format = strtolower($format);
  631. $format = strtr($format,self::$_dateFormatReplacements);
  632. if (strpos($format,'A') === false) { // 24-hour time format
  633. $format = strtr($format,self::$_dateFormatReplacements24);
  634. } else { // 12-hour time format
  635. $format = strtr($format,self::$_dateFormatReplacements12);
  636. }
  637. $dateTime = $this->ExcelToPHP($value);
  638. $days = floor($dateTime / 86400);
  639. $time = round((($dateTime / 86400) - $days) * 86400);
  640. $hours = round($time / 3600);
  641. $minutes = round($time / 60) - ($hours * 60);
  642. $seconds = round($time) - ($hours * 3600) - ($minutes * 60);
  643. $dateObj = date_create('1-Jan-1970+'.$days.' days');
  644. $dateObj->setTime($hours,$minutes,$seconds);
  645. return $dateObj->format($format);
  646. }
  647. private function ExcelToPHP($dateValue = 0) {
  648. if ($this->_excelBaseDate == 1900) {
  649. $my_excelBaseDate = 25569;
  650. // Adjust for the spurious 29-Feb-1900 (Day 60)
  651. if ($dateValue < 60) {
  652. --$my_excelBaseDate;
  653. }
  654. } else {
  655. $my_excelBaseDate = 24107;
  656. }
  657. // Perform conversion
  658. if ($dateValue >= 1) {
  659. $utcDays = $dateValue - $my_excelBaseDate;
  660. $returnValue = round($utcDays * 86400);
  661. if (($returnValue <= PHP_INT_MAX) && ($returnValue >= -PHP_INT_MAX)) {
  662. $returnValue = (integer) $returnValue;
  663. }
  664. } else {
  665. $hours = round($dateValue * 24);
  666. $mins = round($dateValue * 1440) - round($hours * 60);
  667. $secs = round($dateValue * 86400) - round($hours * 3600) - round($mins * 60);
  668. $returnValue = (integer) gmmktime($hours, $mins, $secs);
  669. }
  670. // Return
  671. return $returnValue;
  672. }
  673. /**
  674. * Read BOF
  675. */
  676. private function _readBof() {
  677. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  678. $recordData = substr($this->_data, $this->_pos + 4, $length);
  679. $this->_pos += 4 + $length; // move stream pointer to next record
  680. $substreamType = self::_GetInt2d($recordData, 2); // offset: 2; size: 2; type of the following data
  681. switch ($substreamType) {
  682. case self::XLS_WorkbookGlobals:
  683. $version = self::_GetInt2d($recordData, 0);
  684. if (($version != self::XLS_BIFF8) && ($version != self::XLS_BIFF7)) {
  685. die('Cannot read this Excel file. Version is too old.');
  686. }
  687. $this->_version = $version;
  688. break;
  689. case self::XLS_Worksheet:
  690. // do not use this version information for anything
  691. // it is unreliable (OpenOffice doc, 5.8), use only version information from the global stream
  692. break;
  693. default:
  694. // substream, e.g. chart. just skip the entire substream
  695. do {
  696. $code = self::_GetInt2d($this->_data, $this->_pos);
  697. $this->_readDefault();
  698. } while ($code != self::XLS_Type_EOF && $this->_pos < $this->_dataSize);
  699. break;
  700. }
  701. }
  702. /**
  703. * Read Sheet
  704. */
  705. private function _readSheet() {
  706. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  707. $recordData = substr($this->_data, $this->_pos + 4, $length);
  708. $rec_offset = self::_GetInt4d($this->_data, $this->_pos + 4); // offset: 0; size: 4; absolute stream position of the BOF record of the sheet
  709. $this->_pos += 4 + $length; // move stream pointer to next record
  710. // offset: 6; size: var; sheet name
  711. if ($this->_version == self::XLS_BIFF8) {
  712. $string = self::_readUnicodeStringShort(substr($recordData, 6));
  713. $rec_name = $string['value'];
  714. } elseif ($this->_version == self::XLS_BIFF7) {
  715. $string = self::_readByteStringShort(substr($recordData, 6));
  716. $rec_name = $string['value'];
  717. }
  718. $this->_sheets[] = array(
  719. 'name' => $rec_name,
  720. 'offset' => $rec_offset
  721. );
  722. }
  723. /**
  724. * Reads a general type of BIFF record. Does nothing except for moving stream pointer forward to next record.
  725. */
  726. private function _readDefault() {
  727. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  728. $this->_pos += 4 + $length; // move stream pointer to next record
  729. }
  730. /**
  731. * CODEPAGE
  732. *
  733. * This record stores the text encoding used to write byte
  734. * strings, stored as MS Windows code page identifier.
  735. */
  736. private function _readCodepage() {
  737. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  738. $recordData = substr($this->_data, $this->_pos + 4, $length);
  739. $this->_pos += 4 + $length;
  740. $codepage = self::_GetInt2d($recordData, 0);
  741. self::$_codepage = self::NumberToName($codepage);
  742. }
  743. /**
  744. * DATEMODE
  745. *
  746. * This record specifies the base date for displaying date values. All dates are stored as count of days past this base date.
  747. * In BIFF2-BIFF4 this record is part of the Calculation Settings Block. In BIFF5-BIFF8 it is stored in the Workbook Globals Substream.
  748. */
  749. private function _readDateMode() {
  750. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  751. $recordData = substr($this->_data, $this->_pos + 4, $length);
  752. $this->_pos += 4 + $length;
  753. if (ord($recordData{0}) == 1) {
  754. $this->_excelBaseDate = 1904;
  755. }
  756. else{
  757. $this->_excelBaseDate = 1900;
  758. }
  759. }
  760. /**
  761. * data format
  762. */
  763. private function _readFormat(){
  764. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  765. $recordData = substr($this->_data, $this->_pos + 4, $length);
  766. $this->_pos += 4 + $length;
  767. $indexCode = self::_GetInt2d($recordData, 0);
  768. if ($this->_version == self::XLS_BIFF8) {
  769. $string = str_replace('"', '', self::_readUnicodeStringLong(substr($recordData, 2)));
  770. } else {
  771. // BIFF7
  772. $string = self::_readByteStringShort(substr($recordData, 2));
  773. }
  774. $formatString = $string['value'];
  775. $this->formatRecords[$indexCode] = $formatString;
  776. }
  777. /**
  778. * XF - Extended Format
  779. *
  780. * This record contains formatting information for cells, rows, columns or styles.
  781. * According to http://support.microsoft.com/kb/147732 there are always at least 15 cell style XF and 1 cell XF.
  782. * Inspection of Excel files generated by MS Office Excel shows that XF records 0-14 are cell style XF and XF record 15 is a cell XF
  783. * We only read the first cell style XF and skip the remaining cell style XF records
  784. */
  785. private function _readXf() {
  786. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  787. $recordData = substr($this->_data, $this->_pos + 4, $length);
  788. $this->_pos += 4 + $length;
  789. $indexCode = self::_GetInt2d($recordData, 2);
  790. if (isset($this->formatRecords[$indexCode])) {
  791. $format = $this->formatRecords[$indexCode];
  792. }
  793. else if (isset($this->builtInFormats[$indexCode])) {
  794. $format = $this->builtInFormats[$indexCode];
  795. }
  796. else {
  797. $format = 'General';
  798. }
  799. $this->xfRecords[] = array(
  800. 'index' => $indexCode,
  801. 'format' => $format
  802. );
  803. }
  804. /**
  805. * SST - Shared String Table
  806. *
  807. * This record contains a list of all strings used anywherein the workbook. Each string occurs only once. The
  808. * workbook uses indexes into the list to reference the strings.
  809. **/
  810. private function _readSst() {
  811. $pos = 0; // offset within (spliced) record data
  812. $splicedRecordData = $this->_getSplicedRecordData(); // get spliced record data
  813. $recordData = $splicedRecordData['recordData'];
  814. $spliceOffsets = $splicedRecordData['spliceOffsets'];
  815. $pos += 4; // offset: 0; size: 4; total number of strings in the workbook
  816. $nm = self::_GetInt4d($recordData, 4); // offset: 4; size: 4; number of following strings ($nm)
  817. $pos += 4;
  818. for ($i = 0; $i < $nm; ++$i) { // loop through the Unicode strings (16-bit length)
  819. $numChars = self::_GetInt2d($recordData, $pos); // number of characters in the Unicode string
  820. $pos += 2;
  821. $optionFlags = ord($recordData{$pos}); // option flags
  822. ++$pos;
  823. $isCompressed = (($optionFlags & 0x01) == 0) ; // bit: 0; mask: 0x01; 0 = compressed; 1 = uncompressed
  824. $hasAsian = (($optionFlags & 0x04) != 0); // bit: 2; mask: 0x02; 0 = ordinary; 1 = Asian phonetic
  825. $hasRichText = (($optionFlags & 0x08) != 0); // bit: 3; mask: 0x03; 0 = ordinary; 1 = Rich-Text
  826. if ($hasRichText) {
  827. $formattingRuns = self::_GetInt2d($recordData, $pos); // number of Rich-Text formatting runs
  828. $pos += 2;
  829. }
  830. if ($hasAsian) {
  831. $extendedRunLength = self::_GetInt4d($recordData, $pos); // size of Asian phonetic setting
  832. $pos += 4;
  833. }
  834. $len = ($isCompressed) ? $numChars : $numChars * 2; // expected byte length of character array if not split
  835. foreach ($spliceOffsets as $spliceOffset) { // look up limit position
  836. if ($pos <= $spliceOffset) { // it can happen that the string is empty, therefore we need. <= and not just <
  837. $limitpos = $spliceOffset;
  838. break;
  839. }
  840. }
  841. if ($pos + $len <= $limitpos) {
  842. $retstr = substr($recordData, $pos, $len); // character array is not split between records
  843. $pos += $len;
  844. } else {
  845. $retstr = substr($recordData, $pos, $limitpos - $pos); // character array is split between records. first part of character array
  846. $bytesRead = $limitpos - $pos;
  847. $charsLeft = $numChars - (($isCompressed) ? $bytesRead : ($bytesRead / 2)); // remaining characters in Unicode string
  848. $pos = $limitpos;
  849. // keep reading the characters
  850. while ($charsLeft > 0) {
  851. // look up next limit position, in case the string span more than one continue record
  852. foreach ($spliceOffsets as $spliceOffset) {
  853. if ($pos < $spliceOffset) {
  854. $limitpos = $spliceOffset;
  855. break;
  856. }
  857. }
  858. // repeated option flags. OpenOffice.org documentation 5.21
  859. $option = ord($recordData{$pos});
  860. ++$pos;
  861. if ($isCompressed && ($option == 0)) {
  862. // 1st fragment compressed. this fragment compressed
  863. $len = min($charsLeft, $limitpos - $pos);
  864. $retstr .= substr($recordData, $pos, $len);
  865. $charsLeft -= $len;
  866. $isCompressed = true;
  867. } elseif (!$isCompressed && ($option != 0)) {
  868. // 1st fragment uncompressed. this fragment uncompressed
  869. $len = min($charsLeft * 2, $limitpos - $pos);
  870. $retstr .= substr($recordData, $pos, $len);
  871. $charsLeft -= $len / 2;
  872. $isCompressed = false;
  873. } elseif (!$isCompressed && ($option == 0)) {
  874. // 1st fragment uncompressed. this fragment compressed
  875. $len = min($charsLeft, $limitpos - $pos);
  876. for ($j = 0; $j < $len; ++$j) {
  877. $retstr .= $recordData{$pos + $j} . chr(0);
  878. }
  879. $charsLeft -= $len;
  880. $isCompressed = false;
  881. } else {
  882. // 1st fragment compressed. this fragment uncompressed
  883. $newstr = '';
  884. for ($j = 0; $j < strlen($retstr); ++$j) {
  885. $newstr .= $retstr[$j] . chr(0);
  886. }
  887. $retstr = $newstr;
  888. $len = min($charsLeft * 2, $limitpos - $pos);
  889. $retstr .= substr($recordData, $pos, $len);
  890. $charsLeft -= $len / 2;
  891. $isCompressed = false;
  892. }
  893. $pos += $len;
  894. }
  895. }
  896. $retstr = self::_encodeUTF16($retstr, $isCompressed); // convert to UTF-8
  897. $fmtRuns = array(); // read additional Rich-Text information, if any
  898. if ($hasRichText) {
  899. // list of formatting runs
  900. for ($j = 0; $j < $formattingRuns; ++$j) {
  901. $charPos = self::_GetInt2d($recordData, $pos + $j * 4); // first formatted character; zero-based
  902. $fontIndex = self::_GetInt2d($recordData, $pos + 2 + $j * 4); // index to font record
  903. $fmtRuns[] = array(
  904. 'charPos' => $charPos,
  905. 'fontIndex' => $fontIndex
  906. );
  907. }
  908. $pos += 4 * $formattingRuns;
  909. }
  910. // read additional Asian phonetics information, if any
  911. if ($hasAsian) {
  912. $pos += $extendedRunLength; // For Asian phonetic settings, we skip the extended string data
  913. }
  914. // store the shared sting
  915. $this->_sst[] = array(
  916. 'value' => $retstr,
  917. 'fmtRuns' => $fmtRuns
  918. );
  919. }
  920. }
  921. /**
  922. * Read RK record
  923. * This record represents a cell that contains an RK value (encoded integer or floating-point value). If a floating-point value
  924. * cannot be encoded to an RK value, a NUMBER record will be written. This record replaces the record INTEGER written in BIFF2.
  925. */
  926. private function _readRk() {
  927. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  928. $recordData = substr($this->_data, $this->_pos + 4, $length);
  929. $this->_pos += 4 + $length;
  930. $row = self::_GetInt2d($recordData, 0);
  931. $column = self::_GetInt2d($recordData, 2);
  932. $xfIndex = self::_GetInt2d($recordData, 4);
  933. $rknum = self::_GetInt4d($recordData, 6);
  934. $numValue = self::_GetIEEE754($rknum);
  935. // add cell
  936. $this->_addCell($row, $column, $numValue, $xfIndex);
  937. }
  938. /**
  939. * Read LABELSST record This record represents a cell that contains a string. It
  940. * replaces the LABEL record and RSTRING record used in BIFF2-BIFF5.
  941. */
  942. private function _readLabelSst() {
  943. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  944. $recordData = substr($this->_data, $this->_pos + 4, $length);
  945. $this->_pos += 4 + $length;
  946. $xfIndex = self::_GetInt2d($recordData, 4);
  947. $row = self::_GetInt2d($recordData, 0);
  948. $column = self::_GetInt2d($recordData, 2);
  949. // offset: 6; size: 4; index to SST record
  950. $index = self::_GetInt4d($recordData, 6);
  951. $this->_addCell($row, $column, $this->_sst[$index]['value'], $xfIndex);
  952. }
  953. /**
  954. * Read MULRK record
  955. * This record represents a cell range containing RK value cells. All cells are located in the same row.
  956. */
  957. private function _readMulRk() {
  958. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  959. $recordData = substr($this->_data, $this->_pos + 4, $length);
  960. $this->_pos += 4 + $length;
  961. $row = self::_GetInt2d($recordData, 0);
  962. $colFirst = self::_GetInt2d($recordData, 2);
  963. $colLast = self::_GetInt2d($recordData, $length - 2);
  964. $columns = $colLast - $colFirst + 1;
  965. // offset within record data
  966. $offset = 4;
  967. for ($i = 0; $i < $columns; ++$i) {
  968. $xfIndex = self::_GetInt2d($recordData, $offset);
  969. $numValue = self::_GetIEEE754(self::_GetInt4d($recordData, $offset + 2));
  970. $this->_addCell($row, $colFirst + $i, $numValue, $xfIndex);
  971. $offset += 6;
  972. }
  973. }
  974. /**
  975. * Read NUMBER record
  976. * This record represents a cell that contains a floating-point value.
  977. */
  978. private function _readNumber() {
  979. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  980. $recordData = substr($this->_data, $this->_pos + 4, $length);
  981. $this->_pos += 4 + $length;
  982. $row = self::_GetInt2d($recordData, 0);
  983. $column = self::_GetInt2d($recordData, 2);
  984. $xfIndex = self::_GetInt2d($recordData, 4);
  985. $numValue = self::_extractNumber(substr($recordData, 6, 8));
  986. $this->_addCell($row, $column, $numValue, $xfIndex);
  987. }
  988. /**
  989. * Read FORMULA record + perhaps a following STRING record if formula result is a string
  990. * This record contains the token array and the result of a formula cell.
  991. */
  992. private function _readFormula() {
  993. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  994. $recordData = substr($this->_data, $this->_pos + 4, $length);
  995. $this->_pos += 4 + $length;
  996. $xfIndex = self::_GetInt2d($recordData, 4);
  997. $row = self::_GetInt2d($recordData, 0);
  998. $column = self::_GetInt2d($recordData, 2);
  999. if ((ord($recordData{6}) == 0) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255)) {
  1000. $this->_preRow = $row;
  1001. $this->_preColumn = $column;
  1002. return false;
  1003. }
  1004. elseif ((ord($recordData{6}) == 1) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255)) {
  1005. // Boolean formula. Result is in +2; 0=false, 1=true
  1006. $value = (bool) ord($recordData{8});
  1007. }
  1008. elseif ((ord($recordData{6}) == 2) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255)) {
  1009. // Error formula. Error code is in +2
  1010. $value = self::_mapErrorCode(ord($recordData{8}));
  1011. }
  1012. elseif ((ord($recordData{6}) == 3) && (ord($recordData{12}) == 255) && (ord($recordData{13}) == 255)) {
  1013. // Formula result is a null string
  1014. $value = '';
  1015. }
  1016. else {
  1017. // forumla result is a number, first 14 bytes like _NUMBER record
  1018. $value = self::_extractNumber(substr($recordData, 6, 8));
  1019. }
  1020. $this->_addCell($row, $column, $value, $xfIndex);
  1021. }
  1022. /**
  1023. * Read a STRING record from current stream position and advance the stream pointer to next record
  1024. * This record is used for storing result from FORMULA record when it is a string, and it occurs directly after the FORMULA record
  1025. *
  1026. * @return string The string contents as UTF-8
  1027. */
  1028. private function _readString() {
  1029. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1030. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1031. $this->_pos += 4 + $length;
  1032. $xfIndex = self::_GetInt2d($recordData, 4);
  1033. if ($this->_version == self::XLS_BIFF8) {
  1034. $string = self::_readUnicodeStringLong($recordData);
  1035. $value = $string['value'];
  1036. } else {
  1037. $string = self::_readByteStringLong($recordData);
  1038. $value = $string['value'];
  1039. }
  1040. $this->_addCell($this->_preRow, $this->_preColumn, $value, $xfIndex);
  1041. }
  1042. /**
  1043. * Read BOOLERR record
  1044. * This record represents a Boolean value or error value cell.
  1045. */
  1046. private function _readBoolErr() {
  1047. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1048. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1049. $this->_pos += 4 + $length;
  1050. $xfIndex = self::_GetInt2d($recordData, 4);
  1051. $row = self::_GetInt2d($recordData, 0);
  1052. $column = self::_GetInt2d($recordData, 2);
  1053. // offset: 6; size: 1; the boolean value or error value
  1054. $boolErr = ord($recordData{6});
  1055. // offset: 7; size: 1; 0=boolean; 1=error
  1056. $isError = ord($recordData{7});
  1057. switch ($isError) {
  1058. case 0: // boolean
  1059. $value = (bool) $boolErr;
  1060. // add cell value
  1061. $this->_addCell($row, $column, $value, $xfIndex);
  1062. break;
  1063. case 1: // error type
  1064. $value = self::_mapErrorCode($boolErr);
  1065. // add cell value
  1066. $this->_addCell($row, $column, $value, $xfIndex);
  1067. break;
  1068. }
  1069. }
  1070. /**
  1071. * Read LABEL record
  1072. * This record represents a cell that contains a string. In BIFF8 it is usually replaced by the LABELSST record.
  1073. * Excel still uses this record, if it copies unformatted text cells to the clipboard.
  1074. */
  1075. private function _readLabel() {
  1076. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1077. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1078. $this->_pos += 4 + $length;
  1079. $xfIndex = self::_GetInt2d($recordData, 4);
  1080. $row = self::_GetInt2d($recordData, 0);
  1081. $column = self::_GetInt2d($recordData, 2);
  1082. if ($this->_version == self::XLS_BIFF8) {
  1083. $string = self::_readUnicodeStringLong(substr($recordData, 6));
  1084. $value = $string['value'];
  1085. } else {
  1086. $string = self::_readByteStringLong(substr($recordData, 6));
  1087. $value = $string['value'];
  1088. }
  1089. $this->_addCell($row, $column, $value, $xfIndex);
  1090. }
  1091. /**
  1092. * Read BLANK record
  1093. */
  1094. private function _readBlank() {
  1095. $length = self::_GetInt2d($this->_data, $this->_pos + 2);
  1096. $recordData = substr($this->_data, $this->_pos + 4, $length);
  1097. $this->_pos += 4 + $length;
  1098. $xfIndex = self::_GetInt2d($recordData, 4);
  1099. $row = self::_GetInt2d($recordData, 0);
  1100. $column = self::_GetInt2d($recordData, 2);
  1101. $this->_addCell($row, $column, '', $xfIndex);
  1102. }
  1103. /**
  1104. * Reads a record from current position in data stream and continues reading data as long as CONTINUE
  1105. * records are found. Splices the record data pieces and returns the combined string as if record data is in one piece.
  1106. * Moves to next current position in data stream to start of next record different from a CONtINUE record
  1107. *
  1108. * @return array
  1109. */
  1110. private function _getSplicedRecordData() {
  1111. $data = '';
  1112. $spliceOffsets = array();
  1113. $i = 0;
  1114. $spliceOffsets[0] = 0;
  1115. do {
  1116. ++$i;
  1117. $identifier = self::_GetInt2d($this->_data, $this->_pos); // offset: 0; size: 2; identifier
  1118. $length = self::_GetInt2d($this->_data, $this->_pos + 2); // offset: 2; size: 2; length
  1119. $data .= substr($this->_data, $this->_pos + 4, $length);
  1120. $spliceOffsets[$i] = $spliceOffsets[$i - 1] + $length;
  1121. $this->_pos += 4 + $length;
  1122. $nextIdentifier = self::_GetInt2d($this->_data, $this->_pos);
  1123. } while ($nextIdentifier == self::XLS_Type_CONTINUE);
  1124. $splicedData = array(
  1125. 'recordData' => $data,
  1126. 'spliceOffsets' => $spliceOffsets,
  1127. );
  1128. return $splicedData;
  1129. }
  1130. /**
  1131. * Read byte string (16-bit string length)
  1132. * OpenOffice documentation: 2.5.2
  1133. *
  1134. * @param string $subData
  1135. * @return array
  1136. */
  1137. private static function _readByteStringLong($subData) {
  1138. // offset: 0; size: 2; length of the string (character count)
  1139. $ln = self::_GetInt2d($subData, 0);
  1140. // offset: 2: size: var; character array (8-bit characters)
  1141. $value = self::_decodeCodepage(substr($subData, 2));
  1142. //return $string;
  1143. return array(
  1144. 'value' => $value,
  1145. 'size' => 2 + $ln, // size in bytes of data structure
  1146. );
  1147. }
  1148. /**
  1149. * Map error code, e.g. '#N/A'
  1150. *
  1151. * @param int $subData
  1152. * @return string
  1153. */
  1154. private static function _mapErrorCode($subData) {
  1155. switch ($subData) {
  1156. case 0x00: return '#NULL!'; break;
  1157. case 0x07: return '#DIV/0!'; break;
  1158. case 0x0F: return '#VALUE!'; break;
  1159. case 0x17: return '#REF!'; break;
  1160. case 0x1D: return '#NAME?'; break;
  1161. case 0x24: return '#NUM!'; break;
  1162. case 0x2A: return '#N/A'; break;
  1163. default: return false;
  1164. }
  1165. }
  1166. /**
  1167. * Convert Microsoft Code Page Identifier to Code Page Name which iconv
  1168. * and mbstring understands
  1169. *
  1170. * @param integer $codePage Microsoft Code Page Indentifier
  1171. * @return string Code Page Name
  1172. */
  1173. private static function NumberToName($codePage = 1252) {
  1174. switch ($codePage) {
  1175. case 367: return 'ASCII'; break; // ASCII
  1176. case 437: return 'CP437'; break; // OEM US
  1177. //case 720: throw new PHPExcel_Exception('Code page 720 not supported.'); break; // OEM Arabic
  1178. case 737: return 'CP737'; break; // OEM Greek
  1179. case 775: return 'CP775'; break; // OEM Baltic
  1180. case 850: return 'CP850'; break; // OEM Latin I
  1181. case 852: return 'CP852'; break; // OEM Latin II (Central European)
  1182. case 855: return 'CP855'; break; // OEM Cyrillic
  1183. case 857: return 'CP857'; break; // OEM Turkish
  1184. case 858: return 'CP858'; break; // OEM Multilingual Latin I with Euro
  1185. case 860: return 'CP860'; break; // OEM Portugese
  1186. case 861: return 'CP861'; break; // OEM Icelandic
  1187. case 862: return 'CP862'; break; // OEM Hebrew
  1188. case 863: return 'CP863'; break; // OEM Canadian (French)
  1189. case 864: return 'CP864'; break; // OEM Arabic
  1190. case 865: return 'CP865'; break; // OEM Nordic
  1191. case 866: return 'CP866'; break; // OEM Cyrillic (Russian)
  1192. case 869: return 'CP869'; break; // OEM Greek (Modern)
  1193. case 874: return 'CP874'; break; // ANSI Thai
  1194. case 932: return 'CP932'; break; // ANSI Japanese Shift-JIS
  1195. case 936: return 'CP936'; break; // ANSI Chinese Simplified GBK
  1196. case 949: return 'CP949'; break; // ANSI Korean (Wansung)
  1197. case 950: return 'CP950'; break; // ANSI Chinese Traditional BIG5
  1198. case 1200: return 'UTF-16LE'; break; // UTF-16 (BIFF8)
  1199. case 1250: return 'CP1250'; break; // ANSI Latin II (Central European)
  1200. case 1251: return 'CP1251'; break; // ANSI Cyrillic
  1201. case 0: // CodePage is not always correctly set when the xls file was saved by Apple's Numbers program
  1202. case 1252: return 'CP1252'; break; // ANSI Latin I (BIFF4-BIFF7)
  1203. case 1253: return 'CP1253'; break; // ANSI Greek
  1204. case 1254: return 'CP1254'; break; // ANSI Turkish
  1205. case 1255: return 'CP1255'; break; // ANSI Hebrew
  1206. case 1256: return 'CP1256'; break; // ANSI Arabic
  1207. case 1257: return 'CP1257'; break; // ANSI Baltic
  1208. case 1258: return 'CP1258'; break; // ANSI Vietnamese
  1209. case 1361: return 'CP1361'; break; // ANSI Korean (Johab)
  1210. case 10000: return 'MAC'; break; // Apple Roman
  1211. case 10006: return 'MACGREEK'; break; // Macintosh Greek
  1212. case 10007: return 'MACCYRILLIC'; break; // Macintosh Cyrillic
  1213. case 10008: return 'CP936'; break; // Macintosh - Simplified Chinese (GB 2312)
  1214. case 10029: return 'MACCENTRALEUROPE'; break; // Macintosh Central Europe
  1215. case 10079: return 'MACICELAND'; break; // Macintosh Icelandic
  1216. case 10081: return 'MACTURKISH'; break; // Macintosh Turkish
  1217. case 32768: return 'MAC'; break; // Apple Roman
  1218. //case 32769: throw new PHPExcel_Exception('Code page 32769 not supported.'); break; // ANSI Latin I (BIFF2-BIFF3)
  1219. case 65000: return 'UTF-7'; break; // Unicode (UTF-7)
  1220. case 65001: return 'UTF-8'; break; // Unicode (UTF-8)
  1221. default: return 'UTF-8'; break;
  1222. }
  1223. }
  1224. /**
  1225. * String from columnindex
  1226. *
  1227. * @param int $pColumnIndex
  1228. * @return string
  1229. */
  1230. private static function _stringFromColumnIndex($pColumnIndex = 0) {
  1231. static $_indexCache = array();
  1232. if ( ! isset($_indexCache[$pColumnIndex])) {
  1233. if ($pColumnIndex < 26) {
  1234. $_indexCache[$pColumnIndex] = chr(65 + $pColumnIndex);
  1235. } elseif ($pColumnIndex < 702) {
  1236. $_indexCache[$pColumnIndex] = chr(64 + ($pColumnIndex / 26)) . chr(65 + $pColumnIndex % 26);
  1237. } else {
  1238. $_indexCache[$pColumnIndex] = chr(64 + (($pColumnIndex - 26) / 676)) . chr(65 + ((($pColumnIndex - 26) % 676) / 26)) . chr(65 + $pColumnIndex % 26);
  1239. }
  1240. }
  1241. return $_indexCache[$pColumnIndex];
  1242. }
  1243. /**
  1244. * Extracts an Excel Unicode short string (8-bit string length)
  1245. * OpenOffice documentation: 2.5.3
  1246. * function will automatically find out where the Unicode string ends.
  1247. *
  1248. * @param string $subData
  1249. * @return array
  1250. */
  1251. private static function _readUnicodeStringShort($subData) {
  1252. $characterCount = ord($subData[0]); // offset: 0: size: 1; length of the string (character count)
  1253. $string = self::_readUnicodeString(substr($subData, 1), $characterCount);
  1254. $string['size'] += 1; // add 1 for the string length
  1255. return $string;
  1256. }
  1257. /**
  1258. * Read byte string (8-bit string length)
  1259. * OpenOffice documentation: 2.5.2
  1260. *
  1261. * @param string $subData
  1262. * @return array
  1263. */
  1264. private static function _readByteStringShort($subData) {
  1265. $ln = ord($subData[0]); // offset: 0; size: 1; length of the string (character count)
  1266. $value = self::_decodeCodepage(substr($subData, 1, $ln)); // offset: 1: size: var; character array (8-bit characters)
  1267. return array(
  1268. 'value' => $value,
  1269. 'size' => 1 + $ln, // size in bytes of data structure
  1270. );
  1271. }
  1272. /**
  1273. * Extracts an Excel Unicode long string (16-bit string length)
  1274. * OpenOffice documentation: 2.5.3. this function is under construction, needs to support rich text, and Asian phonetic settings
  1275. *
  1276. * @param string $subData
  1277. * @return array
  1278. */
  1279. private static function _readUnicodeStringLong($subData) {
  1280. $value = '';
  1281. // offset: 0: size: 2; length of the string (character count)
  1282. $characterCount = self::_GetInt2d($subData, 0);
  1283. $string = self::_readUnicodeString(substr($subData, 2), $characterCount);
  1284. // add 2 for the string length
  1285. $string['size'] += 2;
  1286. return $string;
  1287. }
  1288. /**
  1289. * Read Unicode string with no string length field, but with known character count
  1290. * this function is under construction, needs to support rich text, and Asian phonetic settings
  1291. * OpenOffice.org's Documentation of the Microsoft Excel File Format, section 2.5.3
  1292. *
  1293. * @param string $subData
  1294. * @param int $characterCount
  1295. * @return array
  1296. */
  1297. private static function _readUnicodeString($subData, $characterCount) {
  1298. $isCompressed = !((0x01 & ord($subData[0])) >> 0); // bit: 0; mask: 0x01; character compression (0 = compressed 8-bit, 1 = uncompressed 16-bit)
  1299. $hasAsian = (0x04) & ord($subData[0]) >> 2; // bit: 2; mask: 0x04; Asian phonetic settings
  1300. $hasRichText = (0x08) & ord($subData[0]) >> 3; // bit: 3; mask: 0x08; Rich-Text settings
  1301. // offset: 1: size: var; character array
  1302. // this offset assumes richtext and Asian phonetic settings are off which is generally wrong
  1303. // needs to be fixed
  1304. $value = self::_encodeUTF16(substr($subData, 1, $isCompressed ? $characterCount : 2 * $characterCount), $isCompressed);
  1305. return array(
  1306. 'value' => $value,
  1307. 'size' => $isCompressed ? 1 + $characterCount : 1 + 2 * $characterCount, // the size in bytes including the option flags
  1308. );
  1309. }
  1310. /**
  1311. * Get UTF-8 string from (compressed or uncompressed) UTF-16 string
  1312. *
  1313. * @param string $string
  1314. * @param bool $compressed
  1315. * @return string
  1316. */
  1317. private static function _encodeUTF16($string, $compressed = '') {
  1318. if ($compressed) {
  1319. $string = self::_uncompressByteString($string);
  1320. }
  1321. return mb_convert_encoding($string, 'UTF-8', 'UTF-16LE');
  1322. }
  1323. /**
  1324. * Convert string to UTF-8. Only used for BIFF5.
  1325. *
  1326. * @param string $string
  1327. * @return string
  1328. */
  1329. private static function _decodeCodepage($string) {
  1330. return mb_convert_encoding($string, 'UTF-8', self::$_codepage);
  1331. }
  1332. /**
  1333. * Convert UTF-16 string in compressed notation to uncompressed form. Only used for BIFF8.
  1334. *
  1335. * @param string $string
  1336. * @return string
  1337. */
  1338. private static function _uncompressByteString($string) {
  1339. $uncompressedString = '';
  1340. $strLen = strlen($string);
  1341. for ($i = 0; $i < $strLen; ++$i) {
  1342. $uncompressedString .= $string[$i] . "\0";
  1343. }
  1344. return $uncompressedString;
  1345. }
  1346. /**
  1347. * Read 16-bit unsigned integer
  1348. *
  1349. * @param string $data
  1350. * @param int $pos
  1351. * @return int
  1352. */
  1353. private static function _GetInt2d($data, $pos) {
  1354. return ord($data[$pos]) | (ord($data[$pos+1]) << 8);
  1355. }
  1356. /**
  1357. * Read 32-bit signed integer
  1358. * FIX: represent numbers correctly on 64-bit system. Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  1359. * http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  1360. *
  1361. * @param string $data
  1362. * @param int $pos
  1363. * @return int
  1364. */
  1365. private static function _GetInt4d($data, $pos) {
  1366. $_or_24 = ord($data[$pos + 3]);
  1367. if ($_or_24 >= 128) {
  1368. $_ord_24 = -abs((256 - $_or_24) << 24); // negative number
  1369. } else {
  1370. $_ord_24 = ($_or_24 & 127) << 24;
  1371. }
  1372. return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | $_ord_24;
  1373. }
  1374. /**
  1375. * Reads first 8 bytes of a string and return IEEE 754 float
  1376. *
  1377. * @param string $data Binary string that is at least 8 bytes long
  1378. * @return float
  1379. */
  1380. private static function _extractNumber($data) {
  1381. $rknumhigh = self::_GetInt4d($data, 4);
  1382. $rknumlow = self::_GetInt4d($data, 0);
  1383. $sign = ($rknumhigh & 0x80000000) >> 31;
  1384. $exp = (($rknumhigh & 0x7ff00000) >> 20) - 1023;
  1385. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  1386. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  1387. $mantissalow2 = ($rknumlow & 0x7fffffff);
  1388. $value = $mantissa / pow( 2 , (20 - $exp));
  1389. if ($mantissalow1 != 0) {
  1390. $value += 1 / pow (2 , (21 - $exp));
  1391. }
  1392. $value += $mantissalow2 / pow (2 , (52 - $exp));
  1393. if ($sign) {
  1394. $value *= -1;
  1395. }
  1396. return $value;
  1397. }
  1398. private static function _GetIEEE754($rknum) {
  1399. if (($rknum & 0x02) != 0) {
  1400. $value = $rknum >> 2;
  1401. } else {
  1402. // changes by mmp, info on IEEE754 encoding from
  1403. // research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  1404. // The RK format calls for using only the most significant 30 bits
  1405. // of the 64 bit floating point value. The other 34 bits are assumed
  1406. // to be 0 so we use the upper 30 bits of $rknum as follows...
  1407. $sign = ($rknum & 0x80000000) >> 31;
  1408. $exp = ($rknum & 0x7ff00000) >> 20;
  1409. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  1410. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1411. if ($sign) {
  1412. $value = -1 * $value;
  1413. }
  1414. //end of changes by mmp
  1415. }
  1416. if (($rknum & 0x01) != 0) {
  1417. $value /= 100;
  1418. }
  1419. return $value;
  1420. }
  1421. }