入到数据库的需要,php-excel-reader可以很轻松的使用它读取excel文件,本文将详细介绍,需要了解的朋友可以参考下

 

php开发中肯定会遇到将excel文件内容导入到数据库的需要,php-excel-reader是一个读取excel的类,可以很轻松的使用它读取excel文件非常方便。

php-excel-reader下载地址: http://www.jb51.net/codes/67223.html

我下载的是php-excel-reader-2.21版本,使用的时候还遇到几个小问题,后面再细说,先奉上php实例:

我使用的excel如下图:

php代码如下:

复制代码 代码如下:

  1. <?php
  2. /*by www.phpddt.com*/
  3. header("Content-Type:text/html;charset=utf-8");
  4. require_once 'excel_reader2.php';
  5. //创建对象
  6. $data = new Spreadsheet_Excel_Reader();
  7. //设置文本输出编码
  8. $data->setOutputEncoding('UTF-8');
  9. //读取Excel文件
  10. $data->read("example.xls");
  11. //$data->sheets[0]['numRows']为Excel行数
  12. for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
  13. //$data->sheets[0]['numCols']为Excel列数
  14. for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
  15. //显示每个单元格内容
  16. echo $data->sheets[0]['cells'][$i][$j].' ';
  17. }
  18. echo '<br>';
  19. }
  20. ?>

读取结果截图如下

再来说说这个类的小问题:

(1)出现Deprecated: Function split() is deprecated in 。。。错误

解决:将excel_reader2.php源码中split改为explode,详情点击php中explode与split的区别介绍

(2)出现Deprecated: Assigning the return value of new by reference is deprecated in错误

解决:将excel_reader2.php源码中$this->_ole =& new OLERead()中 &去掉,因为php5.3中废除了=& 符号直接用=引用

(3)乱码问题解决:

构造函数是function
Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding=''),
它默认的编码是utf-8,如果不指定,可能会出现乱码问题,可通过$data->setOutputEncoding('GBK');指定,还有
如果你使用dump()函数,dump()函数将excel内容一html格式输出,使用htmlentities将字符转化为html的,它默认使用
ISO8559-1编码的,所以你要将
excel_reader2.php源码中 htmlentities($val)函数改为
htmlentities($val,ENT_COMPAT,"GB2312");才行。

最后来说说,php-excel-reader操作excel中的两个重要的方法

1.dump(),它可以将excel内容以html格式输出:

echo $data->dump(true,true);

2.将excel数据存入数组中,使用$data->sheets,打印下如下:

复制代码 代码如下:

  1. Array
  2. (
  3.   [0] => Array
  4.     (
  5.       maxrow] => 0
  6.       [maxcol] => 0
  7.       [numRows] => 5
  8.       [numCols] => 4
  9.       [cells] => Array
  10.         (
  11.           [1] => Array
  12.             (
  13.               [1] => 编号
  14.               [2] => 姓名
  15.               [3] => 年龄
  16.               [4] => 学号
  17.             )
  18.           [2] => Array
  19.             (
  20.               [1] => 1
  21. 21               [2] => 小红
  22. 22               [3] => 22
  23.               [4] => a1000
  24. 24             )
  25.           [3] => Array
  26.             (
  27.               [1] => 2
  28.               [2] => 小王
  29.               [3] => 33
  30.               [4] => a1001
  31. 31             )
  32.           [4] => Array
  33.             (
  34.               [1] => 3
  35.               [2] => 小黑
  36.               [3] => 44
  37. 37               [4] => a1002
  38. 38             )
  39.           )   [cellsInfo] => Array
  40.     (
  41.       [1] => Array
  42.         (
  43.           [1] => Array
  44.             (
  45.               [xfIndex] => 15
  46. 52              )
  47.           [2] => Array
  48.             (
  49. 55               [xfIndex] => 15
  50.             )
  51.           [3] => Array
  52.             (
  53.               [xfIndex] => 15
  54.             )
  55.           [4] => Array
  56.             (
  57.               [xfIndex] => 15
  58.             )
  59. 65          )
  60.       [2] => Array
  61.         (
  62.           [1] => Array
  63. 69             (
  64.               [string] => 1
  65.               [raw] => 1
  66. 72               [rectype] => unknown
  67.               [format] => %s
  68.               [formatIndex] => 0
  69.               [fontIndex] => 0
  70.               [formatColor] =>
  71.               [xfIndex] => 15
  72. 78             )
  73.           [2] => Array
  74.             (
  75.               [xfIndex] => 15
  76.             )
  77.           [3] => Array
  78.             (
  79.               [string] => 22
  80.               [raw] => 22
  81.               [rectype] => unknown
  82.               [format] => %s
  83.               [formatIndex] => 0
  84.               [fontIndex] => 0
  85.               [formatColor] =>
  86.               [xfIndex] => 15
  87. 93             )
  88.           [4] => Array
  89.             (
  90.               [xfIndex] => 15
  91.             )
  92.          )
  93.       [3] => Array
  94.          (
  95.         ...  ...//剩下的部分可以自己打印出来看看

下面的read_php的类是改过上文提到过的问题的代码,可以直接使用,不再需要修改

  1. <?php
  2. /**
  3. * A class for reading Microsoft Excel (97/2003) Spreadsheets.
  4. *
  5. * Version 2.21
  6. *
  7. * Enhanced and maintained by Matt Kruse < http://mattkruse.com >
  8. * Maintained at http://code.google.com/p/php-excel-reader/
  9. *
  10. * Format parsing and MUCH more contributed by:
  11. * Matt Roxburgh < http://www.roxburgh.me.uk >
  12. *
  13. * DOCUMENTATION
  14. * =============
  15. * http://code.google.com/p/php-excel-reader/wiki/Documentation
  16. *
  17. * CHANGE LOG
  18. * ==========
  19. * http://code.google.com/p/php-excel-reader/wiki/ChangeHistory
  20. *
  21. * DISCUSSION/SUPPORT
  22. * ==================
  23. * http://groups.google.com/group/php-excel-reader-discuss/topics
  24. *
  25. * --------------------------------------------------------------------------
  26. *
  27. * Originally developed by Vadim Tkachenko under the name PHPExcelReader.
  28. * (http://sourceforge.net/projects/phpexcelreader)
  29. * Based on the Java version by Andy Khan (http://www.andykhan.com). Now
  30. * maintained by David Sanders. Reads only Biff 7 and Biff 8 formats.
  31. *
  32. * PHP versions 4 and 5
  33. *
  34. * LICENSE: This source file is subject to version 3.0 of the PHP license
  35. * that is available through the world-wide-web at the following URI:
  36. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  37. * the PHP License and are unable to obtain it through the web, please
  38. * send a note to license@php.net so we can mail you a copy immediately.
  39. *
  40. * @category Spreadsheet
  41. * @package Spreadsheet_Excel_Reader
  42. * @author Vadim Tkachenko <vt@apachephp.com>
  43. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  44. * @version CVS: $Id: reader.php 19 2007-03-13 12:42:41Z shangxiao $
  45. * @link http://pear.php.net/package/Spreadsheet_Excel_Reader
  46. * @see OLE, Spreadsheet_Excel_Writer
  47. * --------------------------------------------------------------------------
  48. */
  49.  
  50. define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
  51. define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
  52. define('ROOT_START_BLOCK_POS', 0x30);
  53. define('BIG_BLOCK_SIZE', 0x200);
  54. define('SMALL_BLOCK_SIZE', 0x40);
  55. define('EXTENSION_BLOCK_POS', 0x44);
  56. define('NUM_EXTENSION_BLOCK_POS', 0x48);
  57. define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
  58. define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
  59. define('SMALL_BLOCK_THRESHOLD', 0x1000);
  60. // property storage offsets
  61. define('SIZE_OF_NAME_POS', 0x40);
  62. define('TYPE_POS', 0x42);
  63. define('START_BLOCK_POS', 0x74);
  64. define('SIZE_POS', 0x78);
  65. define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));
  66.  
  67. function GetInt4d($data, $pos) {
  68. $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  69. if ($value>=4294967294) {
  70. $value=-2;
  71. }
  72. return $value;
  73. }
  74.  
  75. // http://uk.php.net/manual/en/function.getdate.php
  76. function gmgetdate($ts = null){
  77. $k = array('seconds','minutes','hours','mday','wday','mon','year','yday','weekday','month',0);
  78. return(array_comb($k,explode(":",gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts))));
  79. }
  80.  
  81. // Added for PHP4 compatibility
  82. function array_comb($array1, $array2) {
  83. $out = array();
  84. foreach ($array1 as $key => $value) {
  85. $out[$value] = $array2[$key];
  86. }
  87. return $out;
  88. }
  89.  
  90. function v($data,$pos) {
  91. return ord($data[$pos]) | ord($data[$pos+1])<<8;
  92. }
  93.  
  94. class OLERead {
  95. var $data = '';
  96. function OLERead(){ }
  97.  
  98. function read($sFileName){
  99. // check if file exist and is readable (Darko Miljanovic)
  100. if(!is_readable($sFileName)) {
  101. $this->error = 1;
  102. return false;
  103. }
  104. $this->data = @file_get_contents($sFileName);
  105. if (!$this->data) {
  106. $this->error = 1;
  107. return false;
  108. }
  109. if (substr($this->data, 0, 8) != IDENTIFIER_OLE) {
  110. $this->error = 1;
  111. return false;
  112. }
  113. $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  114. $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
  115. $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS);
  116. $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS);
  117. $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);
  118.  
  119. $bigBlockDepotBlocks = array();
  120. $pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
  121. $bbdBlocks = $this->numBigBlockDepotBlocks;
  122. if ($this->numExtensionBlocks != 0) {
  123. $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
  124. }
  125.  
  126. for ($i = 0; $i < $bbdBlocks; $i++) {
  127. $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
  128. $pos += 4;
  129. }
  130.  
  131. for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
  132. $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
  133. $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);
  134.  
  135. for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
  136. $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
  137. $pos += 4;
  138. }
  139.  
  140. $bbdBlocks += $blocksToRead;
  141. if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
  142. $this->extensionBlock = GetInt4d($this->data, $pos);
  143. }
  144. }
  145.  
  146. // readBigBlockDepot
  147. $pos = 0;
  148. $index = 0;
  149. $this->bigBlockChain = array();
  150.  
  151. for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
  152. $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
  153. //echo "pos = $pos";
  154. for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
  155. $this->bigBlockChain[$index] = GetInt4d($this->data, $pos);
  156. $pos += 4 ;
  157. $index++;
  158. }
  159. }
  160.  
  161. // readSmallBlockDepot();
  162. $pos = 0;
  163. $index = 0;
  164. $sbdBlock = $this->sbdStartBlock;
  165. $this->smallBlockChain = array();
  166.  
  167. while ($sbdBlock != -2) {
  168. $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;
  169. for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
  170. $this->smallBlockChain[$index] = GetInt4d($this->data, $pos);
  171. $pos += 4;
  172. $index++;
  173. }
  174. $sbdBlock = $this->bigBlockChain[$sbdBlock];
  175. }
  176.  
  177. // readData(rootStartBlock)
  178. $block = $this->rootStartBlock;
  179. $pos = 0;
  180. $this->entry = $this->__readData($block);
  181. $this->__readPropertySets();
  182. }
  183.  
  184. function __readData($bl) {
  185. $block = $bl;
  186. $pos = 0;
  187. $data = '';
  188. while ($block != -2) {
  189. $pos = ($block + 1) * BIG_BLOCK_SIZE;
  190. $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
  191. $block = $this->bigBlockChain[$block];
  192. }
  193. return $data;
  194. }
  195.  
  196. function __readPropertySets(){
  197. $offset = 0;
  198. while ($offset < strlen($this->entry)) {
  199. $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);
  200. $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8);
  201. $type = ord($d[TYPE_POS]);
  202. $startBlock = GetInt4d($d, START_BLOCK_POS);
  203. $size = GetInt4d($d, SIZE_POS);
  204. $name = '';
  205. for ($i = 0; $i < $nameSize ; $i++) {
  206. $name .= $d[$i];
  207. }
  208. $name = str_replace("\x00", "", $name);
  209. $this->props[] = array (
  210. 'name' => $name,
  211. 'type' => $type,
  212. 'startBlock' => $startBlock,
  213. 'size' => $size);
  214. if ((strtolower($name) == "workbook") || ( strtolower($name) == "book")) {
  215. $this->wrkbook = count($this->props) - 1;
  216. }
  217. if ($name == "Root Entry") {
  218. $this->rootentry = count($this->props) - 1;
  219. }
  220. $offset += PROPERTY_STORAGE_BLOCK_SIZE;
  221. }
  222.  
  223. }
  224.  
  225. function getWorkBook(){
  226. if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
  227. $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);
  228. $streamData = '';
  229. $block = $this->props[$this->wrkbook]['startBlock'];
  230. $pos = 0;
  231. while ($block != -2) {
  232. $pos = $block * SMALL_BLOCK_SIZE;
  233. $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);
  234. $block = $this->smallBlockChain[$block];
  235. }
  236. return $streamData;
  237. }else{
  238. $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
  239. if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
  240. $numBlocks++;
  241. }
  242.  
  243. if ($numBlocks == 0) return '';
  244. $streamData = '';
  245. $block = $this->props[$this->wrkbook]['startBlock'];
  246. $pos = 0;
  247. while ($block != -2) {
  248. $pos = ($block + 1) * BIG_BLOCK_SIZE;
  249. $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
  250. $block = $this->bigBlockChain[$block];
  251. }
  252. return $streamData;
  253. }
  254. }
  255.  
  256. }
  257.  
  258. define('SPREADSHEET_EXCEL_READER_BIFF8', 0x600);
  259. define('SPREADSHEET_EXCEL_READER_BIFF7', 0x500);
  260. define('SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS', 0x5);
  261. define('SPREADSHEET_EXCEL_READER_WORKSHEET', 0x10);
  262. define('SPREADSHEET_EXCEL_READER_TYPE_BOF', 0x809);
  263. define('SPREADSHEET_EXCEL_READER_TYPE_EOF', 0x0a);
  264. define('SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET', 0x85);
  265. define('SPREADSHEET_EXCEL_READER_TYPE_DIMENSION', 0x200);
  266. define('SPREADSHEET_EXCEL_READER_TYPE_ROW', 0x208);
  267. define('SPREADSHEET_EXCEL_READER_TYPE_DBCELL', 0xd7);
  268. define('SPREADSHEET_EXCEL_READER_TYPE_FILEPASS', 0x2f);
  269. define('SPREADSHEET_EXCEL_READER_TYPE_NOTE', 0x1c);
  270. define('SPREADSHEET_EXCEL_READER_TYPE_TXO', 0x1b6);
  271. define('SPREADSHEET_EXCEL_READER_TYPE_RK', 0x7e);
  272. define('SPREADSHEET_EXCEL_READER_TYPE_RK2', 0x27e);
  273. define('SPREADSHEET_EXCEL_READER_TYPE_MULRK', 0xbd);
  274. define('SPREADSHEET_EXCEL_READER_TYPE_MULBLANK', 0xbe);
  275. define('SPREADSHEET_EXCEL_READER_TYPE_INDEX', 0x20b);
  276. define('SPREADSHEET_EXCEL_READER_TYPE_SST', 0xfc);
  277. define('SPREADSHEET_EXCEL_READER_TYPE_EXTSST', 0xff);
  278. define('SPREADSHEET_EXCEL_READER_TYPE_CONTINUE', 0x3c);
  279. define('SPREADSHEET_EXCEL_READER_TYPE_LABEL', 0x204);
  280. define('SPREADSHEET_EXCEL_READER_TYPE_LABELSST', 0xfd);
  281. define('SPREADSHEET_EXCEL_READER_TYPE_NUMBER', 0x203);
  282. define('SPREADSHEET_EXCEL_READER_TYPE_NAME', 0x18);
  283. define('SPREADSHEET_EXCEL_READER_TYPE_ARRAY', 0x221);
  284. define('SPREADSHEET_EXCEL_READER_TYPE_STRING', 0x207);
  285. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA', 0x406);
  286. define('SPREADSHEET_EXCEL_READER_TYPE_FORMULA2', 0x6);
  287. define('SPREADSHEET_EXCEL_READER_TYPE_FORMAT', 0x41e);
  288. define('SPREADSHEET_EXCEL_READER_TYPE_XF', 0xe0);
  289. define('SPREADSHEET_EXCEL_READER_TYPE_BOOLERR', 0x205);
  290. define('SPREADSHEET_EXCEL_READER_TYPE_FONT', 0x0031);
  291. define('SPREADSHEET_EXCEL_READER_TYPE_PALETTE', 0x0092);
  292. define('SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN', 0xffff);
  293. define('SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22);
  294. define('SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS', 0xE5);
  295. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS' , 25569);
  296. define('SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107);
  297. define('SPREADSHEET_EXCEL_READER_MSINADAY', 86400);
  298. define('SPREADSHEET_EXCEL_READER_TYPE_HYPER', 0x01b8);
  299. define('SPREADSHEET_EXCEL_READER_TYPE_COLINFO', 0x7d);
  300. define('SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH', 0x55);
  301. define('SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH', 0x99);
  302. define('SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', "%s");
  303.  
  304. /*
  305. * Main Class
  306. */
  307. class Spreadsheet_Excel_Reader {
  308.  
  309. // MK: Added to make data retrieval easier
  310. var $colnames = array();
  311. var $colindexes = array();
  312. var $standardColWidth = 0;
  313. var $defaultColWidth = 0;
  314.  
  315. function myHex($d) {
  316. if ($d < 16) return "0" . dechex($d);
  317. return dechex($d);
  318. }
  319.  
  320. function dumpHexData($data, $pos, $length) {
  321. $info = "";
  322. for ($i = 0; $i <= $length; $i++) {
  323. $info .= ($i==0?"":" ") . $this->myHex(ord($data[$pos + $i])) . (ord($data[$pos + $i])>31? "[" . $data[$pos + $i] . "]":'');
  324. }
  325. return $info;
  326. }
  327.  
  328. function getCol($col) {
  329. if (is_string($col)) {
  330. $col = strtolower($col);
  331. if (array_key_exists($col,$this->colnames)) {
  332. $col = $this->colnames[$col];
  333. }
  334. }
  335. return $col;
  336. }
  337.  
  338. // PUBLIC API FUNCTIONS
  339. // --------------------
  340.  
  341. function val($row,$col,$sheet=0) {
  342. $col = $this->getCol($col);
  343. if (array_key_exists($row,$this->sheets[$sheet]['cells']) && array_key_exists($col,$this->sheets[$sheet]['cells'][$row])) {
  344. return $this->sheets[$sheet]['cells'][$row][$col];
  345. }
  346. return "";
  347. }
  348. function value($row,$col,$sheet=0) {
  349. return $this->val($row,$col,$sheet);
  350. }
  351. function info($row,$col,$type='',$sheet=0) {
  352. $col = $this->getCol($col);
  353. if (array_key_exists('cellsInfo',$this->sheets[$sheet])
  354. && array_key_exists($row,$this->sheets[$sheet]['cellsInfo'])
  355. && array_key_exists($col,$this->sheets[$sheet]['cellsInfo'][$row])
  356. && array_key_exists($type,$this->sheets[$sheet]['cellsInfo'][$row][$col])) {
  357. return $this->sheets[$sheet]['cellsInfo'][$row][$col][$type];
  358. }
  359. return "";
  360. }
  361. function type($row,$col,$sheet=0) {
  362. return $this->info($row,$col,'type',$sheet);
  363. }
  364. function raw($row,$col,$sheet=0) {
  365. return $this->info($row,$col,'raw',$sheet);
  366. }
  367. function rowspan($row,$col,$sheet=0) {
  368. $val = $this->info($row,$col,'rowspan',$sheet);
  369. if ($val=="") { return 1; }
  370. return $val;
  371. }
  372. function colspan($row,$col,$sheet=0) {
  373. $val = $this->info($row,$col,'colspan',$sheet);
  374. if ($val=="") { return 1; }
  375. return $val;
  376. }
  377. function hyperlink($row,$col,$sheet=0) {
  378. $link = $this->sheets[$sheet]['cellsInfo'][$row][$col]['hyperlink'];
  379. if ($link) {
  380. return $link['link'];
  381. }
  382. return '';
  383. }
  384. function rowcount($sheet=0) {
  385. return $this->sheets[$sheet]['numRows'];
  386. }
  387. function colcount($sheet=0) {
  388. return $this->sheets[$sheet]['numCols'];
  389. }
  390. function colwidth($col,$sheet=0) {
  391. // Col width is actually the width of the number 0. So we have to estimate and come close
  392. return $this->colInfo[$sheet][$col]['width']/9142*200;
  393. }
  394. function colhidden($col,$sheet=0) {
  395. return !!$this->colInfo[$sheet][$col]['hidden'];
  396. }
  397. function rowheight($row,$sheet=0) {
  398. return $this->rowInfo[$sheet][$row]['height'];
  399. }
  400. function rowhidden($row,$sheet=0) {
  401. return !!$this->rowInfo[$sheet][$row]['hidden'];
  402. }
  403.  
  404. // GET THE CSS FOR FORMATTING
  405. // ==========================
  406. function style($row,$col,$sheet=0,$properties='') {
  407. $css = "";
  408. $font=$this->font($row,$col,$sheet);
  409. if ($font!="") {
  410. $css .= "font-family:$font;";
  411. }
  412. $align=$this->align($row,$col,$sheet);
  413. if ($align!="") {
  414. $css .= "text-align:$align;";
  415. }
  416. $height=$this->height($row,$col,$sheet);
  417. if ($height!="") {
  418. $css .= "font-size:$height"."px;";
  419. }
  420. $bgcolor=$this->bgColor($row,$col,$sheet);
  421. if ($bgcolor!="") {
  422. $bgcolor = $this->colors[$bgcolor];
  423. $css .= "background-color:$bgcolor;";
  424. }
  425. $color=$this->color($row,$col,$sheet);
  426. if ($color!="") {
  427. $css .= "color:$color;";
  428. }
  429. $bold=$this->bold($row,$col,$sheet);
  430. if ($bold) {
  431. $css .= "font-weight:bold;";
  432. }
  433. $italic=$this->italic($row,$col,$sheet);
  434. if ($italic) {
  435. $css .= "font-style:italic;";
  436. }
  437. $underline=$this->underline($row,$col,$sheet);
  438. if ($underline) {
  439. $css .= "text-decoration:underline;";
  440. }
  441. // Borders
  442. $bLeft = $this->borderLeft($row,$col,$sheet);
  443. $bRight = $this->borderRight($row,$col,$sheet);
  444. $bTop = $this->borderTop($row,$col,$sheet);
  445. $bBottom = $this->borderBottom($row,$col,$sheet);
  446. $bLeftCol = $this->borderLeftColor($row,$col,$sheet);
  447. $bRightCol = $this->borderRightColor($row,$col,$sheet);
  448. $bTopCol = $this->borderTopColor($row,$col,$sheet);
  449. $bBottomCol = $this->borderBottomColor($row,$col,$sheet);
  450. // Try to output the minimal required style
  451. if ($bLeft!="" && $bLeft==$bRight && $bRight==$bTop && $bTop==$bBottom) {
  452. $css .= "border:" . $this->lineStylesCss[$bLeft] .";";
  453. }
  454. else {
  455. if ($bLeft!="") { $css .= "border-left:" . $this->lineStylesCss[$bLeft] .";"; }
  456. if ($bRight!="") { $css .= "border-right:" . $this->lineStylesCss[$bRight] .";"; }
  457. if ($bTop!="") { $css .= "border-top:" . $this->lineStylesCss[$bTop] .";"; }
  458. if ($bBottom!="") { $css .= "border-bottom:" . $this->lineStylesCss[$bBottom] .";"; }
  459. }
  460. // Only output border colors if there is an actual border specified
  461. if ($bLeft!="" && $bLeftCol!="") { $css .= "border-left-color:" . $bLeftCol .";"; }
  462. if ($bRight!="" && $bRightCol!="") { $css .= "border-right-color:" . $bRightCol .";"; }
  463. if ($bTop!="" && $bTopCol!="") { $css .= "border-top-color:" . $bTopCol . ";"; }
  464. if ($bBottom!="" && $bBottomCol!="") { $css .= "border-bottom-color:" . $bBottomCol .";"; }
  465.  
  466. return $css;
  467. }
  468.  
  469. // FORMAT PROPERTIES
  470. // =================
  471. function format($row,$col,$sheet=0) {
  472. return $this->info($row,$col,'format',$sheet);
  473. }
  474. function formatIndex($row,$col,$sheet=0) {
  475. return $this->info($row,$col,'formatIndex',$sheet);
  476. }
  477. function formatColor($row,$col,$sheet=0) {
  478. return $this->info($row,$col,'formatColor',$sheet);
  479. }
  480.  
  481. // CELL (XF) PROPERTIES
  482. // ====================
  483. function xfRecord($row,$col,$sheet=0) {
  484. $xfIndex = $this->info($row,$col,'xfIndex',$sheet);
  485. if ($xfIndex!="") {
  486. return $this->xfRecords[$xfIndex];
  487. }
  488. return null;
  489. }
  490. function xfProperty($row,$col,$sheet,$prop) {
  491. $xfRecord = $this->xfRecord($row,$col,$sheet);
  492. if ($xfRecord!=null) {
  493. return $xfRecord[$prop];
  494. }
  495. return "";
  496. }
  497. function align($row,$col,$sheet=0) {
  498. return $this->xfProperty($row,$col,$sheet,'align');
  499. }
  500. function bgColor($row,$col,$sheet=0) {
  501. return $this->xfProperty($row,$col,$sheet,'bgColor');
  502. }
  503. function borderLeft($row,$col,$sheet=0) {
  504. return $this->xfProperty($row,$col,$sheet,'borderLeft');
  505. }
  506. function borderRight($row,$col,$sheet=0) {
  507. return $this->xfProperty($row,$col,$sheet,'borderRight');
  508. }
  509. function borderTop($row,$col,$sheet=0) {
  510. return $this->xfProperty($row,$col,$sheet,'borderTop');
  511. }
  512. function borderBottom($row,$col,$sheet=0) {
  513. return $this->xfProperty($row,$col,$sheet,'borderBottom');
  514. }
  515. function borderLeftColor($row,$col,$sheet=0) {
  516. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderLeftColor')];
  517. }
  518. function borderRightColor($row,$col,$sheet=0) {
  519. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderRightColor')];
  520. }
  521. function borderTopColor($row,$col,$sheet=0) {
  522. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderTopColor')];
  523. }
  524. function borderBottomColor($row,$col,$sheet=0) {
  525. return $this->colors[$this->xfProperty($row,$col,$sheet,'borderBottomColor')];
  526. }
  527.  
  528. // FONT PROPERTIES
  529. // ===============
  530. function fontRecord($row,$col,$sheet=0) {
  531. $xfRecord = $this->xfRecord($row,$col,$sheet);
  532. if ($xfRecord!=null) {
  533. $font = $xfRecord['fontIndex'];
  534. if ($font!=null) {
  535. return $this->fontRecords[$font];
  536. }
  537. }
  538. return null;
  539. }
  540. function fontProperty($row,$col,$sheet=0,$prop) {
  541. $font = $this->fontRecord($row,$col,$sheet);
  542. if ($font!=null) {
  543. return $font[$prop];
  544. }
  545. return false;
  546. }
  547. function fontIndex($row,$col,$sheet=0) {
  548. return $this->xfProperty($row,$col,$sheet,'fontIndex');
  549. }
  550. function color($row,$col,$sheet=0) {
  551. $formatColor = $this->formatColor($row,$col,$sheet);
  552. if ($formatColor!="") {
  553. return $formatColor;
  554. }
  555. $ci = $this->fontProperty($row,$col,$sheet,'color');
  556. return $this->rawColor($ci);
  557. }
  558. function rawColor($ci) {
  559. if (($ci <> 0x7FFF) && ($ci <> '')) {
  560. return $this->colors[$ci];
  561. }
  562. return "";
  563. }
  564. function bold($row,$col,$sheet=0) {
  565. return $this->fontProperty($row,$col,$sheet,'bold');
  566. }
  567. function italic($row,$col,$sheet=0) {
  568. return $this->fontProperty($row,$col,$sheet,'italic');
  569. }
  570. function underline($row,$col,$sheet=0) {
  571. return $this->fontProperty($row,$col,$sheet,'under');
  572. }
  573. function height($row,$col,$sheet=0) {
  574. return $this->fontProperty($row,$col,$sheet,'height');
  575. }
  576. function font($row,$col,$sheet=0) {
  577. return $this->fontProperty($row,$col,$sheet,'font');
  578. }
  579.  
  580. // DUMP AN HTML TABLE OF THE ENTIRE XLS DATA
  581. // =========================================
  582. function dump($row_numbers=false,$col_letters=false,$sheet=0,$table_class='excel') {
  583. $out = "<table class=\"$table_class\" cellspacing=0>";
  584. if ($col_letters) {
  585. $out .= "<thead>\n\t<tr>";
  586. if ($row_numbers) {
  587. $out .= "\n\t\t<th>&nbsp</th>";
  588. }
  589. for($i=1;$i<=$this->colcount($sheet);$i++) {
  590. $style = "width:" . ($this->colwidth($i,$sheet)*1) . "px;";
  591. if ($this->colhidden($i,$sheet)) {
  592. $style .= "display:none;";
  593. }
  594. $out .= "\n\t\t<th style=\"$style\">" . strtoupper($this->colindexes[$i]) . "</th>";
  595. }
  596. $out .= "</tr></thead>\n";
  597. }
  598.  
  599. $out .= "<tbody>\n";
  600. for($row=1;$row<=$this->rowcount($sheet);$row++) {
  601. $rowheight = $this->rowheight($row,$sheet);
  602. $style = "height:" . ($rowheight*(4/3)) . "px;";
  603. if ($this->rowhidden($row,$sheet)) {
  604. $style .= "display:none;";
  605. }
  606. $out .= "\n\t<tr style=\"$style\">";
  607. if ($row_numbers) {
  608. $out .= "\n\t\t<th>$row</th>";
  609. }
  610. for($col=1;$col<=$this->colcount($sheet);$col++) {
  611. // Account for Rowspans/Colspans
  612. $rowspan = $this->rowspan($row,$col,$sheet);
  613. $colspan = $this->colspan($row,$col,$sheet);
  614. for($i=0;$i<$rowspan;$i++) {
  615. for($j=0;$j<$colspan;$j++) {
  616. if ($i>0 || $j>0) {
  617. $this->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
  618. }
  619. }
  620. }
  621. if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {
  622. $style = $this->style($row,$col,$sheet);
  623. if ($this->colhidden($col,$sheet)) {
  624. $style .= "display:none;";
  625. }
  626. $out .= "\n\t\t<td style=\"$style\"" . ($colspan > 1?" colspan=$colspan":"") . ($rowspan > 1?" rowspan=$rowspan":"") . ">";
  627. $val = $this->val($row,$col,$sheet);
  628. if ($val=='') { $val="&nbsp;"; }
  629. else {
  630. $val = htmlentities($val);
  631. $link = $this->hyperlink($row,$col,$sheet);
  632. if ($link!='') {
  633. $val = "<a href=\"$link\">$val</a>";
  634. }
  635. }
  636. $out .= "<nobr>".nl2br($val)."</nobr>";
  637. $out .= "</td>";
  638. }
  639. }
  640. $out .= "</tr>\n";
  641. }
  642. $out .= "</tbody></table>";
  643. return $out;
  644. }
  645.  
  646. // --------------
  647. // END PUBLIC API
  648.  
  649. var $boundsheets = array();
  650. var $formatRecords = array();
  651. var $fontRecords = array();
  652. var $xfRecords = array();
  653. var $colInfo = array();
  654. var $rowInfo = array();
  655.  
  656. var $sst = array();
  657. var $sheets = array();
  658.  
  659. var $data;
  660. var $_ole;
  661. var $_defaultEncoding = "UTF-8";
  662. var $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT;
  663. var $_columnsFormat = array();
  664. var $_rowoffset = 1;
  665. var $_coloffset = 1;
  666.  
  667. /**
  668. * List of default date formats used by Excel
  669. */
  670. var $dateFormats = array (
  671. 0xe => "m/d/Y",
  672. 0xf => "M-d-Y",
  673. 0x10 => "d-M",
  674. 0x11 => "M-Y",
  675. 0x12 => "h:i a",
  676. 0x13 => "h:i:s a",
  677. 0x14 => "H:i",
  678. 0x15 => "H:i:s",
  679. 0x16 => "d/m/Y H:i",
  680. 0x2d => "i:s",
  681. 0x2e => "H:i:s",
  682. 0x2f => "i:s.S"
  683. );
  684.  
  685. /**
  686. * Default number formats used by Excel
  687. */
  688. var $numberFormats = array(
  689. 0x1 => "0",
  690. 0x2 => "0.00",
  691. 0x3 => "#,##0",
  692. 0x4 => "#,##0.00",
  693. 0x5 => "\$#,##0;(\$#,##0)",
  694. 0x6 => "\$#,##0;[Red](\$#,##0)",
  695. 0x7 => "\$#,##0.00;(\$#,##0.00)",
  696. 0x8 => "\$#,##0.00;[Red](\$#,##0.00)",
  697. 0x9 => "0%",
  698. 0xa => "0.00%",
  699. 0xb => "0.00E+00",
  700. 0x25 => "#,##0;(#,##0)",
  701. 0x26 => "#,##0;[Red](#,##0)",
  702. 0x27 => "#,##0.00;(#,##0.00)",
  703. 0x28 => "#,##0.00;[Red](#,##0.00)",
  704. 0x29 => "#,##0;(#,##0)", // Not exactly
  705. 0x2a => "\$#,##0;(\$#,##0)", // Not exactly
  706. 0x2b => "#,##0.00;(#,##0.00)", // Not exactly
  707. 0x2c => "\$#,##0.00;(\$#,##0.00)", // Not exactly
  708. 0x30 => "##0.0E+0"
  709. );
  710.  
  711. var $colors = Array(
  712. 0x00 => "#000000",
  713. 0x01 => "#FFFFFF",
  714. 0x02 => "#FF0000",
  715. 0x03 => "#00FF00",
  716. 0x04 => "#0000FF",
  717. 0x05 => "#FFFF00",
  718. 0x06 => "#FF00FF",
  719. 0x07 => "#00FFFF",
  720. 0x08 => "#000000",
  721. 0x09 => "#FFFFFF",
  722. 0x0A => "#FF0000",
  723. 0x0B => "#00FF00",
  724. 0x0C => "#0000FF",
  725. 0x0D => "#FFFF00",
  726. 0x0E => "#FF00FF",
  727. 0x0F => "#00FFFF",
  728. 0x10 => "#800000",
  729. 0x11 => "#008000",
  730. 0x12 => "#000080",
  731. 0x13 => "#808000",
  732. 0x14 => "#800080",
  733. 0x15 => "#008080",
  734. 0x16 => "#C0C0C0",
  735. 0x17 => "#808080",
  736. 0x18 => "#9999FF",
  737. 0x19 => "#993366",
  738. 0x1A => "#FFFFCC",
  739. 0x1B => "#CCFFFF",
  740. 0x1C => "#660066",
  741. 0x1D => "#FF8080",
  742. 0x1E => "#0066CC",
  743. 0x1F => "#CCCCFF",
  744. 0x20 => "#000080",
  745. 0x21 => "#FF00FF",
  746. 0x22 => "#FFFF00",
  747. 0x23 => "#00FFFF",
  748. 0x24 => "#800080",
  749. 0x25 => "#800000",
  750. 0x26 => "#008080",
  751. 0x27 => "#0000FF",
  752. 0x28 => "#00CCFF",
  753. 0x29 => "#CCFFFF",
  754. 0x2A => "#CCFFCC",
  755. 0x2B => "#FFFF99",
  756. 0x2C => "#99CCFF",
  757. 0x2D => "#FF99CC",
  758. 0x2E => "#CC99FF",
  759. 0x2F => "#FFCC99",
  760. 0x30 => "#3366FF",
  761. 0x31 => "#33CCCC",
  762. 0x32 => "#99CC00",
  763. 0x33 => "#FFCC00",
  764. 0x34 => "#FF9900",
  765. 0x35 => "#FF6600",
  766. 0x36 => "#666699",
  767. 0x37 => "#969696",
  768. 0x38 => "#003366",
  769. 0x39 => "#339966",
  770. 0x3A => "#003300",
  771. 0x3B => "#333300",
  772. 0x3C => "#993300",
  773. 0x3D => "#993366",
  774. 0x3E => "#333399",
  775. 0x3F => "#333333",
  776. 0x40 => "#000000",
  777. 0x41 => "#FFFFFF",
  778.  
  779. 0x43 => "#000000",
  780. 0x4D => "#000000",
  781. 0x4E => "#FFFFFF",
  782. 0x4F => "#000000",
  783. 0x50 => "#FFFFFF",
  784. 0x51 => "#000000",
  785.  
  786. 0x7FFF => "#000000"
  787. );
  788.  
  789. var $lineStyles = array(
  790. 0x00 => "",
  791. 0x01 => "Thin",
  792. 0x02 => "Medium",
  793. 0x03 => "Dashed",
  794. 0x04 => "Dotted",
  795. 0x05 => "Thick",
  796. 0x06 => "Double",
  797. 0x07 => "Hair",
  798. 0x08 => "Medium dashed",
  799. 0x09 => "Thin dash-dotted",
  800. 0x0A => "Medium dash-dotted",
  801. 0x0B => "Thin dash-dot-dotted",
  802. 0x0C => "Medium dash-dot-dotted",
  803. 0x0D => "Slanted medium dash-dotted"
  804. );
  805.  
  806. var $lineStylesCss = array(
  807. "Thin" => "1px solid",
  808. "Medium" => "2px solid",
  809. "Dashed" => "1px dashed",
  810. "Dotted" => "1px dotted",
  811. "Thick" => "3px solid",
  812. "Double" => "double",
  813. "Hair" => "1px solid",
  814. "Medium dashed" => "2px dashed",
  815. "Thin dash-dotted" => "1px dashed",
  816. "Medium dash-dotted" => "2px dashed",
  817. "Thin dash-dot-dotted" => "1px dashed",
  818. "Medium dash-dot-dotted" => "2px dashed",
  819. "Slanted medium dash-dotte" => "2px dashed"
  820. );
  821.  
  822. function read16bitstring($data, $start) {
  823. $len = 0;
  824. while (ord($data[$start + $len]) + ord($data[$start + $len + 1]) > 0) $len++;
  825. return substr($data, $start, $len);
  826. }
  827.  
  828. // ADDED by Matt Kruse for better formatting
  829. function _format_value($format,$num,$f) {
  830. // 49==TEXT format
  831. // http://code.google.com/p/php-excel-reader/issues/detail?id=7
  832. if ( (!$f && $format=="%s") || ($f==49) || ($format=="GENERAL") ) {
  833. return array('string'=>$num, 'formatColor'=>null);
  834. }
  835.  
  836. // Custom pattern can be POSITIVE;NEGATIVE;ZERO
  837. // The "text" option as 4th parameter is not handled
  838. $parts = explode(";",$format);
  839. $pattern = $parts[0];
  840. // Negative pattern
  841. if (count($parts)>2 && $num==0) {
  842. $pattern = $parts[2];
  843. }
  844. // Zero pattern
  845. if (count($parts)>1 && $num<0) {
  846. $pattern = $parts[1];
  847. $num = abs($num);
  848. }
  849.  
  850. $color = "";
  851. $matches = array();
  852. $color_regex = "/^\[(BLACK|BLUE|CYAN|GREEN|MAGENTA|RED|WHITE|YELLOW)\]/i";
  853. if (preg_match($color_regex,$pattern,$matches)) {
  854. $color = strtolower($matches[1]);
  855. $pattern = preg_replace($color_regex,"",$pattern);
  856. }
  857.  
  858. // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  859. $pattern = preg_replace("/_./","",$pattern);
  860.  
  861. // Some non-number characters are escaped with \, which we don't need
  862. $pattern = preg_replace("/\\\/","",$pattern);
  863.  
  864. // Some non-number strings are quoted, so we'll get rid of the quotes
  865. $pattern = preg_replace("/\"/","",$pattern);
  866.  
  867. // TEMPORARY - Convert # to 0
  868. $pattern = preg_replace("/\#/","0",$pattern);
  869.  
  870. // Find out if we need comma formatting
  871. $has_commas = preg_match("/,/",$pattern);
  872. if ($has_commas) {
  873. $pattern = preg_replace("/,/","",$pattern);
  874. }
  875.  
  876. // Handle Percentages
  877. if (preg_match("/\d(\%)([^\%]|$)/",$pattern,$matches)) {
  878. $num = $num * 100;
  879. $pattern = preg_replace("/(\d)(\%)([^\%]|$)/","$1%$3",$pattern);
  880. }
  881.  
  882. // Handle the number itself
  883. $number_regex = "/(\d+)(\.?)(\d*)/";
  884. if (preg_match($number_regex,$pattern,$matches)) {
  885. $left = $matches[1];
  886. $dec = $matches[2];
  887. $right = $matches[3];
  888. if ($has_commas) {
  889. $formatted = number_format($num,strlen($right));
  890. }
  891. else {
  892. $sprintf_pattern = "%1.".strlen($right)."f";
  893. $formatted = sprintf($sprintf_pattern, $num);
  894. }
  895. $pattern = preg_replace($number_regex, $formatted, $pattern);
  896. }
  897.  
  898. return array(
  899. 'string'=>$pattern,
  900. 'formatColor'=>$color
  901. );
  902. }
  903.  
  904. /**
  905. * Constructor
  906. *
  907. * Some basic initialisation
  908. */
  909. function Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding='') {
  910. $this->_ole = new OLERead();
  911. $this->setUTFEncoder('iconv');
  912. if ($outputEncoding != '') {
  913. $this->setOutputEncoding($outputEncoding);
  914. }
  915. for ($i=1; $i<245; $i++) {
  916. $name = strtolower(( (($i-1)/26>=1)?chr(($i-1)/26+64):'') . chr(($i-1)%26+65));
  917. $this->colnames[$name] = $i;
  918. $this->colindexes[$i] = $name;
  919. }
  920. $this->store_extended_info = $store_extended_info;
  921. if ($file!="") {
  922. $this->read($file);
  923. }
  924. }
  925.  
  926. /**
  927. * Set the encoding method
  928. */
  929. function setOutputEncoding($encoding) {
  930. $this->_defaultEncoding = $encoding;
  931. }
  932.  
  933. /**
  934. * $encoder = 'iconv' or 'mb'
  935. * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
  936. * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
  937. */
  938. function setUTFEncoder($encoder = 'iconv') {
  939. $this->_encoderFunction = '';
  940. if ($encoder == 'iconv') {
  941. $this->_encoderFunction = function_exists('iconv') ? 'iconv' : '';
  942. } elseif ($encoder == 'mb') {
  943. $this->_encoderFunction = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : '';
  944. }
  945. }
  946.  
  947. function setRowColOffset($iOffset) {
  948. $this->_rowoffset = $iOffset;
  949. $this->_coloffset = $iOffset;
  950. }
  951.  
  952. /**
  953. * Set the default number format
  954. */
  955. function setDefaultFormat($sFormat) {
  956. $this->_defaultFormat = $sFormat;
  957. }
  958.  
  959. /**
  960. * Force a column to use a certain format
  961. */
  962. function setColumnFormat($column, $sFormat) {
  963. $this->_columnsFormat[$column] = $sFormat;
  964. }
  965.  
  966. /**
  967. * Read the spreadsheet file using OLE, then parse
  968. */
  969. function read($sFileName) {
  970. $res = $this->_ole->read($sFileName);
  971.  
  972. // oops, something goes wrong (Darko Miljanovic)
  973. if($res === false) {
  974. // check error code
  975. if($this->_ole->error == 1) {
  976. // bad file
  977. die('The filename ' . $sFileName . ' is not readable');
  978. }
  979. // check other error codes here (eg bad fileformat, etc...)
  980. }
  981. $this->data = $this->_ole->getWorkBook();
  982. $this->_parse();
  983. }
  984.  
  985. /**
  986. * Parse a workbook
  987. *
  988. * @access private
  989. * @return bool
  990. */
  991. function _parse() {
  992. $pos = 0;
  993. $data = $this->data;
  994.  
  995. $code = v($data,$pos);
  996. $length = v($data,$pos+2);
  997. $version = v($data,$pos+4);
  998. $substreamType = v($data,$pos+6);
  999.  
  1000. $this->version = $version;
  1001.  
  1002. if (($version != SPREADSHEET_EXCEL_READER_BIFF8) &&
  1003. ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  1004. return false;
  1005. }
  1006.  
  1007. if ($substreamType != SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS){
  1008. return false;
  1009. }
  1010.  
  1011. $pos += $length + 4;
  1012.  
  1013. $code = v($data,$pos);
  1014. $length = v($data,$pos+2);
  1015.  
  1016. while ($code != SPREADSHEET_EXCEL_READER_TYPE_EOF) {
  1017. switch ($code) {
  1018. case SPREADSHEET_EXCEL_READER_TYPE_SST:
  1019. $spos = $pos + 4;
  1020. $limitpos = $spos + $length;
  1021. $uniqueStrings = $this->_GetInt4d($data, $spos+4);
  1022. $spos += 8;
  1023. for ($i = 0; $i < $uniqueStrings; $i++) {
  1024. // Read in the number of characters
  1025. if ($spos == $limitpos) {
  1026. $opcode = v($data,$spos);
  1027. $conlength = v($data,$spos+2);
  1028. if ($opcode != 0x3c) {
  1029. return -1;
  1030. }
  1031. $spos += 4;
  1032. $limitpos = $spos + $conlength;
  1033. }
  1034. $numChars = ord($data[$spos]) | (ord($data[$spos+1]) << 8);
  1035. $spos += 2;
  1036. $optionFlags = ord($data[$spos]);
  1037. $spos++;
  1038. $asciiEncoding = (($optionFlags & 0x01) == 0) ;
  1039. $extendedString = ( ($optionFlags & 0x04) != 0);
  1040.  
  1041. // See if string contains formatting information
  1042. $richString = ( ($optionFlags & 0x08) != 0);
  1043.  
  1044. if ($richString) {
  1045. // Read in the crun
  1046. $formattingRuns = v($data,$spos);
  1047. $spos += 2;
  1048. }
  1049.  
  1050. if ($extendedString) {
  1051. // Read in cchExtRst
  1052. $extendedRunLength = $this->_GetInt4d($data, $spos);
  1053. $spos += 4;
  1054. }
  1055.  
  1056. $len = ($asciiEncoding)? $numChars : $numChars*2;
  1057. if ($spos + $len < $limitpos) {
  1058. $retstr = substr($data, $spos, $len);
  1059. $spos += $len;
  1060. }
  1061. else{
  1062. // found countinue
  1063. $retstr = substr($data, $spos, $limitpos - $spos);
  1064. $bytesRead = $limitpos - $spos;
  1065. $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2));
  1066. $spos = $limitpos;
  1067.  
  1068. while ($charsLeft > 0){
  1069. $opcode = v($data,$spos);
  1070. $conlength = v($data,$spos+2);
  1071. if ($opcode != 0x3c) {
  1072. return -1;
  1073. }
  1074. $spos += 4;
  1075. $limitpos = $spos + $conlength;
  1076. $option = ord($data[$spos]);
  1077. $spos += 1;
  1078. if ($asciiEncoding && ($option == 0)) {
  1079. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  1080. $retstr .= substr($data, $spos, $len);
  1081. $charsLeft -= $len;
  1082. $asciiEncoding = true;
  1083. }
  1084. elseif (!$asciiEncoding && ($option != 0)) {
  1085. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  1086. $retstr .= substr($data, $spos, $len);
  1087. $charsLeft -= $len/2;
  1088. $asciiEncoding = false;
  1089. }
  1090. elseif (!$asciiEncoding && ($option == 0)) {
  1091. // Bummer - the string starts off as Unicode, but after the
  1092. // continuation it is in straightforward ASCII encoding
  1093. $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength);
  1094. for ($j = 0; $j < $len; $j++) {
  1095. $retstr .= $data[$spos + $j].chr(0);
  1096. }
  1097. $charsLeft -= $len;
  1098. $asciiEncoding = false;
  1099. }
  1100. else{
  1101. $newstr = '';
  1102. for ($j = 0; $j < strlen($retstr); $j++) {
  1103. $newstr = $retstr[$j].chr(0);
  1104. }
  1105. $retstr = $newstr;
  1106. $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength);
  1107. $retstr .= substr($data, $spos, $len);
  1108. $charsLeft -= $len/2;
  1109. $asciiEncoding = false;
  1110. }
  1111. $spos += $len;
  1112. }
  1113. }
  1114. $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);
  1115.  
  1116. if ($richString){
  1117. $spos += 4 * $formattingRuns;
  1118. }
  1119.  
  1120. // For extended strings, skip over the extended string data
  1121. if ($extendedString) {
  1122. $spos += $extendedRunLength;
  1123. }
  1124. $this->sst[]=$retstr;
  1125. }
  1126. break;
  1127. case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS:
  1128. return false;
  1129. break;
  1130. case SPREADSHEET_EXCEL_READER_TYPE_NAME:
  1131. break;
  1132. case SPREADSHEET_EXCEL_READER_TYPE_FORMAT:
  1133. $indexCode = v($data,$pos+4);
  1134. if ($version == SPREADSHEET_EXCEL_READER_BIFF8) {
  1135. $numchars = v($data,$pos+6);
  1136. if (ord($data[$pos+8]) == 0){
  1137. $formatString = substr($data, $pos+9, $numchars);
  1138. } else {
  1139. $formatString = substr($data, $pos+9, $numchars*2);
  1140. }
  1141. } else {
  1142. $numchars = ord($data[$pos+6]);
  1143. $formatString = substr($data, $pos+7, $numchars*2);
  1144. }
  1145. $this->formatRecords[$indexCode] = $formatString;
  1146. break;
  1147. case SPREADSHEET_EXCEL_READER_TYPE_FONT:
  1148. $height = v($data,$pos+4);
  1149. $option = v($data,$pos+6);
  1150. $color = v($data,$pos+8);
  1151. $weight = v($data,$pos+10);
  1152. $under = ord($data[$pos+14]);
  1153. $font = "";
  1154. // Font name
  1155. $numchars = ord($data[$pos+18]);
  1156. if ((ord($data[$pos+19]) & 1) == 0){
  1157. $font = substr($data, $pos+20, $numchars);
  1158. } else {
  1159. $font = substr($data, $pos+20, $numchars*2);
  1160. $font = $this->_encodeUTF16($font);
  1161. }
  1162. $this->fontRecords[] = array(
  1163. 'height' => $height / 20,
  1164. 'italic' => !!($option & 2),
  1165. 'color' => $color,
  1166. 'under' => !($under==0),
  1167. 'bold' => ($weight==700),
  1168. 'font' => $font,
  1169. 'raw' => $this->dumpHexData($data, $pos+3, $length)
  1170. );
  1171. break;
  1172.  
  1173. case SPREADSHEET_EXCEL_READER_TYPE_PALETTE:
  1174. $colors = ord($data[$pos+4]) | ord($data[$pos+5]) << 8;
  1175. for ($coli = 0; $coli < $colors; $coli++) {
  1176. $colOff = $pos + 2 + ($coli * 4);
  1177. $colr = ord($data[$colOff]);
  1178. $colg = ord($data[$colOff+1]);
  1179. $colb = ord($data[$colOff+2]);
  1180. $this->colors[0x07 + $coli] = '#' . $this->myhex($colr) . $this->myhex($colg) . $this->myhex($colb);
  1181. }
  1182. break;
  1183.  
  1184. case SPREADSHEET_EXCEL_READER_TYPE_XF:
  1185. $fontIndexCode = (ord($data[$pos+4]) | ord($data[$pos+5]) << 8) - 1;
  1186. $fontIndexCode = max(0,$fontIndexCode);
  1187. $indexCode = ord($data[$pos+6]) | ord($data[$pos+7]) << 8;
  1188. $alignbit = ord($data[$pos+10]) & 3;
  1189. $bgi = (ord($data[$pos+22]) | ord($data[$pos+23]) << 8) & 0x3FFF;
  1190. $bgcolor = ($bgi & 0x7F);
  1191. // $bgcolor = ($bgi & 0x3f80) >> 7;
  1192. $align = "";
  1193. if ($alignbit==3) { $align="right"; }
  1194. if ($alignbit==2) { $align="center"; }
  1195.  
  1196. $fillPattern = (ord($data[$pos+21]) & 0xFC) >> 2;
  1197. if ($fillPattern == 0) {
  1198. $bgcolor = "";
  1199. }
  1200.  
  1201. $xf = array();
  1202. $xf['formatIndex'] = $indexCode;
  1203. $xf['align'] = $align;
  1204. $xf['fontIndex'] = $fontIndexCode;
  1205. $xf['bgColor'] = $bgcolor;
  1206. $xf['fillPattern'] = $fillPattern;
  1207.  
  1208. $border = ord($data[$pos+14]) | (ord($data[$pos+15]) << 8) | (ord($data[$pos+16]) << 16) | (ord($data[$pos+17]) << 24);
  1209. $xf['borderLeft'] = $this->lineStyles[($border & 0xF)];
  1210. $xf['borderRight'] = $this->lineStyles[($border & 0xF0) >> 4];
  1211. $xf['borderTop'] = $this->lineStyles[($border & 0xF00) >> 8];
  1212. $xf['borderBottom'] = $this->lineStyles[($border & 0xF000) >> 12];
  1213.  
  1214. $xf['borderLeftColor'] = ($border & 0x7F0000) >> 16;
  1215. $xf['borderRightColor'] = ($border & 0x3F800000) >> 23;
  1216. $border = (ord($data[$pos+18]) | ord($data[$pos+19]) << 8);
  1217.  
  1218. $xf['borderTopColor'] = ($border & 0x7F);
  1219. $xf['borderBottomColor'] = ($border & 0x3F80) >> 7;
  1220.  
  1221. if (array_key_exists($indexCode, $this->dateFormats)) {
  1222. $xf['type'] = 'date';
  1223. $xf['format'] = $this->dateFormats[$indexCode];
  1224. if ($align=='') { $xf['align'] = 'right'; }
  1225. }elseif (array_key_exists($indexCode, $this->numberFormats)) {
  1226. $xf['type'] = 'number';
  1227. $xf['format'] = $this->numberFormats[$indexCode];
  1228. if ($align=='') { $xf['align'] = 'right'; }
  1229. }else{
  1230. $isdate = FALSE;
  1231. $formatstr = '';
  1232. if ($indexCode > 0){
  1233. if (isset($this->formatRecords[$indexCode]))
  1234. $formatstr = $this->formatRecords[$indexCode];
  1235. if ($formatstr!="") {
  1236. $tmp = preg_replace("/\;.*/","",$formatstr);
  1237. $tmp = preg_replace("/^\[[^\]]*\]/","",$tmp);
  1238. if (preg_match("/[^hmsday\/\-:\s\\\,AMP]/i", $tmp) == 0) { // found day and time format
  1239. $isdate = TRUE;
  1240. $formatstr = $tmp;
  1241. $formatstr = str_replace(array('AM/PM','mmmm','mmm'), array('a','F','M'), $formatstr);
  1242. // m/mm are used for both minutes and months - oh SNAP!
  1243. // This mess tries to fix for that.
  1244. // 'm' == minutes only if following h/hh or preceding s/ss
  1245. $formatstr = preg_replace("/(h:?)mm?/","$1i", $formatstr);
  1246. $formatstr = preg_replace("/mm?(:?s)/","i$1", $formatstr);
  1247. // A single 'm' = n in PHP
  1248. $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1249. $formatstr = preg_replace("/(^|[^m])m([^m]|$)/", '$1n$2', $formatstr);
  1250. // else it's months
  1251. $formatstr = str_replace('mm', 'm', $formatstr);
  1252. // Convert single 'd' to 'j'
  1253. $formatstr = preg_replace("/(^|[^d])d([^d]|$)/", '$1j$2', $formatstr);
  1254. $formatstr = str_replace(array('dddd','ddd','dd','yyyy','yy','hh','h'), array('l','D','d','Y','y','H','g'), $formatstr);
  1255. $formatstr = preg_replace("/ss?/", 's', $formatstr);
  1256. }
  1257. }
  1258. }
  1259. if ($isdate){
  1260. $xf['type'] = 'date';
  1261. $xf['format'] = $formatstr;
  1262. if ($align=='') { $xf['align'] = 'right'; }
  1263. }else{
  1264. // If the format string has a 0 or # in it, we'll assume it's a number
  1265. if (preg_match("/[0#]/", $formatstr)) {
  1266. $xf['type'] = 'number';
  1267. if ($align=='') { $xf['align']='right'; }
  1268. }
  1269. else {
  1270. $xf['type'] = 'other';
  1271. }
  1272. $xf['format'] = $formatstr;
  1273. $xf['code'] = $indexCode;
  1274. }
  1275. }
  1276. $this->xfRecords[] = $xf;
  1277. break;
  1278. case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR:
  1279. $this->nineteenFour = (ord($data[$pos+4]) == 1);
  1280. break;
  1281. case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET:
  1282. $rec_offset = $this->_GetInt4d($data, $pos+4);
  1283. $rec_typeFlag = ord($data[$pos+8]);
  1284. $rec_visibilityFlag = ord($data[$pos+9]);
  1285. $rec_length = ord($data[$pos+10]);
  1286.  
  1287. if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1288. $chartype = ord($data[$pos+11]);
  1289. if ($chartype == 0){
  1290. $rec_name = substr($data, $pos+12, $rec_length);
  1291. } else {
  1292. $rec_name = $this->_encodeUTF16(substr($data, $pos+12, $rec_length*2));
  1293. }
  1294. }elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1295. $rec_name = substr($data, $pos+11, $rec_length);
  1296. }
  1297. $this->boundsheets[] = array('name'=>$rec_name,'offset'=>$rec_offset);
  1298. break;
  1299.  
  1300. }
  1301.  
  1302. $pos += $length + 4;
  1303. $code = ord($data[$pos]) | ord($data[$pos+1])<<8;
  1304. $length = ord($data[$pos+2]) | ord($data[$pos+3])<<8;
  1305. }
  1306.  
  1307. foreach ($this->boundsheets as $key=>$val){
  1308. $this->sn = $key;
  1309. $this->_parsesheet($val['offset']);
  1310. }
  1311. return true;
  1312. }
  1313.  
  1314. /**
  1315. * Parse a worksheet
  1316. */
  1317. function _parsesheet($spos) {
  1318. $cont = true;
  1319. $data = $this->data;
  1320. // read BOF
  1321. $code = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1322. $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1323.  
  1324. $version = ord($data[$spos + 4]) | ord($data[$spos + 5])<<8;
  1325. $substreamType = ord($data[$spos + 6]) | ord($data[$spos + 7])<<8;
  1326.  
  1327. if (($version != SPREADSHEET_EXCEL_READER_BIFF8) && ($version != SPREADSHEET_EXCEL_READER_BIFF7)) {
  1328. return -1;
  1329. }
  1330.  
  1331. if ($substreamType != SPREADSHEET_EXCEL_READER_WORKSHEET){
  1332. return -2;
  1333. }
  1334. $spos += $length + 4;
  1335. while($cont) {
  1336. $lowcode = ord($data[$spos]);
  1337. if ($lowcode == SPREADSHEET_EXCEL_READER_TYPE_EOF) break;
  1338. $code = $lowcode | ord($data[$spos+1])<<8;
  1339. $length = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1340. $spos += 4;
  1341. $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1;
  1342. $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1;
  1343. unset($this->rectype);
  1344. switch ($code) {
  1345. case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION:
  1346. if (!isset($this->numRows)) {
  1347. if (($length == 10) || ($version == SPREADSHEET_EXCEL_READER_BIFF7)){
  1348. $this->sheets[$this->sn]['numRows'] = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1349. $this->sheets[$this->sn]['numCols'] = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1350. } else {
  1351. $this->sheets[$this->sn]['numRows'] = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1352. $this->sheets[$this->sn]['numCols'] = ord($data[$spos+10]) | ord($data[$spos+11]) << 8;
  1353. }
  1354. }
  1355. break;
  1356. case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS:
  1357. $cellRanges = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1358. for ($i = 0; $i < $cellRanges; $i++) {
  1359. $fr = ord($data[$spos + 8*$i + 2]) | ord($data[$spos + 8*$i + 3])<<8;
  1360. $lr = ord($data[$spos + 8*$i + 4]) | ord($data[$spos + 8*$i + 5])<<8;
  1361. $fc = ord($data[$spos + 8*$i + 6]) | ord($data[$spos + 8*$i + 7])<<8;
  1362. $lc = ord($data[$spos + 8*$i + 8]) | ord($data[$spos + 8*$i + 9])<<8;
  1363. if ($lr - $fr > 0) {
  1364. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1;
  1365. }
  1366. if ($lc - $fc > 0) {
  1367. $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1;
  1368. }
  1369. }
  1370. break;
  1371. case SPREADSHEET_EXCEL_READER_TYPE_RK:
  1372. case SPREADSHEET_EXCEL_READER_TYPE_RK2:
  1373. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1374. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1375. $rknum = $this->_GetInt4d($data, $spos + 6);
  1376. $numValue = $this->_GetIEEE754($rknum);
  1377. $info = $this->_getCellDetails($spos,$numValue,$column);
  1378. $this->addcell($row, $column, $info['string'],$info);
  1379. break;
  1380. case SPREADSHEET_EXCEL_READER_TYPE_LABELSST:
  1381. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1382. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1383. $xfindex = ord($data[$spos+4]) | ord($data[$spos+5])<<8;
  1384. $index = $this->_GetInt4d($data, $spos + 6);
  1385. $this->addcell($row, $column, $this->sst[$index], array('xfIndex'=>$xfindex) );
  1386. break;
  1387. case SPREADSHEET_EXCEL_READER_TYPE_MULRK:
  1388. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1389. $colFirst = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1390. $colLast = ord($data[$spos + $length - 2]) | ord($data[$spos + $length - 1])<<8;
  1391. $columns = $colLast - $colFirst + 1;
  1392. $tmppos = $spos+4;
  1393. for ($i = 0; $i < $columns; $i++) {
  1394. $numValue = $this->_GetIEEE754($this->_GetInt4d($data, $tmppos + 2));
  1395. $info = $this->_getCellDetails($tmppos-4,$numValue,$colFirst + $i + 1);
  1396. $tmppos += 6;
  1397. $this->addcell($row, $colFirst + $i, $info['string'], $info);
  1398. }
  1399. break;
  1400. case SPREADSHEET_EXCEL_READER_TYPE_NUMBER:
  1401. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1402. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1403. $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1404. if ($this->isDate($spos)) {
  1405. $numValue = $tmp['double'];
  1406. }
  1407. else {
  1408. $numValue = $this->createNumber($spos);
  1409. }
  1410. $info = $this->_getCellDetails($spos,$numValue,$column);
  1411. $this->addcell($row, $column, $info['string'], $info);
  1412. break;
  1413.  
  1414. case SPREADSHEET_EXCEL_READER_TYPE_FORMULA:
  1415. case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2:
  1416. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1417. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1418. if ((ord($data[$spos+6])==0) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1419. //String formula. Result follows in a STRING record
  1420. // This row/col are stored to be referenced in that record
  1421. // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1422. $previousRow = $row;
  1423. $previousCol = $column;
  1424. } elseif ((ord($data[$spos+6])==1) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1425. //Boolean formula. Result is in +2; 0=false,1=true
  1426. // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1427. if (ord($this->data[$spos+8])==1) {
  1428. $this->addcell($row, $column, "TRUE");
  1429. } else {
  1430. $this->addcell($row, $column, "FALSE");
  1431. }
  1432. } elseif ((ord($data[$spos+6])==2) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1433. //Error formula. Error code is in +2;
  1434. } elseif ((ord($data[$spos+6])==3) && (ord($data[$spos+12])==255) && (ord($data[$spos+13])==255)) {
  1435. //Formula result is a null string.
  1436. $this->addcell($row, $column, '');
  1437. } else {
  1438. // result is a number, so first 14 bytes are just like a _NUMBER record
  1439. $tmp = unpack("ddouble", substr($data, $spos + 6, 8)); // It machine machine dependent
  1440. if ($this->isDate($spos)) {
  1441. $numValue = $tmp['double'];
  1442. }
  1443. else {
  1444. $numValue = $this->createNumber($spos);
  1445. }
  1446. $info = $this->_getCellDetails($spos,$numValue,$column);
  1447. $this->addcell($row, $column, $info['string'], $info);
  1448. }
  1449. break;
  1450. case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR:
  1451. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1452. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1453. $string = ord($data[$spos+6]);
  1454. $this->addcell($row, $column, $string);
  1455. break;
  1456. case SPREADSHEET_EXCEL_READER_TYPE_STRING:
  1457. // http://code.google.com/p/php-excel-reader/issues/detail?id=4
  1458. if ($version == SPREADSHEET_EXCEL_READER_BIFF8){
  1459. // Unicode 16 string, like an SST record
  1460. $xpos = $spos;
  1461. $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1462. $xpos += 2;
  1463. $optionFlags =ord($data[$xpos]);
  1464. $xpos++;
  1465. $asciiEncoding = (($optionFlags &0x01) == 0) ;
  1466. $extendedString = (($optionFlags & 0x04) != 0);
  1467. // See if string contains formatting information
  1468. $richString = (($optionFlags & 0x08) != 0);
  1469. if ($richString) {
  1470. // Read in the crun
  1471. $formattingRuns =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1472. $xpos += 2;
  1473. }
  1474. if ($extendedString) {
  1475. // Read in cchExtRst
  1476. $extendedRunLength =$this->_GetInt4d($this->data, $xpos);
  1477. $xpos += 4;
  1478. }
  1479. $len = ($asciiEncoding)?$numChars : $numChars*2;
  1480. $retstr =substr($data, $xpos, $len);
  1481. $xpos += $len;
  1482. $retstr = ($asciiEncoding)? $retstr : $this->_encodeUTF16($retstr);
  1483. }
  1484. elseif ($version == SPREADSHEET_EXCEL_READER_BIFF7){
  1485. // Simple byte string
  1486. $xpos = $spos;
  1487. $numChars =ord($data[$xpos]) | (ord($data[$xpos+1]) << 8);
  1488. $xpos += 2;
  1489. $retstr =substr($data, $xpos, $numChars);
  1490. }
  1491. $this->addcell($previousRow, $previousCol, $retstr);
  1492. break;
  1493. case SPREADSHEET_EXCEL_READER_TYPE_ROW:
  1494. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1495. $rowInfo = ord($data[$spos + 6]) | ((ord($data[$spos+7]) << 8) & 0x7FFF);
  1496. if (($rowInfo & 0x8000) > 0) {
  1497. $rowHeight = -1;
  1498. } else {
  1499. $rowHeight = $rowInfo & 0x7FFF;
  1500. }
  1501. $rowHidden = (ord($data[$spos + 12]) & 0x20) >> 5;
  1502. $this->rowInfo[$this->sn][$row+1] = Array('height' => $rowHeight / 20, 'hidden'=>$rowHidden );
  1503. break;
  1504. case SPREADSHEET_EXCEL_READER_TYPE_DBCELL:
  1505. break;
  1506. case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK:
  1507. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1508. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1509. $cols = ($length / 2) - 3;
  1510. for ($c = 0; $c < $cols; $c++) {
  1511. $xfindex = ord($data[$spos + 4 + ($c * 2)]) | ord($data[$spos + 5 + ($c * 2)])<<8;
  1512. $this->addcell($row, $column + $c, "", array('xfIndex'=>$xfindex));
  1513. }
  1514. break;
  1515. case SPREADSHEET_EXCEL_READER_TYPE_LABEL:
  1516. $row = ord($data[$spos]) | ord($data[$spos+1])<<8;
  1517. $column = ord($data[$spos+2]) | ord($data[$spos+3])<<8;
  1518. $this->addcell($row, $column, substr($data, $spos + 8, ord($data[$spos + 6]) | ord($data[$spos + 7])<<8));
  1519. break;
  1520. case SPREADSHEET_EXCEL_READER_TYPE_EOF:
  1521. $cont = false;
  1522. break;
  1523. case SPREADSHEET_EXCEL_READER_TYPE_HYPER:
  1524. // Only handle hyperlinks to a URL
  1525. $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8;
  1526. $row2 = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8;
  1527. $column = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8;
  1528. $column2 = ord($this->data[$spos+6]) | ord($this->data[$spos+7])<<8;
  1529. $linkdata = Array();
  1530. $flags = ord($this->data[$spos + 28]);
  1531. $udesc = "";
  1532. $ulink = "";
  1533. $uloc = 32;
  1534. $linkdata['flags'] = $flags;
  1535. if (($flags & 1) > 0 ) { // is a type we understand
  1536. // is there a description ?
  1537. if (($flags & 0x14) == 0x14 ) { // has a description
  1538. $uloc += 4;
  1539. $descLen = ord($this->data[$spos + 32]) | ord($this->data[$spos + 33]) << 8;
  1540. $udesc = substr($this->data, $spos + $uloc, $descLen * 2);
  1541. $uloc += 2 * $descLen;
  1542. }
  1543. $ulink = $this->read16bitstring($this->data, $spos + $uloc + 20);
  1544. if ($udesc == "") {
  1545. $udesc = $ulink;
  1546. }
  1547. }
  1548. $linkdata['desc'] = $udesc;
  1549. $linkdata['link'] = $this->_encodeUTF16($ulink);
  1550. for ($r=$row; $r<=$row2; $r++) {
  1551. for ($c=$column; $c<=$column2; $c++) {
  1552. $this->sheets[$this->sn]['cellsInfo'][$r+1][$c+1]['hyperlink'] = $linkdata;
  1553. }
  1554. }
  1555. break;
  1556. case SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH:
  1557. $this->defaultColWidth = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1558. break;
  1559. case SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH:
  1560. $this->standardColWidth = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1561. break;
  1562. case SPREADSHEET_EXCEL_READER_TYPE_COLINFO:
  1563. $colfrom = ord($data[$spos+0]) | ord($data[$spos+1]) << 8;
  1564. $colto = ord($data[$spos+2]) | ord($data[$spos+3]) << 8;
  1565. $cw = ord($data[$spos+4]) | ord($data[$spos+5]) << 8;
  1566. $cxf = ord($data[$spos+6]) | ord($data[$spos+7]) << 8;
  1567. $co = ord($data[$spos+8]);
  1568. for ($coli = $colfrom; $coli <= $colto; $coli++) {
  1569. $this->colInfo[$this->sn][$coli+1] = Array('width' => $cw, 'xf' => $cxf, 'hidden' => ($co & 0x01), 'collapsed' => ($co & 0x1000) >> 12);
  1570. }
  1571. break;
  1572.  
  1573. default:
  1574. break;
  1575. }
  1576. $spos += $length;
  1577. }
  1578.  
  1579. if (!isset($this->sheets[$this->sn]['numRows']))
  1580. $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow'];
  1581. if (!isset($this->sheets[$this->sn]['numCols']))
  1582. $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol'];
  1583. }
  1584.  
  1585. function isDate($spos) {
  1586. $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  1587. return ($this->xfRecords[$xfindex]['type'] == 'date');
  1588. }
  1589.  
  1590. // Get the details for a particular cell
  1591. function _getCellDetails($spos,$numValue,$column) {
  1592. $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8;
  1593. $xfrecord = $this->xfRecords[$xfindex];
  1594. $type = $xfrecord['type'];
  1595.  
  1596. $format = $xfrecord['format'];
  1597. $formatIndex = $xfrecord['formatIndex'];
  1598. $fontIndex = $xfrecord['fontIndex'];
  1599. $formatColor = "";
  1600. $rectype = '';
  1601. $string = '';
  1602. $raw = '';
  1603.  
  1604. if (isset($this->_columnsFormat[$column + 1])){
  1605. $format = $this->_columnsFormat[$column + 1];
  1606. }
  1607.  
  1608. if ($type == 'date') {
  1609. // See http://groups.google.com/group/php-excel-reader-discuss/browse_frm/thread/9c3f9790d12d8e10/f2045c2369ac79de
  1610. $rectype = 'date';
  1611. // Convert numeric value into a date
  1612. $utcDays = floor($numValue - ($this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS));
  1613. $utcValue = ($utcDays) * SPREADSHEET_EXCEL_READER_MSINADAY;
  1614. $dateinfo = gmgetdate($utcValue);
  1615.  
  1616. $raw = $numValue;
  1617. $fractionalDay = $numValue - floor($numValue) + .0000001; // The .0000001 is to fix for php/excel fractional diffs
  1618.  
  1619. $totalseconds = floor(SPREADSHEET_EXCEL_READER_MSINADAY * $fractionalDay);
  1620. $secs = $totalseconds % 60;
  1621. $totalseconds -= $secs;
  1622. $hours = floor($totalseconds / (60 * 60));
  1623. $mins = floor($totalseconds / 60) % 60;
  1624. $string = date ($format, mktime($hours, $mins, $secs, $dateinfo["mon"], $dateinfo["mday"], $dateinfo["year"]));
  1625. } else if ($type == 'number') {
  1626. $rectype = 'number';
  1627. $formatted = $this->_format_value($format, $numValue, $formatIndex);
  1628. $string = $formatted['string'];
  1629. $formatColor = $formatted['formatColor'];
  1630. $raw = $numValue;
  1631. } else{
  1632. if ($format=="") {
  1633. $format = $this->_defaultFormat;
  1634. }
  1635. $rectype = 'unknown';
  1636. $formatted = $this->_format_value($format, $numValue, $formatIndex);
  1637. $string = $formatted['string'];
  1638. $formatColor = $formatted['formatColor'];
  1639. $raw = $numValue;
  1640. }
  1641.  
  1642. return array(
  1643. 'string'=>$string,
  1644. 'raw'=>$raw,
  1645. 'rectype'=>$rectype,
  1646. 'format'=>$format,
  1647. 'formatIndex'=>$formatIndex,
  1648. 'fontIndex'=>$fontIndex,
  1649. 'formatColor'=>$formatColor,
  1650. 'xfIndex'=>$xfindex
  1651. );
  1652.  
  1653. }
  1654.  
  1655. function createNumber($spos) {
  1656. $rknumhigh = $this->_GetInt4d($this->data, $spos + 10);
  1657. $rknumlow = $this->_GetInt4d($this->data, $spos + 6);
  1658. $sign = ($rknumhigh & 0x80000000) >> 31;
  1659. $exp = ($rknumhigh & 0x7ff00000) >> 20;
  1660. $mantissa = (0x100000 | ($rknumhigh & 0x000fffff));
  1661. $mantissalow1 = ($rknumlow & 0x80000000) >> 31;
  1662. $mantissalow2 = ($rknumlow & 0x7fffffff);
  1663. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1664. if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023)));
  1665. $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023)));
  1666. if ($sign) {$value = -1 * $value;}
  1667. return $value;
  1668. }
  1669.  
  1670. function addcell($row, $col, $string, $info=null) {
  1671. $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset);
  1672. $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset);
  1673. $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string;
  1674. if ($this->store_extended_info && $info) {
  1675. foreach ($info as $key=>$val) {
  1676. $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset][$key] = $val;
  1677. }
  1678. }
  1679. }
  1680.  
  1681. function _GetIEEE754($rknum) {
  1682. if (($rknum & 0x02) != 0) {
  1683. $value = $rknum >> 2;
  1684. } else {
  1685. //mmp
  1686. // I got my info on IEEE754 encoding from
  1687. // http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
  1688. // The RK format calls for using only the most significant 30 bits of the
  1689. // 64 bit floating point value. The other 34 bits are assumed to be 0
  1690. // So, we use the upper 30 bits of $rknum as follows...
  1691. $sign = ($rknum & 0x80000000) >> 31;
  1692. $exp = ($rknum & 0x7ff00000) >> 20;
  1693. $mantissa = (0x100000 | ($rknum & 0x000ffffc));
  1694. $value = $mantissa / pow( 2 , (20- ($exp - 1023)));
  1695. if ($sign) {
  1696. $value = -1 * $value;
  1697. }
  1698. //end of changes by mmp
  1699. }
  1700. if (($rknum & 0x01) != 0) {
  1701. $value /= 100;
  1702. }
  1703. return $value;
  1704. }
  1705.  
  1706. function _encodeUTF16($string) {
  1707. $result = $string;
  1708. if ($this->_defaultEncoding){
  1709. switch ($this->_encoderFunction){
  1710. case 'iconv' : $result = iconv('UTF-16LE', $this->_defaultEncoding, $string);
  1711. break;
  1712. case 'mb_convert_encoding' : $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' );
  1713. break;
  1714. }
  1715. }
  1716. return $result;
  1717. }
  1718.  
  1719. function _GetInt4d($data, $pos) {
  1720. $value = ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
  1721. if ($value>=4294967294) {
  1722. $value=-2;
  1723. }
  1724. return $value;
  1725. }
  1726.  
  1727. }
  1728.  
  1729. ?>

php读取excel文档内容(转载)的更多相关文章

  1. PHP读取excel文档

    PHP读取excel文档 项目需要读取Excel的内容,从百度搜索了下,主要有两个选择,第一个是PHPExcelReader,另外一个是PHPExcel.   PHPExcelReader比较轻量级, ...

  2. C#操作Excel(2)-- 打开-读取Excel文档

    由于要为某软件实现导出Excel功能,故有此文. 本文的开发环境是Visual Studio 2010 ,C#, Excel 2007. 新建C#工程后打开Solution Explorer,可以看到 ...

  3. Python实现读取Excel文档中的配置并下载软件包

    问题:现在遇到这样一个问题,服务器存储了很多软件包,这些包输入不同的产品,每个产品都有自己的配置,互相交叉,那么到底某一产品所有配置的软件包下载后,占用多大空间呢? 分析:从这个问题入手,了解到:软件 ...

  4. ASP 读取Word文档内容简单示例

    以下通过Word.Application对象来读取Doc文档内容并显示示例. 下面进行注册Word组件:1.将以下代码存档命名为:AxWord.wsc XML code复制代码 <?xml ve ...

  5. 【转】ExcelHelper类,用npoi读取Excel文档

    //------------------------------------------------------------------------------------- // All Right ...

  6. C#读取Excel文档

    上面分别是Excel文档的内容和读取结果: 奉上C#源代码: using System; using System.Data; using System.Data.OleDb; namespace R ...

  7. Java使用poi包读取Excel文档

    项目需要解析Excel文档获取数据,就在网上找了一些资料,结合自己这次使用,写下心得: 1.maven项目需加入如下依赖: <dependency> <groupId>org. ...

  8. NPOI 2.1.1 系列(1) 使用NPOI读取 Excel文档 ;NpoiExcelHelper 导入导出 2003格式 2007格式的 Excel; Npoi 导出 xlsx 格式

    下载地址 http://npoi.codeplex.com/releases 下面放一个 NPOIHelper 助手类吧,也不是我写的- NpoiExcelHelper 可以生成xlsx格式publi ...

  9. 读取EXCEL文档解析工具类

    package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...

随机推荐

  1. MySQL的一点浅显知识

    本人最近看了一本有关于MySQL的书籍<MySQL必知必会>,书中只写了一些基本知识,但是也基本涵盖了所有的MySQL的知识点.其余的比较高级的也只是在基础上进行扩展或者是优化,看完这本书 ...

  2. 转换时间对象和字符串对象&添加时间

    /* *基本思路,将字符串时间转化为时间对象,通过毫秒数来加减时间,然后在转化为字符串输出 */ //转化字符时间yy-mm-dd hh:mm:ss 为时间对象   使用split进行字符串的分割,取 ...

  3. JS -- Variables As Properties

    Variables As Properties When you declare a global JavaScript variable, what you are actually doing i ...

  4. 第4章 同步控制 Synchronization ----死锁(DeadLock)

    Jeffrey Richter 在他所主持的 Win32 Q&A 专栏(Microsoft Systems Journal,1996/07)中曾经提到过,Windows NT 和 Window ...

  5. vue学习之vue基本功能初探

    vue学习之vue基本功能初探: 采用简洁的模板语法将声明式的将数据渲染进 DOM: <div id="app"> {{ message }} </div> ...

  6. vue-chat项目之重构与体验优化

    前言 vue-chat 也就是我的几个月之前写的一个基于vue的实时聊天项目,到目前为止已经快满400star了,注册量也已经超过了1700+,消息量达2000+,由于一直在实习,没有时间对它频繁地更 ...

  7. SpringMVC加载.roperties文件属性值的方法?

    1.在xml文件中引入来获取属性值就不说了. 2.在controller层获取引用配置文件中的属性值: (1).编写工具类 @Configuration @PropertySource(value=& ...

  8. python中package注意事项

    个人工作中的SSD.Cardreader.Camera.Audio模块文档组织形式如下: RclLib __init__.py RclLegacy.py modules AgilentOp.py Uv ...

  9. DNS—正、反向解析;委派;主从;子域;转发;智能dns等的实现

    前言:DNS,耳熟能详的东西,内容太多,小编也不太好讲清,只能写几个实验详解,供大家参考. 一.简单介绍 1.DNS:通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析). 端 ...

  10. 【特效】hover效果之四线动画

    效果预览:http://www.gbtags.com/gb/rtreplayerpreview-standalone/3102.htm html: <div class="wrap&q ...